Help Center > > Getting Started> AI Engineers: AI Development Lifecycle> Modeling with MXNet

Modeling with MXNet

Updated at: May 09, 2020 GMT+08:00

ModelArts is a one-stop AI development platform, on which AI engineers can manage the AI development lifecycle, including data preparation, model development, model training, and model deployment. It is compatible with mainstream engines in the industry and different user habits. In addition, ModelArts provides the Huawei-developed MoXing deep learning framework to improve the algorithm development efficiency and training speed.

This section describes how to use MXNet to develop a handwritten digit recognition application, helping you quickly learn about AI development with ModelArts.

MNIST is a dataset containing handwritten digits, and is often used as an introductory example of deep learning. In this example, you use a model training script (provided by the ModelArts by default) compiled using the native interface of MXNet based on the MNIST dataset for model training on ModelArts, and deploy the model as a real-time service. After the deployment is completed, you can use the real-time service to identify whether an input image contains a certain digit.

Before you start, carefully complete the preparations described in Preparations in advance. The procedure for building a model is as follows:


  • You have registered with HUAWEI CLOUD and checked the account status before using ModelArts. The account cannot be in arrears or frozen.
  • You have obtained the AK/SK of the account and configured 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 an OBS bucket and folder, 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




    Stores the dataset.


    Stores the training script.


    Stores the model and prediction files outputted in the training.


    Stores training job logs.

  • 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. Obtain the sample files.
      Go to the ModelArts-Lab project on GitHub, click Clone or download, and then click Download ZIP to download the project.
      Figure 1 Downloading the ModelArts-Lab

      After the project is downloaded, decompress the file. The sample files are stored in the following path: \ModelArts-Lab-master\official_examples\Using_MXNet_to_Create_a_MNIST_Dataset_Recognition_Application\codes

    2. Upload the sample files 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



    OBS Path

    Training script


    Inference code



    Upload the files after the training is completed. For details, see Step 3: Compile the Inference Code and Configuration Files and Upload Them to the Path of the Model.


    Configuration file

Step 1: Prepare 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 by clicking the download link.
  2. Decompress the file 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.

Step 2: Train a Model

After data preparation is completed, you can create a training job, select the MXNet engine, and generate an available model based on the uploaded training script. This example provides a compiled code script (based on the native interface of the MXNet engine). If you use your own code, select the engine and version supported by ModelArts.

  1. Log in to the ModelArts management console. In the left navigation pane, choose Training Management > Training Jobs. The Training Jobs page is displayed.
  2. Click Create. The Create Training Job page is displayed.
  3. On the Create Training Job page, set required parameters.
    1. In the basic information area, retain the default values for Billing Mode and Version, and enter a name and description as prompted.
      Figure 2 Entering a name and description
    2. In the parameter configuration area, set Data Source, Algorithm Source, Running Parameter, Training Output Path, and Job Log Path.
      • Algorithm Source: Select Frequently-used, set AI Engine to MXNet and MXNet-1.2.1-python2.7. Set Code Directory and Boot File. In this example, set Code Directory to test-modelarts/MXNet-mnist/code/ and Boot File to test-modelarts/MXNet-mnist/code/ For details about how to upload the training script, see Preparations.
      • Data Source: Select Data path, and then select the OBS path for saving the MNIST dataset.
      • Training Output Path: Select the OBS path to store the model and prediction files generated by the training job, that is, select the created mnist-model folder. If no folder is available, click Select and create a folder in the dialog box that is displayed.
      • Running Parameter: Click Add Running Parameter, and add max_epoches=10. The entire dataset is trained in 1 epoch. If you set max_epoches to 10, 10 epochs of training are performed. The value can be changed. If this parameter is not set, the default value 100 is used. The training duration prolongs as the value of max_epoches increases.
      • Job Log Path: Select the OBS path to store job logs, that is, select the created train-log folder. If no folder is available, click Select and create a folder in the dialog box that is displayed.
      Figure 3 Configuring job parameters
    3. In the resource setting area, select Public resource pools, and set Type, Specifications, and Compute Nodes.

      The MNIST dataset has a large amount of data. To improve the training efficiency, select a GPU-based resource pool for training. However, the cost of the GPU is higher than that of the CPU. You can select an available resource pool based on the actual situation.

      If you select free specifications, read the prompt carefully.

      Figure 4 Configuring resources
    4. Click Next.
  4. On the Confirm tab page, check the parameters of the training job and click Submit.
  5. On the Training Jobs page, view the status of the created training job. It takes a couple of minutes to create and run a training job. When the job status changes to Successful, the training job is successfully created.

    You can click the name of the training job to go to the job details page and learn about the configurations, logs, and resource usage of the training job. You can obtain the generated model file from the OBS path specified by Training Output Path, that is, /test-modelarts/mnist-model/.

    Figure 5 Training job details page

