Help Center> ModelArts> Best Practices> Frequently-used Frameworks> Using TensorFlow for Handwritten Digit Recognition

Using TensorFlow for Handwritten Digit Recognition

This section describes how to use TensorFlow to recognize handwritten digits and images from an MNIST dataset on the ModelArts platform.

Before using the following sample, complete necessary operations. For details, see Preparations. The following describes the process of using TensorFlow to recognize handwritten digits and images.

  1. Preparing Data: Obtain the MNIST dataset and upload it to OBS.
  2. Training a Model: Use the TensorFlow framework to compile the model training script and create a training job for model training.
  3. Deploying the Model: After the training is completed, import the model to ModelArts and deploy the model as a real-time service.
  4. Performing Prediction: Initiate a prediction request and obtain the prediction result.

Preparations

  • You have registered with HUAWEI CLOUD and checked the account status before using ModelArts. The account cannot be in arrears or frozen.
  • Obtain the AK/SK of the account and configure the AK/SK in Settings of ModelArts.
  • You have created a bucket and folders in OBS for storing the sample dataset and model. In this example, create a bucket named test-modelarts and folders listed in Table 1.
    For details about how to create OBS buckets and folders, see Creating a Bucket and Creating a Folder. Ensure that the OBS directory you use and ModelArts are in the same region.
    Table 1 Folder list

    Folder

    Usage

    dataset-mnist

    Stores the dataset.

    mnist-tensorflow-code

    Stores the training script.

    mnist-model

    Stores the model and prediction files outputted in the training.

  • In this example, ModelArts provides the training script, inference code, and configuration file for service deployment. Obtain the sample files and upload them to the specified OBS path. For details about the files to be uploaded, see Table 2.
    1. Go to the ModelArts-Lab project on Gitee, click Clone/Download, and then click Download ZIP to download the project.
    2. After the project is downloaded, decompress the ModelArts-Lab-master.zip file, obtain training script train_mnist_tf.py, inference code customize_service.py, and configuration file config.json from the \ModelArts-Lab-master\official_examples\Using_TensorFlow_to_Create_a_MNIST_Dataset_Recognition_Application\codes directory, and upload them to the corresponding OBS path. For details about how to upload files to OBS, see Uploading a File.
    Table 2 Files to be uploaded to OBS

    File

    Description

    OBS Path

    train_mnist_tf.py

    Training script

    test-modelarts/mnist-tensorflow-code

    customize_service.py

    Inference code

    test-modelarts/mnist-model/model

    NOTE:
    • Upload the files after the training is completed.
    • If the training job runs multiple times, multiple versions are generated. That is, directories of multiple versions, such as V0001 and V0002, are generated in the mnist-model directory. Upload the files to the model folder of the corresponding training job version.

    config.json

    Configuration file

Preparing Data

ModelArts provides a sample MNIST dataset named Mnist-Data-Set. This example uses this dataset to build a model. Perform the following operations to upload the dataset to the OBS directory test-modelarts/dataset-mnist created in preparation.

  1. Download the Mnist-Data-Set dataset to the local PC.
  2. Decompress the Mnist-Data-Set.zip file, for example, to the Mnist-Data-Set directory on the local PC.
  3. Upload all files in the Mnist-Data-Set folder to the test-modelarts/dataset-mnist directory on OBS in batches. For details about how to upload files, see Uploading a File.

    The following provides content of the Mnist-Data-Set dataset. .gz is the compressed package.

    • t10k-images-idx3-ubyte: validation set, which contains 10,000 samples
    • t10k-images-idx3-ubyte.gz: compressed package file of the validation set.
    • t10k-labels-idx1-ubyte: labels of the validation set, which contains the labels of the 10,000 samples
    • t10k-labels-idx1-ubyte.gz: compressed package file of the validation set label.
    • train-images-idx3-ubyte: training set, which contains 60,000 samples
    • train-images-idx3-ubyte.gz: compressed package file of the training set.
    • train-labels-idx1-ubyte: labels of the training set, which contains the labels of the 60,000 samples
    • train-labels-idx1-ubyte.gz: compressed package file of the training set label.

