• Home
Name Date Size #Lines LOC

..--

model/12-May-2024-12377

scripts/12-May-2024-573

src/12-May-2024-517384

.gitignoreD12-May-202428 54

MakefileD12-May-20241.6 KiB5843

README.mdD12-May-20246.7 KiB146112

README_CN.mdD12-May-20245.7 KiB135105

prepare_and_run.shD12-May-20244.3 KiB188151

README.md

1# Content
2
3<!-- TOC -->
4
5- [Overview](#overview)
6- [Model Architecture](#model-architecture)
7- [Dataset](#dataset)
8- [Environment Requirements](#environment-requirements)
9- [Quick Start](#quick-start)
10- [Script Detailed Description](#script-detailed-description)
11
12<!-- /TOC -->
13
14# Overview
15
16This folder holds code for Training-on-Device of a LeNet model. Part of the code runs on a server using MindSpore infrastructure, another part uses MindSpore Lite conversion utility, and the last part is the actual training of the model on some android-based device.
17
18# Model Architecture
19
20LeNet is a very simple network which is composed of only 5 layers, 2 of which are convolutional layers and the remaining 3 are fully connected layers. Such a small network can be fully trained (from scratch) on a device in a short time. Therefore, it is a good example.
21
22# Dataset
23
24In this example we use the MNIST dataset of handwritten digits as published in [THE MNIST DATABASE](http://yann.lecun.com/exdb/mnist/)
25
26- Dataset size:52.4M,60,000 28*28 in 10 classes
27    - Test:10,000 images
28    - Train:60,000 images
29- Data format:binary files
30    - Note:Data will be processed in dataset.cc
31
32- The dataset directory structure is as follows:
33
34```text
35mnist/
36├── test
37│   ├── t10k-images-idx3-ubyte
38│   └── t10k-labels-idx1-ubyte
39└── train
40    ├── train-images-idx3-ubyte
41    └── train-labels-idx1-ubyte
42```
43
44# Environment Requirements
45
46- Server side
47    - [MindSpore Framework](https://www.mindspore.cn/install/en): it is recommended to install a docker image
48    - MindSpore ToD Framework
49        - [Downloads](https://www.mindspore.cn/lite/docs/en/r1.5/use/downloads.html)
50        - [Build](https://www.mindspore.cn/lite/docs/en/r1.5/use/build.html)
51    - [Android NDK r20b](https://dl.google.com/android/repository/android-ndk-r20b-linux-x86_64.zip)
52    - [Android SDK](https://developer.android.com/studio?hl=zh-cn#cmdline-tools)
53- A connected Android device
54
55# Quick Start
56
57After installing all the above mentioned, the script in the home directory could be run with the following arguments:
58
59```bash
60sh ./prepare_and_run.sh -D DATASET_PATH [-d MINDSPORE_DOCKER] [-r RELEASE.tar.gz] [-t arm64|x86]
61```
62
63where:
64
65- DATASET_PATH is the path to the [dataset](#dataset),
66- MINDSPORE_DOCKER is the image name of the docker that runs [MindSpore](#environment-requirements). If not provided MindSpore will be run locally
67- RELEASE.tar.gz is a pointer to the MindSpore ToD release tar ball. If not provided, the script will attempt to find MindSpore ToD compilation output
68- target is defaulted to arm64, i.e., on-device. If x86 is provided, the demo will be run locally. Note that infrastructure is not optimized for running on x86. Also, note that user needs to call "make clean" when switching between targets.
69
70# Script Detailed Description
71
72The provided `prepare_and_run.sh` script is performing the following:
73
74- Prepare the trainable lenet model in a `.ms` format
75- Prepare the folder that should be pushed into the device
76- Copy this folder into the device and run the scripts on the device
77
78See how to run the script and parameters definitions in the [Quick Start Section](#quick-start)
79
80## Preparing the model
81
82Within the model folder a `prepare_model.sh` script uses MindSpore infrastructure to export the model into a `.mindir` file. The user can specify a docker image on which MindSpore is installed. Otherwise, the python script will be run locally.
83The script then converts the `.mindir` to a `.ms` format using the MindSpore ToD converter.
84The script accepts a tar ball where the converter resides. Otherwise, the script will attempt to find the converter in the MindSpore ToD build output directory.
85
86## Preparing the Folder
87
88The `lenet_tod.ms` model file is then copied into the `package` folder as well as scripts, the MindSpore ToD library and the MNIST dataset.
89Finally, the code (in src) is compiled for arm64 and the binary is copied into the `package` folder.
90
91### Running the code on the device
92
93To run the code on the device the script first uses `adb` tool to push the `package` folder into the device. It then runs training (which takes some time) and finally runs evaluation of the trained model using the test data.
94
95# Folder Directory tree
96
97``` python
98train_lenet/
99├── Makefile              # Makefile of src code
100├── model
101│   ├── lenet_export.py   # Python script that exports the LeNet model to .mindir
102│   ├── prepare_model.sh  # script that export model (using docker) then converts it
103│   └── train_utils.py    # utility function used during the export
104├── prepare_and_run.sh    # main script that creates model, compiles it and send to device for running
105├── README.md             # English manual
106├── README_CN.md          # Chinese manual
107├── scripts
108│   ├── eval.sh           # on-device script that load the train model and evaluates its accuracy
109│   └── train.sh          # on-device script that load the initial model and train it
110├── src
111│   ├── net_runner.cc     # program that runs training/evaluation of models
112│   ├── net_runner.h      # net_runner header
113│   └── utils.h           # general utilities
114```
115
116When the `prepare_and_run.sh` script is run, the following folder is prepared. It is pushed to the device and then training runs
117
118``` python
119├── package
120│   ├── bin
121│   │   └── net_runner                   # the executable that performs the training/evaluation
122│   ├── dataset
123│   │   ├── test
124│   │   │   ├── t10k-images-idx3-ubyte   # test images
125│   │   │   └── t10k-labels-idx1-ubyte   # test labels
126│   │   └── train
127│   │       ├── train-images-idx3-ubyte  # train images
128│   │       └── train-labels-idx1-ubyte  # train labels
129│   ├── eval.sh                          # on-device script that load the train model and evaluates its accuracy
130│   ├── lib
131│   │   ├── libjpeg.so.62
132│   │   ├── libminddata-lite.a
133│   │   ├── libminddata-lite.so
134│   │   ├── libmindspore-lite.a
135│   │   ├── libmindspore-lite-jni.so
136│   │   ├── libmindspore-lite.so
137│   │   ├── libmindspore-lite-train.a
138│   │   ├── libmindspore-lite-train-jni.so
139│   │   ├── libmindspore-lite-train.so
140│   │   ├── libturbojpeg.so.0
141│   │   └── mindspore-lite-java.jar
142│   ├── model
143│   │   └── lenet_tod.ms                 # model to train
144│   └── train.sh                         # on-device script that load the initial model and train it
145```
146

README_CN.md

1# 目录
2
3<!-- TOC -->
4
5- [目录](#目录)
6- [概述](#概述)
7- [数据集](#数据集)
8- [环境要求](#环境要求)
9- [快速入门](#快速入门)
10- [脚本详述](#脚本详述)
11    - [模型准备](#模型准备)
12    - [模型训练](#模型训练)
13- [工程目录](#工程目录)
14
15<!-- /TOC -->
16
17# 概述
18
19本文主要讲解如何在端侧进行LeNet模型训练。首先在服务器或个人笔记本上进行模型转换;然后在安卓设备上训练模型。LeNet由2层卷积和3层全连接层组成,模型结构简单,因此可以在设备上快速训练。
20
21# 数据集
22
23本例使用[MNIST手写字数据集](http://yann.lecun.com/exdb/mnist/)
24
25- 数据集大小:52.4M, 60,000 28*28 10类
26    - 测试集:10,000 images
27    - 训练集:60,000 images
28
29- 数据格式:二进制文件
30    - 注意:数据处理会在dataset.cc中进行。
31
32- 数据集目录结构如下:
33
34```text
35mnist/
36├── test
37│   ├── t10k-images-idx3-ubyte
38│   └── t10k-labels-idx1-ubyte
39└── train
40    ├── train-images-idx3-ubyte
41    └── train-labels-idx1-ubyte
42```
43
44# 环境要求
45
46- 服务器或个人笔记本
47    - [MindSpore Framework](https://www.mindspore.cn/install): 建议使用Docker安装
48    - [MindSpore ToD Download](https://www.mindspore.cn/lite/docs/zh-CN/r1.5/use/downloads.html)
49    - [MindSpore ToD Build](https://www.mindspore.cn/lite/docs/zh-CN/r1.5/use/build.html)
50    - [Android NDK r20b](https://dl.google.com/android/repository/android-ndk-r20b-linux-x86_64.zip)
51    - [Android SDK](https://developer.android.com/studio?hl=zh-cn#cmdline-tools)
52- Android移动设备
53
54# 快速入门
55
56安装完毕,在`./mindspore/mindspore/lite/examples/train_lenet`目录下执行脚本,命令如下:
57
58```bash
59sh ./prepare_and_run.sh -D DATASET_PATH [-d MINDSPORE_DOCKER] [-r RELEASE.tar.gz] [-t arm64|x86]
60```
61
62其中,`DATASET_PATH`是数据集路径;`MINDSPORE_DOCKER`是运行MindSpore的docker镜像,如果没有使用docker环境,则使用本地运行;`REALEASE.tar.gz`为端侧运行时训练工具压缩包绝对路径;`-t`选项为设备处理器架构,默认为`arm64`,如果输入`x86`则本地运行。注意:若在不同平台执行训练,需在先执行脚本前运行`make clean`指令。
63
64# 脚本详述
65
66`prepare_and_run.sh`脚本的功能如下:
67
68- 将Python模型文件转换为`.ms`文件。
69- 编译训练源码并将相关文件传输到设备端
70- 设备端执行训练
71
72运行命令参见[快速入门](#快速入门)
73
74## 模型准备
75
76脚本`prepare_model.sh`会基于MIndSpore架构将Python模型转换为`lenet_tod.mindir`模型;然后,使用MindSpore ToD 模型转换工具将`lenet_tod.mindir`文件转换为`lenet_tod.ms`文件。如果没有docker环境,则本地执行转换。
77
78## 模型训练
79
80将`lenet_tod.ms`模型文件、训练脚本、MindSpore ToD库文件和`MNIST`数据集拷贝到`package`文件夹。`/src`文件夹中代码将会被编译成arm64架构版本,生成的二进制文件会被拷贝至`package`文件夹。最后使用`adb`工具将`package`文件夹传输至设备端,并执行训练。
81
82# 工程目录
83
84``` txt
85train_lenet/
86├── Makefile              # Makefile of src code
87├── model
88│   ├── lenet_export.py   # Python script that exports the LeNet model to .mindir
89│   ├── prepare_model.sh  # script that export model (using docker) then converts it
90│   └── train_utils.py    # utility function used during the export
91├── prepare_and_run.sh    # main script that creates model, compiles it and send to device for running
92├── README.md             # this manual
93├── scripts
94│   ├── eval.sh           # on-device script that load the train model and evaluates its accuracy
95│   ├── run_eval.sh       # adb script that launches eval.sh
96│   ├── run_train.sh      # adb script that launches train.sh
97│   └── train.sh          # on-device script that load the initial model and train it
98├── src
99│   ├── dataset.cc        # dataset handler
100│   ├── dataset.h         # dataset class header
101│   ├── net_runner.cc     # program that runs training/evaluation of models
102│   └── net_runner.h      # net_runner header
103```
104
105在脚本`prepare_and_run.sh`运行前,必须确保以下目录结构正确,这些文件将被传入设备用于训练。
106
107``` txt
108├── package
109│   ├── bin
110│   │   └── net_runner                   # the executable that performs the training/evaluation
111│   ├── dataset
112│   │   ├── test
113│   │   │   ├── t10k-images-idx3-ubyte   # test images
114│   │   │   └── t10k-labels-idx1-ubyte   # test labels
115│   │   └── train
116│   │       ├── train-images-idx3-ubyte  # train images
117│   │       └── train-labels-idx1-ubyte  # train labels
118│   ├── eval.sh                          # on-device script that load the train model and evaluates its accuracy
119│   ├── lib
120│   │   ├── libjpeg.so.62
121│   │   ├── libminddata-lite.a
122│   │   ├── libminddata-lite.so
123│   │   ├── libmindspore-lite.a
124│   │   ├── libmindspore-lite-jni.so
125│   │   ├── libmindspore-lite.so
126│   │   ├── libmindspore-lite-train.a
127│   │   ├── libmindspore-lite-train-jni.so
128│   │   ├── libmindspore-lite-train.so
129│   │   ├── libturbojpeg.so.0
130│   │   └── mindspore-lite-java.jar
131│   ├── model
132│   │   └── lenet_tod.ms                 # model to train
133│   └── train.sh                         # on-device script that load the initial model and train it
134```
135