[MindSpore two-day training camp 5th notes] Export MindIR format model

1. MindIR overview

MindSpore defines the logical structure of the network and the attributes of operators through unified IR, decouples the model file in MindIR format
from the hardware platform, and realizes multiple deployments for one training.
MindIR, as the unified model file of MindSpore, also stores the network structure and weight parameter values. At the same time, it supports
deployment to cloud Serving and end-side Lite platforms to perform inference tasks.
The same MindIR file supports the deployment of multiple hardware forms:-Serving
deployment reasoning
-End-side Lite reasoning deployment

Second, noun explanation

Checkpoint
• The Protocol Buffers format is used to store all parameter values ​​in the network.
• Generally used to resume training after a training task is interrupted, or to fine-tune (Fine Tune) tasks after training.
•MindIR
• The full name is MindSpore IR, which is a functional IR of MindSpore based on graph representation. It defines a scalable graph
structure and IR representation of operators.
• It eliminates model differences between different backends, and is generally used to perform inference tasks across hardware platforms.
•ONNX
• The full name is Open Neural Network Exchange, which is a general expression for machine learning models.
• Generally used for model migration between different frameworks or used on the inference engine (TensorRT).
•AIR
• The full name is Ascend Intermediate Representation, which is an open
file format designed for machine learning defined by Huawei .
• It can better adapt to the Huawei AI processor and is generally used to perform inference tasks on the Ascend 310.

3. Export the MindIR format model of the LeNet network

1. Train LeNet model to generate checkpoint

LeNet training code uses the code in modelzool in the official MindSpore code warehouse

Address: https://gitee.com/mindspore/mindspore/tree/master/model_zoo/official/cv/lenet

2. MNIST data download address

1) Official download address: http://yann.lecun.com/exdb/mnist/

2) Download from SkyDrive: Link: https://pan.baidu.com/s/1zX-OwL8bOgq4dhEuaRj2Xg Extraction code: zew6 

After the MNIST data set is downloaded, unzip it to the code root directory

3. Execute training commands

python train.py --data_path ./MNIST_DATA/ --ckpt_path=./checkpoint/ --device_target Ascend
Note: I use the Shengteng platform for model training. If you use CPU or GPU for training, then the device_target will be changed to the corresponding one.

When epoch, loss and other values ​​appear on the screen, the model starts training

4. Get the checkpoint

When the ckpt file appears in the ckpt_path folder specified in the model training parameters, the training is successful. Next, we can choose a ckpt file to convert to the mindir format. Here I choose checkpoint_lenet-10_1875.ckpt

5. Write model conversion code

import numpy as np
from mindspore import Tensor, export, load_checkpoint, load_param_into_net
from src.lenet import LeNet5

lenet = LeNet5()
# load the parameter into net
load_checkpoint("./checkpoint/checkpoint_lenet-10_1875.ckpt", net=lenet)  #checkpoint_lenet-10_1875.ckpt更换成对应所需要转换的ckpt文件
input = np.random.uniform(0.0, 1.0, size=[32, 1, 32, 32]).astype(np.float32)  #Lenet模型的size为32,1,32,32
export(lenet, Tensor(input), file_name='lenet-10_1875', file_format='MINDIR') #file_name指定转换后文件的文件名

6. Execute model conversion code

python lenet_mindr.py
After execution, check the root directory of the code. If there is the lenet-10_1875.mindir file we specified earlier, it means the model conversion is successful!

Fourth, export the MindIR format model of the ResNet50 network

1. Train ResNet50 network to generate checkpoint

The ResNet50 model training code still uses the modelzoo code in the official MindSpore code warehouse, link: https://gitee.com/mindspore/mindspore/tree/master/model_zoo/official/cv/resnet

2.Cifar10 data set

Because the ImageNet data set is relatively large, it takes a long time to train, so here we use the cifar10 data set for model training

1) Official download of Cifar10 dataset: http://www.cs.toronto.edu/~kriz/cifar.html

2) Baidu network disk download: link: https://pan.baidu.com/s/1CpgjFtZk2ZQsr_qUtc6z1g Extraction code: kjhi 

3. Execute training commands

python train.py --net resnet50 --dataset cifar10 --dataset_path ./data/cifar10-bin/train/  --device_target Ascend
Note: I use the Shengteng platform for model training. If you use CPU or GPU for training, then the device_target will be changed to the corresponding one.

When epoch, loss and other values ​​appear on the screen, the model starts training

resnet50.png

4. Get checkpoint

When the ckpt file appears in the ckpt_path folder specified in the model training parameters, the training is successful. Next, we can choose a ckpt file to convert to the mindir format. Here I choose resnet-90_1562.ckpt

5. Write model conversion scripts

import numpy as np
from mindspore import Tensor, export, load_checkpoint, load_param_into_net
from src.resnet import resnet50 as ResNet50

resnet = ResNet50()
# load the parameter into net
load_checkpoint("./checkpoint/resnet-90_1562.ckpt", net=resnet)
input = np.random.uniform(0.0, 1.0, size=[32, 3, 224, 224]).astype(np.float32)
export(resnet, Tensor(input), file_name='resnet-90_162', file_format='MINDIR')

6. Perform model conversion

python resnet_mindir.py
After execution, check the code root directory. If there is the resnet-90_1562.mindir file we specified earlier, it means that the model conversion is successful!

Guess you like

Origin blog.csdn.net/yichao_ding/article/details/113102073