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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. # Contents
  2. - [DeepLabV3 Description](#DeepLabV3-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. - [Evaluation Process](#evaluation-process)
  14. - [Model Description](#model-description)
  15. - [Performance](#performance)
  16. - [Evaluation Performance](#evaluation-performance)
  17. - [Description of Random Situation](#description-of-random-situation)
  18. - [ModelZoo Homepage](#modelzoo-homepage)
  19. # [DeepLabV3 Description](#contents)
  20. ## Description
  21. DeepLab is a series of image semantic segmentation models, DeepLabV3 improves significantly over previous versions. Two keypoints of DeepLabV3: Its multi-grid atrous convolution makes it better to deal with segmenting objects at multiple scales, and augmented ASPP makes image-level features available to capture long range information.
  22. This repository provides a script and recipe to DeepLabV3 model and achieve state-of-the-art performance.
  23. Refer to [this paper][1] for network details.
  24. `Chen L C, Papandreou G, Schroff F, et al. Rethinking atrous convolution for semantic image segmentation[J]. arXiv preprint arXiv:1706.05587, 2017.`
  25. [1]: https://arxiv.org/abs/1706.05587
  26. # [Model Architecture](#contents)
  27. Resnet101 as backbone, atrous convolution for dense feature extraction.
  28. # [Dataset](#contents)
  29. Pascal VOC datasets and Semantic Boundaries Dataset
  30. - Download segmentation dataset.
  31. - Prepare the training data list file. The list file saves the relative path to image and annotation pairs. Lines are like:
  32. ```shell
  33. JPEGImages/00001.jpg SegmentationClassGray/00001.png
  34. JPEGImages/00002.jpg SegmentationClassGray/00002.png
  35. JPEGImages/00003.jpg SegmentationClassGray/00003.png
  36. JPEGImages/00004.jpg SegmentationClassGray/00004.png
  37. ......
  38. ```
  39. - Configure and run build_data.sh to convert dataset to mindrecords. Arguments in scripts/build_data.sh:
  40. ```shell
  41. --data_root root path of training data
  42. --data_lst list of training data(prepared above)
  43. --dst_path where mindrecords are saved
  44. --num_shards number of shards of the mindrecords
  45. --shuffle shuffle or not
  46. ```
  47. # [Features](#contents)
  48. ## Mixed Precision
  49. The [mixed precision](https://www.mindspore.cn/tutorial/training/zh-CN/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 types, 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.
  50. 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’.
  51. # [Environment Requirements](#contents)
  52. - Hardware(Ascend)
  53. - Prepare hardware environment with Ascend. If you want to try Ascend , please send the [application form](https://obs-9be7.obs.cn-east-2.myhuaweicloud.com/file/other/Ascend%20Model%20Zoo%E4%BD%93%E9%AA%8C%E8%B5%84%E6%BA%90%E7%94%B3%E8%AF%B7%E8%A1%A8.docx) to ascend@huawei.com. Once approved, you can get the resources.
  54. - Framework
  55. - [MindSpore](https://www.mindspore.cn/install/en)
  56. - For more information, please check the resources below:
  57. - [MindSpore Tutorials](https://www.mindspore.cn/tutorial/training/zh-CN/master/index.html)
  58. - [MindSpore Python API](https://www.mindspore.cn/doc/api_python/zh-CN/master/index.html)
  59. - Install python packages in requirements.txt
  60. - Generate config json file for 8pcs training
  61. ```
  62. # From the root of this project
  63. cd src/tools/
  64. python3 get_multicards_json.py 10.111.*.*
  65. # 10.111.*.* is the computer's ip address.
  66. ```
  67. # [Quick Start](#contents)
  68. After installing MindSpore via the official website, you can start training and evaluation as follows:
  69. - Running on Ascend
  70. Based on original DeepLabV3 paper, we reproduce two training experiments on vocaug (also as trainaug) dataset and evaluate on voc val dataset.
  71. For single device training, please config parameters, training script is:
  72. ```shell
  73. run_standalone_train.sh
  74. ```
  75. For 8 devices training, training steps are as follows:
  76. 1. Train s16 with vocaug dataset, finetuning from resnet101 pretrained model, script is:
  77. ```shell
  78. run_distribute_train_s16_r1.sh
  79. ```
  80. 2. Train s8 with vocaug dataset, finetuning from model in previous step, training script is:
  81. ```shell
  82. run_distribute_train_s8_r1.sh
  83. ```
  84. 3. Train s8 with voctrain dataset, finetuning from model in pervious step, training script is:
  85. ```shell
  86. run_distribute_train_s8_r2.sh
  87. ```
  88. For evaluation, evaluating steps are as follows:
  89. 1. Eval s16 with voc val dataset, eval script is:
  90. ```shell
  91. run_eval_s16.sh
  92. ```
  93. 2. Eval s8 with voc val dataset, eval script is:
  94. ```shell
  95. run_eval_s8.sh
  96. ```
  97. 3. Eval s8 multiscale with voc val dataset, eval script is:
  98. ```shell
  99. run_eval_s8_multiscale.sh
  100. ```
  101. 4. Eval s8 multiscale and flip with voc val dataset, eval script is:
  102. ```shell
  103. run_eval_s8_multiscale_flip.sh
  104. ```
  105. # [Script Description](#contents)
  106. ## [Script and Sample Code](#contents)
  107. ```shell
  108. .
  109. └──deeplabv3
  110. ├── README.md
  111. ├── scripts
  112. ├── build_data.sh # convert raw data to mindrecord dataset
  113. ├── run_distribute_train_s16_r1.sh # launch ascend distributed training(8 pcs) with vocaug dataset in s16 structure
  114. ├── run_distribute_train_s8_r1.sh # launch ascend distributed training(8 pcs) with vocaug dataset in s8 structure
  115. ├── run_distribute_train_s8_r2.sh # launch ascend distributed training(8 pcs) with voctrain dataset in s8 structure
  116. ├── run_eval_s16.sh # launch ascend evaluation in s16 structure
  117. ├── run_eval_s8.sh # launch ascend evaluation in s8 structure
  118. ├── run_eval_s8_multiscale.sh # launch ascend evaluation with multiscale in s8 structure
  119. ├── run_eval_s8_multiscale_filp.sh # launch ascend evaluation with multiscale and filp in s8 structure
  120. ├── run_standalone_train.sh # launch ascend standalone training(1 pc)
  121. ├── src
  122. ├── data
  123. ├── dataset.py # mindrecord data generator
  124. ├── build_seg_data.py # data preprocessing
  125. ├── loss
  126. ├── loss.py # loss definition for deeplabv3
  127. ├── nets
  128. ├── deeplab_v3
  129. ├── deeplab_v3.py # DeepLabV3 network structure
  130. ├── net_factory.py # set S16 and S8 structures
  131. ├── tools
  132. ├── get_multicards_json.py # get rank table file
  133. └── utils
  134. └── learning_rates.py # generate learning rate
  135. ├── eval.py # eval net
  136. ├── train.py # train net
  137. └── requirements.txt # requirements file
  138. ```
  139. ## [Script Parameters](#contents)
  140. Default configuration
  141. ```shell
  142. "data_file":"/PATH/TO/MINDRECORD_NAME" # dataset path
  143. "train_epochs":300 # total epochs
  144. "batch_size":32 # batch size of input tensor
  145. "crop_size":513 # crop size
  146. "base_lr":0.08 # initial learning rate
  147. "lr_type":cos # decay mode for generating learning rate
  148. "min_scale":0.5 # minimum scale of data argumentation
  149. "max_scale":2.0 # maximum scale of data argumentation
  150. "ignore_label":255 # ignore label
  151. "num_classes":21 # number of classes
  152. "model":deeplab_v3_s16 # select model
  153. "ckpt_pre_trained":"/PATH/TO/PRETRAIN_MODEL" # path to load pretrain checkpoint
  154. "is_distributed": # distributed training, it will be True if the parameter is set
  155. "save_steps":410 # steps interval for saving
  156. "keep_checkpoint_max":200 # max checkpoint for saving
  157. ```
  158. ## [Training Process](#contents)
  159. ### Usage
  160. #### Running on Ascend
  161. Based on original DeepLabV3 paper, we reproduce two training experiments on vocaug (also as trainaug) dataset and evaluate on voc val dataset.
  162. For single device training, please config parameters, training script is as follows:
  163. ```shell
  164. # run_standalone_train.sh
  165. python ${train_code_path}/train.py --data_file=/PATH/TO/MINDRECORD_NAME \
  166. --train_dir=${train_path}/ckpt \
  167. --train_epochs=200 \
  168. --batch_size=32 \
  169. --crop_size=513 \
  170. --base_lr=0.015 \
  171. --lr_type=cos \
  172. --min_scale=0.5 \
  173. --max_scale=2.0 \
  174. --ignore_label=255 \
  175. --num_classes=21 \
  176. --model=deeplab_v3_s16 \
  177. --ckpt_pre_trained=/PATH/TO/PRETRAIN_MODEL \
  178. --save_steps=1500 \
  179. --keep_checkpoint_max=200 >log 2>&1 &
  180. ```
  181. For 8 devices training, training steps are as follows:
  182. 1. Train s16 with vocaug dataset, finetuning from resnet101 pretrained model, script is as follows:
  183. ```python
  184. # run_distribute_train_s16_r1.sh
  185. for((i=0;i<=$RANK_SIZE-1;i++));
  186. do
  187. export RANK_ID=${i}
  188. export DEVICE_ID=$((i + RANK_START_ID))
  189. echo 'start rank='${i}', device id='${DEVICE_ID}'...'
  190. mkdir ${train_path}/device${DEVICE_ID}
  191. cd ${train_path}/device${DEVICE_ID} || exit
  192. python ${train_code_path}/train.py --train_dir=${train_path}/ckpt \
  193. --data_file=/PATH/TO/MINDRECORD_NAME \
  194. --train_epochs=300 \
  195. --batch_size=32 \
  196. --crop_size=513 \
  197. --base_lr=0.08 \
  198. --lr_type=cos \
  199. --min_scale=0.5 \
  200. --max_scale=2.0 \
  201. --ignore_label=255 \
  202. --num_classes=21 \
  203. --model=deeplab_v3_s16 \
  204. --ckpt_pre_trained=/PATH/TO/PRETRAIN_MODEL \
  205. --is_distributed \
  206. --save_steps=410 \
  207. --keep_checkpoint_max=200 >log 2>&1 &
  208. done
  209. ```
  210. 2. Train s8 with vocaug dataset, finetuning from model in previous step, training script is as follows:
  211. ```shell
  212. # run_distribute_train_s8_r1.sh
  213. for((i=0;i<=$RANK_SIZE-1;i++));
  214. do
  215. export RANK_ID=${i}
  216. export DEVICE_ID=$((i + RANK_START_ID))
  217. echo 'start rank='${i}', device id='${DEVICE_ID}'...'
  218. mkdir ${train_path}/device${DEVICE_ID}
  219. cd ${train_path}/device${DEVICE_ID} || exit
  220. python ${train_code_path}/train.py --train_dir=${train_path}/ckpt \
  221. --data_file=/PATH/TO/MINDRECORD_NAME \
  222. --train_epochs=800 \
  223. --batch_size=16 \
  224. --crop_size=513 \
  225. --base_lr=0.02 \
  226. --lr_type=cos \
  227. --min_scale=0.5 \
  228. --max_scale=2.0 \
  229. --ignore_label=255 \
  230. --num_classes=21 \
  231. --model=deeplab_v3_s8 \
  232. --loss_scale=2048 \
  233. --ckpt_pre_trained=/PATH/TO/PRETRAIN_MODEL \
  234. --is_distributed \
  235. --save_steps=820 \
  236. --keep_checkpoint_max=200 >log 2>&1 &
  237. done
  238. ```
  239. 3. Train s8 with voctrain dataset, finetuning from model in pervious step, training script is as follows:
  240. ```shell
  241. # run_distribute_train_s8_r2.sh
  242. for((i=0;i<=$RANK_SIZE-1;i++));
  243. do
  244. export RANK_ID=${i}
  245. export DEVICE_ID=$((i + RANK_START_ID))
  246. echo 'start rank='${i}', device id='${DEVICE_ID}'...'
  247. mkdir ${train_path}/device${DEVICE_ID}
  248. cd ${train_path}/device${DEVICE_ID} || exit
  249. python ${train_code_path}/train.py --train_dir=${train_path}/ckpt \
  250. --data_file=/PATH/TO/MINDRECORD_NAME \
  251. --train_epochs=300 \
  252. --batch_size=16 \
  253. --crop_size=513 \
  254. --base_lr=0.008 \
  255. --lr_type=cos \
  256. --min_scale=0.5 \
  257. --max_scale=2.0 \
  258. --ignore_label=255 \
  259. --num_classes=21 \
  260. --model=deeplab_v3_s8 \
  261. --loss_scale=2048 \
  262. --ckpt_pre_trained=/PATH/TO/PRETRAIN_MODEL \
  263. --is_distributed \
  264. --save_steps=110 \
  265. --keep_checkpoint_max=200 >log 2>&1 &
  266. done
  267. ```
  268. ### Result
  269. - Training vocaug in s16 structure
  270. ```shell
  271. # distribute training result(8p)
  272. epoch: 1 step: 41, loss is 0.8319108
  273. epoch time: 213856.477 ms, per step time: 5216.012 ms
  274. epoch: 2 step: 41, loss is 0.46052963
  275. epoch time: 21233.183 ms, per step time: 517.883 ms
  276. epoch: 3 step: 41, loss is 0.45012417
  277. epoch time: 21231.951 ms, per step time: 517.852 ms
  278. epoch: 4 step: 41, loss is 0.30687785
  279. epoch time: 21199.911 ms, per step time: 517.071 ms
  280. epoch: 5 step: 41, loss is 0.22769661
  281. epoch time: 21240.281 ms, per step time: 518.056 ms
  282. epoch: 6 step: 41, loss is 0.25470978
  283. ...
  284. ```
  285. - Training vocaug in s8 structure
  286. ```shell
  287. # distribute training result(8p)
  288. epoch: 1 step: 82, loss is 0.024167
  289. epoch time: 322663.456 ms, per step time: 3934.920 ms
  290. epoch: 2 step: 82, loss is 0.019832281
  291. epoch time: 43107.238 ms, per step time: 525.698 ms
  292. epoch: 3 step: 82, loss is 0.021008959
  293. epoch time: 43109.519 ms, per step time: 525.726 ms
  294. epoch: 4 step: 82, loss is 0.01912349
  295. epoch time: 43177.287 ms, per step time: 526.552 ms
  296. epoch: 5 step: 82, loss is 0.022886964
  297. epoch time: 43095.915 ms, per step time: 525.560 ms
  298. epoch: 6 step: 82, loss is 0.018708453
  299. epoch time: 43107.458 ms per step time: 525.701 ms
  300. ...
  301. ```
  302. - Training voctrain in s8 structure
  303. ```shell
  304. # distribute training result(8p)
  305. epoch: 1 step: 11, loss is 0.00554624
  306. epoch time: 199412.913 ms, per step time: 18128.447 ms
  307. epoch: 2 step: 11, loss is 0.007181881
  308. epoch time: 6119.375 ms, per step time: 556.307 ms
  309. epoch: 3 step: 11, loss is 0.004980865
  310. epoch time: 5996.978 ms, per step time: 545.180 ms
  311. epoch: 4 step: 11, loss is 0.0047651967
  312. epoch time: 5987.412 ms, per step time: 544.310 ms
  313. epoch: 5 step: 11, loss is 0.006262637
  314. epoch time: 5956.682 ms, per step time: 541.517 ms
  315. epoch: 6 step: 11, loss is 0.0060750707
  316. epoch time: 5962.164 ms, per step time: 542.015 ms
  317. ...
  318. ```
  319. ## [Evaluation Process](#contents)
  320. ### Usage
  321. #### Running on Ascend
  322. Configure checkpoint with --ckpt_path and dataset path. Then run script, mIOU will be printed in eval_path/eval_log.
  323. ```shell
  324. ./run_eval_s16.sh # test s16
  325. ./run_eval_s8.sh # test s8
  326. ./run_eval_s8_multiscale.sh # test s8 + multiscale
  327. ./run_eval_s8_multiscale_flip.sh # test s8 + multiscale + flip
  328. ```
  329. Example of test script is as follows:
  330. ```shell
  331. python ${train_code_path}/eval.py --data_root=/PATH/TO/DATA \
  332. --data_lst=/PATH/TO/DATA_lst.txt \
  333. --batch_size=16 \
  334. --crop_size=513 \
  335. --ignore_label=255 \
  336. --num_classes=21 \
  337. --model=deeplab_v3_s8 \
  338. --scales=0.5 \
  339. --scales=0.75 \
  340. --scales=1.0 \
  341. --scales=1.25 \
  342. --scales=1.75 \
  343. --flip \
  344. --freeze_bn \
  345. --ckpt_path=/PATH/TO/PRETRAIN_MODEL >${eval_path}/eval_log 2>&1 &
  346. ```
  347. ### Result
  348. Our result were obtained by running the applicable training script. To achieve the same results, follow the steps in the Quick Start Guide.
  349. #### Training accuracy
  350. | **Network** | OS=16 | OS=8 | MS | Flip | mIOU | mIOU in paper |
  351. | :----------: | :-----: | :----: | :----: | :-----: | :-----: | :-------------: |
  352. | deeplab_v3 | √ | | | | 77.37 | 77.21 |
  353. | deeplab_v3 | | √ | | | 78.84 | 78.51 |
  354. | deeplab_v3 | | √ | √ | | 79.70 |79.45 |
  355. | deeplab_v3 | | √ | √ | √ | 79.89 | 79.77 |
  356. Note: There OS is output stride, and MS is multiscale.
  357. # [Model Description](#contents)
  358. ## [Performance](#contents
  359. ### Evaluation Performance
  360. | Parameters | Ascend 910
  361. | -------------------------- | -------------------------------------- |
  362. | Model Version | DeepLabV3
  363. | Resource | Ascend 910 |
  364. | Uploaded Date | 09/04/2020 (month/day/year) |
  365. | MindSpore Version | 0.7.0-alpha |
  366. | Dataset | PASCAL VOC2012 + SBD |
  367. | Training Parameters | epoch = 300, batch_size = 32 (s16_r1) <br> epoch = 800, batch_size = 16 (s8_r1) <br> epoch = 300, batch_size = 16 (s8_r2) |
  368. | Optimizer | Momentum |
  369. | Loss Function | Softmax Cross Entropy |
  370. | Outputs | probability |
  371. | Loss | 0.0065883575 |
  372. | Speed | 60 ms/step(1pc, s16)<br> 480 ms/step(8pcs, s16) <br> 244 ms/step (8pcs, s8) |
  373. | Total time | 8pcs: 706 mins |
  374. | Parameters (M) | 58.2 |
  375. | Checkpoint for Fine tuning | 443M (.ckpt file) |
  376. | Model for inference | 223M (.air file) |
  377. | Scripts | [Link](https://gitee.com/mindspore/mindspore/tree/master/model_zoo/official/cv/deeplabv3) |
  378. ## Inference Performance
  379. | Parameters | Ascend |
  380. | ------------------- | --------------------------- |
  381. | Model Version | DeepLabV3 V1 |
  382. | Resource | Ascend 910 |
  383. | Uploaded Date | 09/04/2020 (month/day/year) |
  384. | MindSpore Version | 0.7.0-alpha |
  385. | Dataset | VOC datasets |
  386. | batch_size | 32 (s16); 16 (s8) |
  387. | outputs | probability |
  388. | Accuracy | 8pcs: <br> s16: 77.37 <br> s8: 78.84% <br> s8_multiscale: 79.70% <br> s8_Flip: 79.89% |
  389. | Model for inference | 443M (.ckpt file) |
  390. # [Description of Random Situation](#contents)
  391. In dataset.py, we set the seed inside "create_dataset" function. We also use random seed in train.py.
  392. # [ModelZoo Homepage](#contents)
  393. Please check the official [homepage](https://gitee.com/mindspore/mindspore/tree/master/model_zoo).