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_CN.md 5.4 kB

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