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 12 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. # Contents
  2. - [MobileNetV2 Description](#mobilenetv2-description)
  3. - [Model Architecture](#model-architecture)
  4. - [Dataset](#dataset)
  5. - [Features](#features)
  6. - [Mixed Precision](#mixed-precision(ascend))
  7. - [Environment Requirements](#environment-requirements)
  8. - [Script Description](#script-description)
  9. - [Script and Sample Code](#script-and-sample-code)
  10. - [Training Process](#training-process)
  11. - [Evaluation Process](#eval-process)
  12. - [Model Export](#model-export)
  13. - [Model Description](#model-description)
  14. - [Performance](#performance)
  15. - [Training Performance](#training-performance)
  16. - [Evaluation Performance](#evaluation-performance)
  17. - [Description of Random Situation](#description-of-random-situation)
  18. - [ModelZoo Homepage](#modelzoo-homepage)
  19. # [MobileNetV2 Description](#contents)
  20. MobileNetV2 is tuned to mobile phone CPUs through a combination of hardware- aware network architecture search (NAS) complemented by the NetAdapt algorithm and then subsequently improved through novel architecture advances.Nov 20, 2019.
  21. [Paper](https://arxiv.org/pdf/1905.02244) Howard, Andrew, Mark Sandler, Grace Chu, Liang-Chieh Chen, Bo Chen, Mingxing Tan, Weijun Wang et al. "Searching for MobileNetV2." In Proceedings of the IEEE International Conference on Computer Vision, pp. 1314-1324. 2019.
  22. # [Model architecture](#contents)
  23. The overall network architecture of MobileNetV2 is show below:
  24. [Link](https://arxiv.org/pdf/1905.02244)
  25. # [Dataset](#contents)
  26. Dataset used: [imagenet](http://www.image-net.org/)
  27. - Dataset size: ~125G, 1.2W colorful images in 1000 classes
  28. - Train: 120G, 1.2W images
  29. - Test: 5G, 50000 images
  30. - Data format: RGB images.
  31. - Note: Data will be processed in src/dataset.py
  32. # [Features](#contents)
  33. ## [Mixed Precision(Ascend)](#contents)
  34. The [mixed precision](https://www.mindspore.cn/tutorial/training/en/master/advanced_use/enable_mixed_precision.html) training method accelerates the deep learning neural network training process by using both the single-precision and half-precision data formats, and maintains the network precision achieved by the single-precision training at the same time. Mixed precision training can accelerate the computation process, reduce memory usage, and enable a larger model or batch size to be trained on specific hardware.
  35. For FP16 operators, if the input data type is FP32, the backend of MindSpore will automatically handle it with reduced precision. Users could check the reduced-precision operators by enabling INFO log and then searching ‘reduce precision’.
  36. # [Environment Requirements](#contents)
  37. - Hardware(Ascend/GPU/CPU)
  38. - Prepare hardware environment with Ascend, GPU or CPU processor.
  39. - Framework
  40. - [MindSpore](https://www.mindspore.cn/install/en)
  41. - For more information, please check the resources below:
  42. - [MindSpore Tutorials](https://www.mindspore.cn/tutorial/training/en/master/index.html)
  43. - [MindSpore Python API](https://www.mindspore.cn/doc/api_python/en/master/index.html)
  44. # [Script description](#contents)
  45. ## [Script and sample code](#contents)
  46. ```python
  47. ├── MobileNetV2
  48. ├── README.md # descriptions about MobileNetV2
  49. ├── scripts
  50. │ ├──run_train.sh # shell script for train, fine_tune or incremental learn with CPU, GPU or Ascend
  51. │ ├──run_eval.sh # shell script for evaluation with CPU, GPU or Ascend
  52. ├── src
  53. │ ├──args.py # parse args
  54. │ ├──config.py # parameter configuration
  55. │ ├──dataset.py # creating dataset
  56. │ ├──lr_generator.py # learning rate config
  57. │ ├──mobilenetV2.py # MobileNetV2 architecture
  58. │ ├──models.py # contain define_net and Loss, Monitor
  59. │ ├──utils.py # utils to load ckpt_file for fine tune or incremental learn
  60. ├── train.py # training script
  61. ├── eval.py # evaluation script
  62. ├── export.py # export mindir script
  63. ├── mindspore_hub_conf.py # mindspore hub interface
  64. ```
  65. ## [Training process](#contents)
  66. ### Usage
  67. You can start training using python or shell scripts. The usage of shell scripts as follows:
  68. - Ascend: sh run_train.sh Ascend [DEVICE_NUM] [VISIABLE_DEVICES(0,1,2,3,4,5,6,7)] [RANK_TABLE_FILE] [DATASET_PATH] [CKPT_PATH] [FREEZE_LAYER] [FILTER_HEAD]
  69. - GPU: sh run_trian.sh GPU [DEVICE_NUM] [VISIABLE_DEVICES(0,1,2,3,4,5,6,7)] [DATASET_PATH] [CKPT_PATH] [FREEZE_LAYER] [FILTER_HEAD]
  70. - CPU: sh run_trian.sh CPU [DATASET_PATH] [CKPT_PATH] [FREEZE_LAYER] [FILTER_HEAD]
  71. `CKPT_PATH` `FREEZE_LAYER` and `FILTER_HEAD` are optional, when set `CKPT_PATH`, `FREEZE_LAYER` must be set. `FREEZE_LAYER` should be in ["none", "backbone"], and if you set `FREEZE_LAYER`="backbone", the parameter in backbone will be freezed when training and the parameter in head will not be load from checkpoint. if `FILTER_HEAD`=True, the parameter in head will not be load from checkpoint.
  72. > RANK_TABLE_FILE is HCCL configuration file when running on Ascend.
  73. > The common restrictions on using the distributed service are as follows. For details, see the HCCL documentation.
  74. >
  75. > - In a single-node system, a cluster of 1, 2, 4, or 8 devices is supported. In a multi-node system, a cluster of 8 x N devices is supported.
  76. > - Each host has four devices numbered 0 to 3 and four devices numbered 4 to 7 deployed on two different networks. During training of 2 or 4 devices, the devices must be connected and clusters cannot be created across networks.
  77. ### Launch
  78. ```shell
  79. # training example
  80. python:
  81. Ascend: python train.py --platform Ascend --dataset_path [TRAIN_DATASET_PATH]
  82. GPU: python train.py --platform GPU --dataset_path [TRAIN_DATASET_PATH]
  83. CPU: python train.py --platform CPU --dataset_path [TRAIN_DATASET_PATH]
  84. shell:
  85. Ascend: sh run_train.sh Ascend 8 0,1,2,3,4,5,6,7 hccl_config.json [TRAIN_DATASET_PATH]
  86. GPU: sh run_train.sh GPU 8 0,1,2,3,4,5,6,7 [TRAIN_DATASET_PATH]
  87. CPU: sh run_train.sh CPU [TRAIN_DATASET_PATH]
  88. # fine tune whole network example
  89. python:
  90. Ascend: python train.py --platform Ascend --dataset_path [TRAIN_DATASET_PATH] --pretrain_ckpt [CKPT_PATH] --freeze_layer none --filter_head True
  91. GPU: python train.py --platform GPU --dataset_path [TRAIN_DATASET_PATH] --pretrain_ckpt [CKPT_PATH] --freeze_layer none --filter_head True
  92. CPU: python train.py --platform CPU --dataset_path [TRAIN_DATASET_PATH] --pretrain_ckpt [CKPT_PATH] --freeze_layer none --filter_head True
  93. shell:
  94. Ascend: sh run_train.sh Ascend 8 0,1,2,3,4,5,6,7 hccl_config.json [TRAIN_DATASET_PATH] [CKPT_PATH] none True
  95. GPU: sh run_train.sh GPU 8 0,1,2,3,4,5,6,7 [TRAIN_DATASET_PATH] [CKPT_PATH] none True
  96. CPU: sh run_train.sh CPU [TRAIN_DATASET_PATH] [CKPT_PATH] none True
  97. # fine tune full connected layers example
  98. python:
  99. Ascend: python --platform Ascend train.py --dataset_path [TRAIN_DATASET_PATH]--pretrain_ckpt [CKPT_PATH] --freeze_layer backbone
  100. GPU: python --platform GPU train.py --dataset_path [TRAIN_DATASET_PATH] --pretrain_ckpt [CKPT_PATH] --freeze_layer backbone
  101. CPU: python --platform CPU train.py --dataset_path [TRAIN_DATASET_PATH] --pretrain_ckpt [CKPT_PATH] --freeze_layer backbone
  102. shell:
  103. Ascend: sh run_train.sh Ascend 8 0,1,2,3,4,5,6,7 hccl_config.json [TRAIN_DATASET_PATH] [CKPT_PATH] backbone
  104. GPU: sh run_train.sh GPU 8 0,1,2,3,4,5,6,7 [TRAIN_DATASET_PATH] [CKPT_PATH] backbone
  105. CPU: sh run_train.sh CPU [TRAIN_DATASET_PATH] [CKPT_PATH] backbone
  106. ```
  107. ### Result
  108. Training result will be stored in the example path. Checkpoints will be stored at `. /checkpoint` by default, and training log will be redirected to `./train.log` like followings with the platform CPU and GPU, will be wrote to `./train/rank*/log*.log` with the platform Ascend .
  109. ```shell
  110. epoch: [ 0/200], step:[ 624/ 625], loss:[5.258/5.258], time:[140412.236], lr:[0.100]
  111. epoch time: 140522.500, per step time: 224.836, avg loss: 5.258
  112. epoch: [ 1/200], step:[ 624/ 625], loss:[3.917/3.917], time:[138221.250], lr:[0.200]
  113. epoch time: 138331.250, per step time: 221.330, avg loss: 3.917
  114. ```
  115. ## [Evaluation process](#contents)
  116. ### Usage
  117. You can start training using python or shell scripts.If the train method is train or fine tune, should not input the `[CHECKPOINT_PATH]` The usage of shell scripts as follows:
  118. - Ascend: sh run_eval.sh Ascend [DATASET_PATH] [CHECKPOINT_PATH]
  119. - GPU: sh run_eval.sh GPU [DATASET_PATH] [CHECKPOINT_PATH]
  120. - CPU: sh run_eval.sh CPU [DATASET_PATH] [BACKBONE_CKPT_PATH]
  121. ### Launch
  122. ```shell
  123. # eval example
  124. python:
  125. Ascend: python eval.py --platform Ascend --dataset_path [VAL_DATASET_PATH] --pretrain_ckpt ./ckpt_0/mobilenetv2_15.ckpt
  126. GPU: python eval.py --platform GPU --dataset_path [VAL_DATASET_PATH] --pretrain_ckpt ./ckpt_0/mobilenetv2_15.ckpt
  127. CPU: python eval.py --platform CPU --dataset_path [VAL_DATASET_PATH] --pretrain_ckpt ./ckpt_0/mobilenetv2_15.ckpt
  128. shell:
  129. Ascend: sh run_eval.sh Ascend [VAL_DATASET_PATH] ./checkpoint/mobilenetv2_head_15.ckpt
  130. GPU: sh run_eval.sh GPU [VAL_DATASET_PATH] ./checkpoint/mobilenetv2_head_15.ckpt
  131. CPU: sh run_eval.sh CPU [VAL_DATASET_PATH] ./checkpoint/mobilenetv2_head_15.ckpt
  132. ```
  133. > checkpoint can be produced in training process.
  134. ### Result
  135. Inference result will be stored in the example path, you can find result like the followings in `eval.log`.
  136. ```shell
  137. result: {'acc': 0.71976314102564111} ckpt=./ckpt_0/mobilenet-200_625.ckpt
  138. ```
  139. ## [Model Export](#contents)
  140. ```shell
  141. python export.py --platform [PLATFORM] --ckpt_file [CKPT_PATH] --file_format [EXPORT_FORMAT]
  142. ```
  143. `EXPORT_FORMAT` should be in ["AIR", "ONNX", "MINDIR"]
  144. # [Model description](#contents)
  145. ## [Performance](#contents)
  146. ### Training Performance
  147. | Parameters | MobilenetV2 | |
  148. | -------------------------- | ---------------------------------------------------------- | ------------------------- |
  149. | Model Version | V1 | V1 |
  150. | Resource | Ascend 910, cpu:2.60GHz 192cores, memory:755G | NV SMX2 V100-32G |
  151. | uploaded Date | 05/06/2020 | 05/06/2020 |
  152. | MindSpore Version | 0.3.0 | 0.3.0 |
  153. | Dataset | ImageNet | ImageNet |
  154. | Training Parameters | src/config.py | src/config.py |
  155. | Optimizer | Momentum | Momentum |
  156. | Loss Function | SoftmaxCrossEntropy | SoftmaxCrossEntropy |
  157. | outputs | probability | probability |
  158. | Loss | 1.908 | 1.913 |
  159. | Accuracy | ACC1[71.78%] | ACC1[71.08%] |
  160. | Total time | 753 min | 845 min |
  161. | Params (M) | 3.3 M | 3.3 M |
  162. | Checkpoint for Fine tuning | 27.3 M | 27.3 M |
  163. | Scripts | [Link](https://gitee.com/mindspore/mindspore/tree/master/model_zoo/official/cv/mobilenetv2)|
  164. # [Description of Random Situation](#contents)
  165. <!-- In dataset.py, we set the seed inside “create_dataset" function. We also use random seed in train.py. -->
  166. In train.py, we set the seed which is used by numpy.random, mindspore.common.Initializer, mindspore.ops.composite.random_ops and mindspore.nn.probability.distribution.
  167. # [ModelZoo Homepage](#contents)
  168. Please check the official [homepage](https://gitee.com/mindspore/mindspore/tree/master/model_zoo).