本文主要讲解如何在端侧进行LeNet模型训练。首先在服务器或个人笔记本上进行模型转换;然后在安卓设备上训练模型。LeNet由2层卷积和3层全连接层组成,模型结构简单,因此可以在设备上快速训练。
本例使用MNIST手写字数据集
数据集大小:52.4M, 60,000 28*28 10类
数据格式:二进制文件
数据集目录结构如下:
mnist/
├── test
│ ├── t10k-images-idx3-ubyte
│ └── t10k-labels-idx1-ubyte
└── train
├── train-images-idx3-ubyte
└── train-labels-idx1-ubyte
安装完毕,在./mindspore/mindspore/lite/examples/train_lenet目录下执行脚本,命令如下:
sh ./prepare_and_run.sh -D DATASET_PATH [-d MINDSPORE_DOCKER] [-r RELEASE.tar.gz] [-t arm64|x86]
其中,DATASET_PATH是数据集路径;MINDSPORE_DOCKER是运行MindSpore的docker镜像,如果没有使用docker环境,则使用本地运行;REALEASE.tar.gz为端侧运行时训练工具压缩包绝对路径;-t选项为设备处理器架构,默认为arm64,如果输入x86则本地运行。注意:若在不同平台执行训练,需在先执行脚本前运行make clean指令。
prepare_and_run.sh脚本的功能如下:
.ms文件。运行命令参见快速入门
脚本prepare_model.sh会基于MIndSpore架构将Python模型转换为lenet_tod.mindir模型;然后,使用MindSpore ToD 模型转换工具将lenet_tod.mindir文件转换为lenet_tod.ms文件。如果没有docker环境,则本地执行转换。
将lenet_tod.ms模型文件、训练脚本、MindSpore ToD库文件和MNIST数据集拷贝到package文件夹。/src文件夹中代码将会被编译成arm64架构版本,生成的二进制文件会被拷贝至package文件夹。最后使用adb工具将package文件夹传输至设备端,并执行训练。
train_lenet/
├── Makefile # Makefile of src code
├── model
│ ├── lenet_export.py # Python script that exports the LeNet model to .mindir
│ ├── prepare_model.sh # script that export model (using docker) then converts it
│ └── train_utils.py # utility function used during the export
├── prepare_and_run.sh # main script that creates model, compiles it and send to device for running
├── README.md # this manual
├── scripts
│ ├── eval.sh # on-device script that load the train model and evaluates its accuracy
│ ├── run_eval.sh # adb script that launches eval.sh
│ ├── run_train.sh # adb script that launches train.sh
│ └── train.sh # on-device script that load the initial model and train it
├── src
│ ├── dataset.cc # dataset handler
│ ├── dataset.h # dataset class header
│ ├── net_runner.cc # program that runs training/evaluation of models
│ └── net_runner.h # net_runner header
在脚本prepare_and_run.sh运行前,必须确保以下目录结构正确,这些文件将被传入设备用于训练。
├── package
│ ├── bin
│ │ └── net_runner # the executable that performs the training/evaluation
│ ├── dataset
│ │ ├── test
│ │ │ ├── t10k-images-idx3-ubyte # test images
│ │ │ └── t10k-labels-idx1-ubyte # test labels
│ │ └── train
│ │ ├── train-images-idx3-ubyte # train images
│ │ └── train-labels-idx1-ubyte # train labels
│ ├── eval.sh # on-device script that load the train model and evaluates its accuracy
│ ├── lib
│ │ └── libmindspore-lite.so # MindSpore Lite library
│ ├── model
│ │ └── lenet_tod.ms # model to train
│ └── train.sh # on-device script that load the initial model and train it