You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 6.9 kB

5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. # Content
  2. <!-- TOC -->
  3. - [Overview](#overview)
  4. - [Model Architecture](#model-architecture)
  5. - [Dataset](#dataset)
  6. - [Environment Requirements](#environment-requirements)
  7. - [Quick Start](#quick-start)
  8. - [Script Detailed Description](#script-detailed-description)
  9. <!-- /TOC -->
  10. # Overview
  11. This 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.
  12. # Model Architecture
  13. LeNet 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.
  14. # Dataset
  15. In this example we use the MNIST dataset of handwritten digits as published in [THE MNIST DATABASE](http://yann.lecun.com/exdb/mnist/)
  16. - Dataset size:52.4M,60,000 28*28 in 10 classes
  17. - Test:10,000 images
  18. - Train:60,000 images
  19. - Data format:binary files
  20. - Note:Data will be processed in dataset.cc
  21. - The dataset directory structure is as follows:
  22. ```text
  23. mnist/
  24. ├── test
  25. │   ├── t10k-images-idx3-ubyte
  26. │   └── t10k-labels-idx1-ubyte
  27. └── train
  28. ├── train-images-idx3-ubyte
  29. └── train-labels-idx1-ubyte
  30. ```
  31. # Environment Requirements
  32. - Server side
  33. - [MindSpore Framework](https://www.mindspore.cn/install/en): it is recommended to install a docker image
  34. - MindSpore ToD Framework
  35. - [Downloads](https://www.mindspore.cn/lite/docs/en/master/use/downloads.html)
  36. - [Build](https://www.mindspore.cn/lite/docs/en/master/use/build.html)
  37. - [Android NDK r20b](https://dl.google.com/android/repository/android-ndk-r20b-linux-x86_64.zip)
  38. - [Android SDK](https://developer.android.com/studio?hl=zh-cn#cmdline-tools)
  39. - A connected Android device
  40. # Quick Start
  41. After installing all the above mentioned, the script in the home directory could be run with the following arguments:
  42. ```bash
  43. sh ./prepare_and_run.sh -D DATASET_PATH [-d MINDSPORE_DOCKER] [-r RELEASE.tar.gz] [-t arm64|x86]
  44. ```
  45. where:
  46. - DATASET_PATH is the path to the [dataset](#dataset),
  47. - MINDSPORE_DOCKER is the image name of the docker that runs [MindSpore](#environment-requirements). If not provided MindSpore will be run locally
  48. - 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
  49. - 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.
  50. # Script Detailed Description
  51. The provided `prepare_and_run.sh` script is performing the following:
  52. - Prepare the trainable lenet model in a `.ms` format
  53. - Prepare the folder that should be pushed into the device
  54. - Copy this folder into the device and run the scripts on the device
  55. See how to run the script and parameters definitions in the [Quick Start Section](#quick-start)
  56. ## Preparing the model
  57. Within 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.
  58. The script then converts the `.mindir` to a `.ms` format using the MindSpore ToD converter.
  59. The 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.
  60. ## Preparing the Folder
  61. The `lenet_tod.ms` model file is then copied into the `package` folder as well as scripts, the MindSpore ToD library and the MNIST dataset.
  62. Finally, the code (in src) is compiled for arm64 and the binary is copied into the `package` folder.
  63. ### Running the code on the device
  64. To 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.
  65. # Folder Directory tree
  66. ``` python
  67. train_lenet/
  68. ├── Makefile # Makefile of src code
  69. ├── model
  70. │   ├── lenet_export.py # Python script that exports the LeNet model to .mindir
  71. │   ├── prepare_model.sh # script that export model (using docker) then converts it
  72. │   └── train_utils.py # utility function used during the export
  73. ├── prepare_and_run.sh # main script that creates model, compiles it and send to device for running
  74. ├── README.md # English manual
  75. ├── README_CN.md # Chinese manual
  76. ├── scripts
  77. │   ├── eval.sh # on-device script that load the train model and evaluates its accuracy
  78. │   └── train.sh # on-device script that load the initial model and train it
  79. ├── src
  80. │   ├── net_runner.cc # program that runs training/evaluation of models
  81. │   ├── net_runner.h # net_runner header
  82. │   └── utils.h # general utilities
  83. ```
  84. When the `prepare_and_run.sh` script is run, the following folder is prepared. It is pushed to the device and then training runs
  85. ``` python
  86. ├── package
  87. │   ├── bin
  88. │   │   └── net_runner # the executable that performs the training/evaluation
  89. │   ├── dataset
  90. │   │   ├── test
  91. │   │   │   ├── t10k-images-idx3-ubyte # test images
  92. │   │   │   └── t10k-labels-idx1-ubyte # test labels
  93. │   │   └── train
  94. │   │   ├── train-images-idx3-ubyte # train images
  95. │   │   └── train-labels-idx1-ubyte # train labels
  96. │   ├── eval.sh # on-device script that load the train model and evaluates its accuracy
  97. │   ├── lib
  98. │   │   ├── libjpeg.so.62
  99. │   │   ├── libminddata-lite.a
  100. │   │   ├── libminddata-lite.so
  101. │   │   ├── libmindspore-lite.a
  102. │   │   ├── libmindspore-lite-jni.so
  103. │   │   ├── libmindspore-lite.so
  104. │   │   ├── libmindspore-lite-train.a
  105. │   │   ├── libmindspore-lite-train-jni.so
  106. │   │   ├── libmindspore-lite-train.so
  107. │   │   ├── libturbojpeg.so.0
  108. │   │   └── mindspore-lite-java.jar
  109. │   ├── model
  110. │   │   └── lenet_tod.ms # model to train
  111. │   └── train.sh # on-device script that load the initial model and train it
  112. ```