Step 3: Compile the Inference Code and Configuration Files and Upload Them to the Path of the Model

Obtain the and config.json files by referring to Preparations. This inference code and configuration files are sample files provided by ModelArts.

In Step 2: Train a Model, the model generated by the training job is stored in the test-modelarts/mnist-model directory, and the system automatically generates the model folder for storing the model files. Upload the obtained and config.json files to the model folder.

If the training job runs multiple times, Training Output Path has multiple values, 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.

Go to the OBS Console, find the test-modelarts bucket, go to the test-modelarts/mnist-model/model directory, and click Upload Object to upload objects. For details about how to upload files to OBS, see Uploading a File.

Figure 6 Uploading the inference code and configuration files

Step 4: Import the Model

The trained model is stored in the OBS path. You can import the model to ModelArts for management and deployment.

  1. On the ModelArts management console, choose Model Management > Models in the left navigation pane. The Models page is displayed.
  2. On the Models page, click Import.
  3. On the Import Model page, set required parameters and click Next.
    • Set Name and Version.
    • Meta Model Source: Select OBS. Set Meta Model to the OBS path to store the model, for example, test-modelarts/MXNet-mnist/output/. AI Engine is automatically selected based on the model file in the selected path. This example uses the MXNet engine.

      When selecting the model path, you must select the parent directory of the model folder. If you select the model folder, the system cannot identify the model and configuration files and will fail to import the model.

    • Inference Code: The system automatically generates the path and file name of the corresponding inference code based on the selected meta model.
    Figure 7 Page for importing a model
  4. After the model is imported, the Models page is displayed. You can view the imported model and its versions on the Models page.

Step 5: Deploy the Model as a Service

When the status of a model is normal after the model is imported, deploy it as a real-time, batch, or edge service. The following describes how to deploy a real-time service.

  1. On the Models page, click the triangle next to a model name to expend all versions of the model. In the row of the target model version, click Deploy in the Operation column and select Real-Time Services from the drop-down list box. The Deploy page is displayed.
  2. On the Deploy page, set required parameters and click Next.

    Set Name for the service. In Model and Configuration, the system automatically selects the model and version used in Step 4: Import the Model. Select resource specifications from the drop-down list box of Specifications, for example, CPU: 2 vCPUs | 8 GiB.

    Environment Variable: In this example, three pieces of information need to be added to the inference code and configuration files. For details, see the following instructions.

    • input_data_name: The value must be images. This parameter is mandatory for the scenario where the training script is developed by yourself. You can set this parameter when importing the model, or write it into the inference code.
    • input_data_shape: NCHW. In this example, the value is 0,1,28,28, indicating the requirements on the input images. The size of the input images must be 28 x 28 pixels.
    • output_data_shape: number of model output classes. In this example, the value range is 0,10, indicating that the output result displays the probabilities of 10 digits from 0 to 9. This range must be consistent with the number of classes in the training code.
    Figure 8 Deploying the model as a real-time service
  3. On the Confirm tab page, check the configurations and click Submit to create a real-time service.
  4. Choose Service Deployment > Real-Time Services to view information about the real-time service. It takes several minutes to deploy the model. When the service status changes to Running, the real-time service is successfully deployed.

Step 6: Test the Service

After the real-time service is deployed, access the service to send a prediction request for test.

  1. On the Real-Time Services management page, click the name of the real-time service. The real-time service details page is displayed.
  2. On the real-time service details page, click the Prediction tab.
  3. 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 probability of the digit 8 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 must contain white handwritten digit on the 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.
    • The sample code outputs only the classes with top 5 confidence.
    Figure 9 Prediction success

Step 7: Delete Related Resources to Avoid Unnecessary Charging

To avoid unnecessary charging, you are advised to delete related resources, such as the real-time service, training job, data, and OBS directories after trial use.

  • To delete a real-time service, go to the Real-Time Services page, and choose More > Delete in the Operation column.
  • To delete a training job, go to the Training Jobs page and click Delete in the Operation column.
  • To delete data, access OBS, delete the uploaded data, and delete the folder and OBS bucket.
  • To delete the training script and model files, log in to the OBS management console, delete the related folders and data, and delete the test-modelarts bucket.

Did you find this page helpful?

Submit successfully!

Thank you for your feedback. Your feedback helps make our documentation better.

Failed to submit the feedback. Please try again later.

Which of the following issues have you encountered?

Please complete at least one feedback item.

Content most length 200 character

Content is empty.

OK Cancel