Training a Model

After the data preparation is completed, use TensorFlow to compile the training script code. ModelArts provides a code sample, train_mnist_tf.py. The following uses this sample to train the model.

  1. Upload the train_mnist_tf.py file to OBS, for example, to the test-modelarts/mnist-tensorflow-code directory. For details, see Preparations.
  2. On the ModelArts management console, choose Training Management > Training Jobs, and click Create in the upper left corner.
  3. On the Create Training Job page, set required parameters based on Figure 1 and Figure 2, and click Next.
    Figure 1 Basic information for creating a training job
    Figure 2 Parameters for creating a training job
  4. On the Confirm tab page, check the parameters of the training job and click Submit.
  5. On the Training Jobs page, when the training job status changes to Running Success, the model training is completed. If any exception occurs, click the job name to go to the job details page and view the training job logs.

    The training job may take more than 10 minutes to complete. If the training time exceeds a certain period (for example, one hour), manually stop it to release resources. Otherwise, the account balance may be insufficient, especially for the models that are trained using GPUs.

  6. (Optional) During or after model training, you can create a visualization job to view parameter statistics. For details, see Creating a Visualization Job.

    Set Training Output Path to the value of Training Output Path for the training job, for example, /test-modelarts/mnist-model/. Complete visualization job creation as prompted.

Deploying the Model

After the model training is completed, create a prediction job and deploy the model as a real-time prediction service. Before deploying the model, obtain the customize_service.py and config.json configuration files and upload them to OBS. This inference code and configuration files are sample files provided by ModelArts. You can also develop your own inference code and configuration files based on Model Package Specifications.

  1. Before deploying the model, upload the inference code and configuration files to the corresponding OBS path. For details, see Preparations. In this example, the OBS path for saving the outputted model is test-modelarts/mnist-model/model.
  2. On the ModelArts management console, choose Model Management > Models in the left navigation pane. On the displayed Models page, click Import.
  3. On the Import Model page, set required parameters as shown in Figure 3 and click Next.
    In the Meta Model Source area, select OBS. Set Meta Model to the path specified by Training Output Path in the training job.
    Figure 3 Import Model

    On the Models page, if the model status changes to Normal, the model has been imported successfully.

  4. Click the triangle next to a model name to expend all versions of the model. In the row of a version, choose Deploy > Real-Time Services in the Operation column to deploy the model as a real-time service.
    On the Deploy page, set parameters shown in Figure 4 and follow the instructions to create a real-time service.
    Figure 4 Deploying the Model

Performing Prediction

After the model is deployed, wait until the service deployment is completed. If the service status changes to Running, the service has been deployed successfully.

  1. On the Real-Time Services page, click the name of the real-time service. The real-time service details page is displayed.
  2. Choose Prediction. Click Upload next to Image File to upload an image with a white handwritten digit on a black background and click Predict.

    After the prediction is completed, the prediction result is displayed in the Test Result pane. According to the prediction result, the digit on the image is 1.

    • As specified in the inference code and configuration files, the size of the image used for prediction must be 28 x 28 pixels, and the image must contain white handwritten digits on a black background.
    • You are advised not to use the images provided by the dataset. You can use the drawing tool provided by the Windows operating system to draw an image for prediction.
    Figure 5 Prediction results
  3. If you do not need to use this model and real-time service any more, clear related resources to avoid unnecessary fees.
    • On the Real-Time Services page, choose More > Stop or Delete to stop or delete the created real-time service.
    • On the Model Management page, choose More > Delete to delete the imported model.
    • On the Training Jobs page, choose More > Delete to delete the finished training job.
    • Go to OBS and delete the OBS bucket, folders, and files in this example.