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

Modeling with MXNet

Updated at: Sep 12, 2019 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:

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 an OBS bucket and folder, see Creating a Bucket and Creating a Folder.
    Table 1 Folder list

    Folder

    Usage

    dataset-mnist

    Stores the dataset.

    train_mnist

    Stores the training script.

    mnist-model

    Stores the model and prediction files outputted in the training.

    train-log

    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. Go to the ModelArts-Lab project on GitHub, click Clone or download, and then click Download Zip to download the project.
      Figure 1 Downloading ModelArts-Lab
    2. After the project is downloaded, decompress the ModelArts-Lab-master.zip file, obtain training script train_mnist.py, inference code file customize_service.py, and configuration file config.json from the \ModelArts-Lab-master\official_examples\Using_MXNet_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.py

    Training script

    test-modelarts/train_mnist

    customize_service.py

    Inference code

    test-modelarts/mnist-model/model

    NOTE:

    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.

    config.json

    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.
  2. Decompress the Mnist-Data-Set.zip file to the Mnist-Data-Set directory on the local PC.
  3. Upload all files in the Mnist-Data-Set directory to the test-modelarts/dataset-mnist directory on OBS. For details about how to upload files, see Uploading a File.

    In the imported MNIST dataset, .gz files are compressed files with the same name and are not used this time. In this example, only the files that are not compressed are used. The used files are as follows:

    • t10k-images-idx3-ubyte: validation set, which contains 10,000 samples
    • t10k-labels-idx1-ubyte: labels of the validation set, which contains the labels of the 10,000 samples
    • train-images-idx3-ubyte: training set, which contains 60,000 samples
    • train-labels-idx1-ubyte: labels of the training set, which contains the labels of the 60,000 samples

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 train_mnist.py 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 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 automatically generated by the system. Set Name and Description as prompted.
      Figure 2 Entering the name and description
    2. In the parameter configuration area, set Data Source, Algorithm Source, Running Parameter, Training Output Path, and Job Log Path.

      Data Source: Select Data path, and then select the OBS path for saving the MNIST dataset.

      Algorithm Source: Select Commonly-used, set AI Engine to MXNet and MXNet-1.2.1-python2.7. Set Code Dir and Boot File. In this example, set Code Dir to /test-modelarts/train_mnist and Boot File to /test-modelarts/train_mnist/train_mnist.py. For details about how to upload the training script train_mnist.py, see Preparations.

      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.

      Training Output Path: Select the OBS path to the model and prediction files, 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.

      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 Parameter settings
    3. In the resource setting area, click Select on the right of the Resource Pool text box, select public resource pool Compute GPU (P100), and set Compute Nodes to 1.
      NOTE:

      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.

      Figure 4 Resource settings
    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

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

Obtain the customize_service.py 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 customize_service.py and config.json files to the model folder.
NOTE:

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 Existing meta model to the OBS path to the model, for example, test-modelarts/mnist-model. AI Engine is automatically selected based on the model file in the selected path. This example uses the MXNet engine.

    NOTE:

    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.

    Execution Code: The system automatically generates the path and file name of the corresponding inference code based on the selected meta model.
    Figure 7 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 a Service

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 Deploy in the Operation column and select Real-Time Service 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 created in Step 4: Import the Model for Model. Select a resource flavor from the drop-down list box of Instance Flavor, for example, 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: confidence score. In this example, the output result can be digit from 0 to 10, indicating that the output result displays the probabilities of 10 digits from 0 to 10.
    Figure 8 Deploying 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 > Service Deployment 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.

    NOTE:
    • 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.
    Figure 9 Prediction result

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, dataset, 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 a dataset, access OBS, delete the uploaded dataset, and delete the folder and OBS bucket.
  • To delete the training script and model files, log in to the OBS management console, delete the train_mnist, mnist-model, and train-log 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