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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. # Contents
  2. - [DenseNet Description](#densenet-description)
  3. - [Model Architecture](#model-architecture)
  4. - [Dataset](#dataset)
  5. - [Features](#features)
  6. - [Mixed Precision](#mixed-precision)
  7. - [Environment Requirements](#environment-requirements)
  8. - [Quick Start](#quick-start)
  9. - [Script Description](#script-description)
  10. - [Script and Sample Code](#script-and-sample-code)
  11. - [Script Parameters](#script-parameters)
  12. - [Training Process](#training-process)
  13. - [Training](#training)
  14. - [Distributed Training](#distributed-training)
  15. - [Evaluation Process](#evaluation-process)
  16. - [Evaluation](#evaluation)
  17. - [Model Description](#model-description)
  18. - [Performance](#performance)
  19. - [Training accuracy results](#training-accuracy-results)
  20. - [Training performance results](#training-performance-results)
  21. - [Description of Random Situation](#description-of-random-situation)
  22. - [ModelZoo Homepage](#modelzoo-homepage)
  23. # [DenseNet Description](#contents)
  24. DenseNet is a convolution based neural network for the task of image classification. The paper describing the model can be found [here](https://arxiv.org/abs/1608.06993). HuaWei’s DenseNet is a implementation on [MindSpore](https://www.mindspore.cn/).
  25. The repository also contains scripts to launch training and inference routines.
  26. # [Model Architecture](#contents)
  27. DenseNet supports two kinds of implementations: DenseNet100 and DenseNet121, where the number represents number of layers in the network.
  28. DenseNet121 builds on 4 densely connected block and DenseNet100 builds on 3. In every dense block, each layer obtains additional inputs from all preceding layers and passes on its own feature-maps to all subsequent layers. Concatenation is used. Each layer is receiving a “collective knowledge” from all preceding layers.
  29. # [Dataset](#contents)
  30. Dataset used in DenseNet121: ImageNet
  31. The default configuration of the Dataset are as follows:
  32. - Training Dataset preprocess:
  33. - Input size of images is 224\*224
  34. - Range (min, max) of respective size of the original size to be cropped is (0.08, 1.0)
  35. - Range (min, max) of aspect ratio to be cropped is (0.75, 1.333)
  36. - Probability of the image being flipped set to 0.5
  37. - Randomly adjust the brightness, contrast, saturation (0.4, 0.4, 0.4)
  38. - Normalize the input image with respect to mean and standard deviation
  39. - Test Dataset preprocess:
  40. - Input size of images is 224\*224 (Resize to 256\*256 then crops images at the center)
  41. - Normalize the input image with respect to mean and standard deviation
  42. Dataset used in DenseNet100: Cifar-10
  43. The default configuration of the Dataset are as follows:
  44. - Training Dataset preprocess:
  45. - Input size of images is 32\*32
  46. - Randomly cropping is applied to the image with padding=4
  47. - Probability of the image being flipped set to 0.5
  48. - Randomly adjust the brightness, contrast, saturation (0.4, 0.4, 0.4)
  49. - Normalize the input image with respect to mean and standard deviation
  50. - Test Dataset preprocess:
  51. - Input size of images is 32\*32
  52. - Normalize the input image with respect to mean and standard deviation
  53. # [Features](#contents)
  54. ## Mixed Precision
  55. 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.
  56. 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’.
  57. # [Environment Requirements](#contents)
  58. - Hardware(Ascend/GPU)
  59. - Prepare hardware environment with Ascend or GPU processor.
  60. - Framework
  61. - [MindSpore](https://www.mindspore.cn/install/en)
  62. - For more information, please check the resources below:
  63. - [MindSpore Tutorials](https://www.mindspore.cn/tutorial/training/en/master/index.html)
  64. - [MindSpore Python API](https://www.mindspore.cn/doc/api_python/en/master/index.html)
  65. # [Quick Start](#contents)
  66. After installing MindSpore via the official website, you can start training and evaluation as follows:
  67. - running on Ascend
  68. ```python
  69. # run training example
  70. python train.py --net [NET_NAME] --dataset [DATASET_NAME] --data_dir /PATH/TO/DATASET --pretrained /PATH/TO/PRETRAINED_CKPT --is_distributed 0 > train.log 2>&1 &
  71. # run distributed training example
  72. sh scripts/run_distribute_train.sh 8 rank_table.json [NET_NAME] [DATASET_NAME] /PATH/TO/DATASET /PATH/TO/PRETRAINED_CKPT
  73. # run evaluation example
  74. python eval.py --net [NET_NAME] --dataset [DATASET_NAME] --data_dir /PATH/TO/DATASET --pretrained /PATH/TO/CHECKPOINT > eval.log 2>&1 &
  75. OR
  76. sh scripts/run_distribute_eval.sh 8 rank_table.json [NET_NAME] [DATASET_NAME] /PATH/TO/DATASET /PATH/TO/CHECKPOINT
  77. ```
  78. For distributed training, a hccl configuration file with JSON format needs to be created in advance.
  79. Please follow the instructions in the link below:
  80. <https://gitee.com/mindspore/mindspore/tree/master/model_zoo/utils/hccl_tools>.
  81. - running on GPU
  82. For running on GPU, please change `platform` from `Ascend` to `GPU`
  83. ```python
  84. # run training example
  85. export CUDA_VISIBLE_DEVICES=0
  86. python train.py --net=[NET_NAME] --dataset=[DATASET_NAME] --data_dir=[DATASET_PATH] --is_distributed=0 --device_target='GPU' > train.log 2>&1 &
  87. # run distributed training example
  88. sh run_distribute_train_gpu.sh 8 0,1,2,3,4,5,6,7 [NET_NAME] [DATASET_NAME] [DATASET_PATH]
  89. # run evaluation example
  90. python eval.py --net=[NET_NAME] --dataset=[DATASET_NAME] --data_dir=[DATASET_PATH] --device_target='GPU' --pretrained=[CHECKPOINT_PATH] > eval.log 2>&1 &
  91. OR
  92. sh run_distribute_eval_gpu.sh 1 0 [NET_NAME] [DATASET_NAME] [DATASET_PATH] [CHECKPOINT_PATH]
  93. ```
  94. # [Script Description](#contents)
  95. ## [Script and Sample Code](#contents)
  96. ```text
  97. ├── model_zoo
  98. ├── README.md // descriptions about all the models
  99. ├── densenet
  100. ├── README.md // descriptions about densenet
  101. ├── scripts
  102. │ ├── run_distribute_train.sh // shell script for distributed on Ascend
  103. │ ├── run_distribute_train_gpu.sh // shell script for distributed on GPU
  104. │ ├── run_distribute_eval.sh // shell script for evaluation on Ascend
  105. │ ├── run_distribute_eval_gpu.sh // shell script for evaluation on GPU
  106. ├── src
  107. │ ├── datasets // dataset processing function
  108. │ ├── losses
  109. │ ├──crossentropy.py // densenet loss function
  110. │ ├── lr_scheduler
  111. │ ├──lr_scheduler.py // densenet learning rate schedule function
  112. │ ├── network
  113. │ ├──densenet.py // densenet architecture
  114. │ ├──optimizers // densenet optimize function
  115. │ ├──utils
  116. │ ├──logging.py // logging function
  117. │ ├──var_init.py // densenet variable init function
  118. │ ├── config.py // network config
  119. ├── train.py // training script
  120. ├── eval.py // evaluation script
  121. ```
  122. ## [Script Parameters](#contents)
  123. You can modify the training behaviour through the various flags in the `train.py` script. Flags in the `train.py` script are as follows:
  124. ```python
  125. --data_dir train data dir
  126. --num_classes num of classes in dataset(default:1000 for densenet121; 10 for densenet100)
  127. --image_size image size of the dataset
  128. --per_batch_size mini-batch size (default: 32 for densenet121; 64 for densenet100) per gpu
  129. --pretrained path of pretrained model
  130. --lr_scheduler type of LR schedule: exponential, cosine_annealing
  131. --lr initial learning rate
  132. --lr_epochs epoch milestone of lr changing
  133. --lr_gamma decrease lr by a factor of exponential lr_scheduler
  134. --eta_min eta_min in cosine_annealing scheduler
  135. --T_max T_max in cosine_annealing scheduler
  136. --max_epoch max epoch num to train the model
  137. --warmup_epochs warmup epoch(when batchsize is large)
  138. --weight_decay weight decay (default: 1e-4)
  139. --momentum momentum(default: 0.9)
  140. --label_smooth whether to use label smooth in CE
  141. --label_smooth_factor smooth strength of original one-hot
  142. --log_interval logging interval(default:100)
  143. --ckpt_path path to save checkpoint
  144. --ckpt_interval the interval to save checkpoint
  145. --is_save_on_master save checkpoint on master or all rank
  146. --is_distributed if multi device(default: 1)
  147. --rank local rank of distributed(default: 0)
  148. --group_size world size of distributed(default: 1)
  149. ```
  150. ## [Training Process](#contents)
  151. ### Training
  152. - running on Ascend
  153. ```python
  154. python train.py --net [NET_NAME] --dataset [DATASET_NAME] --data_dir /PATH/TO/DATASET --pretrained /PATH/TO/PRETRAINED_CKPT --is_distributed 0 > train.log 2>&1 &
  155. ```
  156. The python command above will run in the background, The log and model checkpoint will be generated in `output/202x-xx-xx_time_xx_xx_xx/`. The loss value of training DenseNet121 on ImageNet will be achieved as follows:
  157. ```shell
  158. 2020-08-22 16:58:56,617:INFO:epoch[0], iter[5003], loss:4.367, mean_fps:0.00 imgs/sec
  159. 2020-08-22 16:58:56,619:INFO:local passed
  160. 2020-08-22 17:02:19,920:INFO:epoch[1], iter[10007], loss:3.193, mean_fps:6301.11 imgs/sec
  161. 2020-08-22 17:02:19,921:INFO:local passed
  162. 2020-08-22 17:05:43,112:INFO:epoch[2], iter[15011], loss:3.096, mean_fps:6304.53 imgs/sec
  163. 2020-08-22 17:05:43,113:INFO:local passed
  164. ...
  165. ```
  166. - running on GPU
  167. ```python
  168. export CUDA_VISIBLE_DEVICES=0
  169. python train.py --net [NET_NAME] --dataset [DATASET_NAME] --data_dir=[DATASET_PATH] --is_distributed=0 --device_target='GPU' > train.log 2>&1 &
  170. ```
  171. The python command above will run in the background, you can view the results through the file `train.log`.
  172. After training, you'll get some checkpoint files under the folder `./ckpt_0/` by default.
  173. - running on CPU
  174. ```python
  175. python train.py --net=[NET_NAME] --dataset=[DATASET_NAME] --data_dir=[DATASET_PATH] --is_distributed=0 --device_target='CPU' > train.log 2>&1 &
  176. ```
  177. The python command above will run in the background, The log and model checkpoint will be generated in `output/202x-xx-xx_time_xx_xx_xx/`.
  178. ### Distributed Training
  179. - running on Ascend
  180. ```bash
  181. sh scripts/run_distribute_train.sh 8 rank_table.json [NET_NAME] [DATASET_NAME] /PATH/TO/DATASET /PATH/TO/PRETRAINED_CKPT
  182. ```
  183. The above shell script will run distribute training in the background. You can view the results log and model checkpoint through the file `train[X]/output/202x-xx-xx_time_xx_xx_xx/`. The loss value of training DenseNet121 on ImageNet will be achieved as follows:
  184. ```log
  185. 2020-08-22 16:58:54,556:INFO:epoch[0], iter[5003], loss:3.857, mean_fps:0.00 imgs/sec
  186. 2020-08-22 17:02:19,188:INFO:epoch[1], iter[10007], loss:3.18, mean_fps:6260.18 imgs/sec
  187. 2020-08-22 17:05:42,490:INFO:epoch[2], iter[15011], loss:2.621, mean_fps:6301.11 imgs/sec
  188. 2020-08-22 17:09:05,686:INFO:epoch[3], iter[20015], loss:3.113, mean_fps:6304.37 imgs/sec
  189. 2020-08-22 17:12:28,925:INFO:epoch[4], iter[25019], loss:3.29, mean_fps:6303.07 imgs/sec
  190. 2020-08-22 17:15:52,167:INFO:epoch[5], iter[30023], loss:2.865, mean_fps:6302.98 imgs/sec
  191. ...
  192. ...
  193. ```
  194. - running on GPU
  195. ```bash
  196. cd scripts
  197. sh run_distribute_train_gpu.sh 8 0,1,2,3,4,5,6,7 [NET_NAME] [DATASET_NAME] [DATASET_PATH]
  198. ```
  199. The above shell script will run distribute training in the background. You can view the results through the file `train/train.log`.
  200. ## [Evaluation Process](#contents)
  201. ### Evaluation
  202. - evaluation on Ascend
  203. running the command below for evaluation.
  204. ```python
  205. python eval.py --net [NET_NAME] --dataset [DATASET_NAME] --data_dir /PATH/TO/DATASET --pretrained /PATH/TO/CHECKPOINT > eval.log 2>&1 &
  206. OR
  207. sh scripts/run_distribute_eval.sh 8 rank_table.json [NET_NAME] [DATASET_NAME] /PATH/TO/DATASET /PATH/TO/CHECKPOINT
  208. ```
  209. The above python command will run in the background. You can view the results through the file "output/202x-xx-xx_time_xx_xx_xx/202x_xxxx.log". The accuracy of evaluating DenseNet121 on the test dataset of ImageNet will be as follows:
  210. ```log
  211. 2020-08-24 09:21:50,551:INFO:after allreduce eval: top1_correct=37657, tot=49920, acc=75.43%
  212. 2020-08-24 09:21:50,551:INFO:after allreduce eval: top5_correct=46224, tot=49920, acc=92.60%
  213. ```
  214. - evaluation on GPU
  215. running the command below for evaluation.
  216. ```python
  217. python eval.py --net=[NET_NAME] --dataset=[DATASET_NAME] --data_dir=[DATASET_PATH] --device_target='GPU' --pretrained=[CHECKPOINT_PATH] > eval.log 2>&1 &
  218. OR
  219. sh run_distribute_eval_gpu.sh 1 0 [NET_NAME] [DATASET_NAME] [DATASET_PATH] [CHECKPOINT_PATH]
  220. ```
  221. The above python command will run in the background. You can view the results through the file "eval/eval.log". The accuracy of evaluating DenseNet121 on the test dataset of ImageNet will be as follows:
  222. ```log
  223. 2021-02-04 14:20:50,551:INFO:after allreduce eval: top1_correct=37637, tot=49984, acc=75.30%
  224. 2021-02-04 14:20:50,551:INFO:after allreduce eval: top5_correct=46370, tot=49984, acc=92.77%
  225. ```
  226. The accuracy of evaluating DenseNet100 on the test dataset of Cifar-10 will be as follows:
  227. ```log
  228. 2021-03-12 18:04:07,893:INFO:after allreduce eval: top1_correct=9536, tot=9984, acc=95.51%
  229. ```
  230. - evaluation on CPU
  231. running the command below for evaluation.
  232. ```python
  233. python eval.py --net=[NET_NAME] --dataset=[DATASET_NAME] --data_dir=[DATASET_PATH] --device_target='CPU' --pretrained=[CHECKPOINT_PATH] > eval.log 2>&1 &
  234. ```
  235. The above python command will run in the background. You can view the results through the file "eval/eval.log". The accuracy of evaluating DenseNet100 on the test dataset of Cifar-10 will be as follows:
  236. ```log
  237. 2021-03-18 09:06:43,247:INFO:after allreduce eval: top1_correct=9492, tot=9984, acc=95.07%
  238. ```
  239. # [Model Description](#contents)
  240. ## [Performance](#contents)
  241. ### DenseNet121
  242. ### Training accuracy results
  243. | Parameters | Ascend | GPU |
  244. | ------------------- | --------------------------- | --------------------------- |
  245. | Model Version | DenseNet121 | DenseNet121 |
  246. | Resource | Ascend 910; OS Euler2.8 | Tesla V100-PCIE |
  247. | Uploaded Date | 09/15/2020 (month/day/year) | 01/27/2021 (month/day/year) |
  248. | MindSpore Version | 1.0.0 | 1.1.0 |
  249. | Dataset | ImageNet | ImageNet |
  250. | epochs | 120 | 120 |
  251. | outputs | probability | probability |
  252. | accuracy | Top1:75.13%; Top5:92.57% | Top1:75.30%; Top5:92.77% |
  253. ### Training performance results
  254. | Parameters | Ascend | GPU |
  255. | ------------------- | --------------------------- | ---------------------------- |
  256. | Model Version | DenseNet121 | DenseNet121 |
  257. | Resource | Ascend 910; OS Euler2.8 | Tesla V100-PCIE |
  258. | Uploaded Date | 09/15/2020 (month/day/year) | 02/04/2021 (month/day/year) |
  259. | MindSpore Version | 1.0.0 | 1.1.1 |
  260. | Dataset | ImageNet | ImageNet |
  261. | batch_size | 32 | 32 |
  262. | outputs | probability | probability |
  263. | speed | 1pc:760 img/s;8pc:6000 img/s| 1pc:161 img/s;8pc:1288 img/s |
  264. ### DenseNet100
  265. ### Training performance
  266. | Parameters | GPU |
  267. | ------------------- | ---------------------------- |
  268. | Model Version | DenseNet100 |
  269. | Resource | Tesla V100-PCIE |
  270. | Uploaded Date | 03/18/2021 (month/day/year) |
  271. | MindSpore Version | 1.2.0 |
  272. | Dataset | Cifar-10 |
  273. | batch_size | 64 |
  274. | epochs | 300 |
  275. | outputs | probability |
  276. | accuracy | 95.31% |
  277. | speed | 1pc: 600.07 img/sec |
  278. # [Description of Random Situation](#contents)
  279. In dataset.py, we set the seed inside “create_dataset" function. We also use random seed in train.py.
  280. # [ModelZoo Homepage](#contents)
  281. Please check the official [homepage](https://gitee.com/mindspore/mindspore/tree/master/model_zoo).