This topic describes how to use Machine Learning Platform for AI (PAI)-TensorFlow to build an image classification model.
Prerequisites
- An Object Storage Service (OSS) bucket is created. PAI is authorized to access the
OSS bucket. For more information, see Create buckets and OSS authorization.
Notice When you create an OSS bucket, make sure that versioning is disabled for the bucket. Otherwise, model training may fail.
- Graphics processing unit (GPU) is enabled for the PAI project where you want to build the model. For more information, see GPU resource authorization.
Background information
As the Internet develops, masses of images and voice data have been generated. Such
unstructured data is difficult to be mined and effectively used due to the following
reasons:
- Deep learning algorithms are required, which are difficult to learn.
- GPU compute engines are required, which causes high cost.
Machine Learning Studio presets a template that uses a deep learning framework to build image classification models. You can create an experiment by using the template and reuse the experiment for other purposes such as pornographic image detection, facial recognition, and object detection.
Dataset
In the following sample experiment, the CIFAR-10 dataset is used. The CIFAR-10 dataset
contains 60,000 color images of 32 × 32 pixels. The images fall into 10 categories,
including airplane, automobile, bird, cat, deer, dog, frog, horse, ship, and truck,
as shown in the following figure. For more information about the download URLs of
the dataset and relevant code, see CIFAR-10 dataset. 

In the experiment, the dataset is divided into a training dataset of 50,000 images
and a prediction dataset of 10,000 images. The training dataset is further divided
into five training batches. The entire prediction dataset is used as a prediction
batch. Each batch is a separate file, as shown in the following figure. 

Prepare data
Upload the dataset files and relevant code that are used for this experiment to an
OSS bucket. For example, you can create the aohai_test folder in an OSS bucket and four subfolders in the aohai_test folder, as shown in
the following figure.
The four subfolders are used for different purposes:

- check_point: stores the model that is generated by the experiment.
Note After you create an experiment by using the preset template in Machine Learning Studio, you must set the Checkpoint Output Directory/Model Input Directory parameters of the TensorFlow components to the path of an existing OSS folder. Otherwise, you cannot run the experiment. In this experiment, set the Checkpoint Output Directory/Model Input Directory parameters of the TensorFlow components to the path of the check_point subfolder.
- cifar-10-batches-py: stores the cifar-10-batcher-py file of the training dataset and the bird_mount_bluebird.jpg file of the prediction dataset.
- train_code: stores the cifar_pai.py file, which contains the training code.
- predict_code: stores the cifar_predict_pai.py file, which contains the prediction code.
Procedure
- Go to a Machine Learning Studio project.
- Log on to the Machine Learning Platform for AI (PAI) console.
- In the left-side navigation pane, choose .
- In the upper-left corner of the page, select the region that you want to manage.
- Optional:In the search box on the PAI Visualization Modeling page, enter the name of a project to search for the project.
- Find the project and click Machine Learning in the Actions column.
- Create an experiment.
- Run the experiment and view the result.
- In the top toolbar of the canvas, click Run.
- After the experiment is run, you can view the prediction result that is stored in the specified OSS path(Checkpoint Output Directory/Model Input Directory).
Training code
This section describes the key code in the cifar_pai.py file.
- The following code can be used to train a convolutional neural network (CNN) model
for image classification:
network = input_data(shape=[None, 32, 32, 3], data_preprocessing=img_prep, data_augmentation=img_aug) network = conv_2d(network, 32, 3, activation='relu') network = max_pool_2d(network, 2) network = conv_2d(network, 64, 3, activation='relu') network = conv_2d(network, 64, 3, activation='relu') network = max_pool_2d(network, 2) network = fully_connected(network, 512, activation='relu') network = dropout(network, 0.5) network = fully_connected(network, 10, activation='softmax') network = regression(network, optimizer='adam', loss='categorical_crossentropy', learning_rate=0.001)
- The following code can be used to generate a model named model.tfl:
model = tflearn.DNN(network, tensorboard_verbose=0) model.fit(X, Y, n_epoch=100, shuffle=True, validation_set=(X_test, Y_test), show_metric=True, batch_size=96, run_id='cifar10_cnn') model_path = os.path.join(FLAGS.checkpointDir, "model.tfl") print(model_path) model.save(model_path)
Prediction code
This section describes the key code in the cifar_predict_pai.py file. First, the system reads the bird_bullocks_oriole.jpg image file and resizes the image to 32 × 32 pixels. Then, the system passes the image
to the model.predict function. The output of the function is the weights of the 10 categories as which
the image is recognized: ['airplane','automobile','bird','cat','deer','dog','frog','horse','ship','truck']. Finally, the system returns the category with the highest weight as the prediction
result.
predict_pic = os.path.join(FLAGS.buckets, "bird_bullocks_oriole.jpg")
img_obj = file_io.read_file_to_string(predict_pic)
file_io.write_string_to_file("bird_bullocks_oriole.jpg", img_obj)
img = scipy.ndimage.imread("bird_bullocks_oriole.jpg", mode="RGB")
# Scale it to 32x32
img = scipy.misc.imresize(img, (32, 32), interp="bicubic").astype(np.float32, casting='unsafe')
# Predict
prediction = model.predict([img])
print (prediction[0])
print (prediction[0])
#print (prediction[0].index(max(prediction[0])))
num=['airplane','automobile','bird','cat','deer','dog','frog','horse','ship','truck']
print ("This is a %s"%(num[prediction[0].index(max(prediction[0]))]))