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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. # Contents
  2. - [YOLOv3-DarkNet53 Description](#yolov3-darknet53-description)
  3. - [Model Architecture](#model-architecture)
  4. - [Dataset](#dataset)
  5. - [Environment Requirements](#environment-requirements)
  6. - [Quick Start](#quick-start)
  7. - [Script Description](#script-description)
  8. - [Script and Sample Code](#script-and-sample-code)
  9. - [Script Parameters](#script-parameters)
  10. - [Training Process](#training-process)
  11. - [Training](#training)
  12. - [Distributed Training](#distributed-training)
  13. - [Evaluation Process](#evaluation-process)
  14. - [Evaluation](#evaluation)
  15. - [Model Description](#model-description)
  16. - [Performance](#performance)
  17. - [Evaluation Performance](#evaluation-performance)
  18. - [Inference Performance](#evaluation-performance)
  19. - [Description of Random Situation](#description-of-random-situation)
  20. - [ModelZoo Homepage](#modelzoo-homepage)
  21. # [YOLOv3-DarkNet53 Description](#contents)
  22. You only look once (YOLO) is a state-of-the-art, real-time object detection system. YOLOv3 is extremely fast and accurate.
  23. Prior detection systems repurpose classifiers or localizers to perform detection. They apply the model to an image at multiple locations and scales. High scoring regions of the image are considered detections.
  24. YOLOv3 use a totally different approach. It apply a single neural network to the full image. This network divides the image into regions and predicts bounding boxes and probabilities for each region. These bounding boxes are weighted by the predicted probabilities.
  25. YOLOv3 uses a few tricks to improve training and increase performance, including: multi-scale predictions, a better backbone classifier, and more. The full details are in the paper!
  26. [Paper](https://pjreddie.com/media/files/papers/YOLOv3.pdf): YOLOv3: An Incremental Improvement. Joseph Redmon, Ali Farhadi,
  27. University of Washington
  28. # [Model Architecture](#contents)
  29. YOLOv3 use DarkNet53 for performing feature extraction, which is a hybrid approach between the network used in YOLOv2, Darknet-19, and that newfangled residual network stuff. DarkNet53 uses successive 3 × 3 and 1 × 1 convolutional layers and has some shortcut connections as well and is significantly larger. It has 53 convolutional layers.
  30. # [Dataset](#contents)
  31. Note that you can run the scripts based on the dataset mentioned in original paper or widely used in relevant domain/network architecture. In the following sections, we will introduce how to run the scripts using the related dataset below.
  32. Dataset used: [COCO2014](https://cocodataset.org/#download)
  33. - Dataset size: 19G, 123,287 images, 80 object categories.
  34. - Train:13G, 82,783 images
  35. - Val:6GM, 40,504 images
  36. - Annotations: 241M, Train/Val annotations
  37. - Data format:zip files
  38. - Note:Data will be processed in yolo_dataset.py, and unzip files before uses it.
  39. # [Environment Requirements](#contents)
  40. - Hardware(Ascend/GPU)
  41. - Prepare hardware environment with Ascend or GPU processor. 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.
  42. - Framework
  43. - [MindSpore](https://www.mindspore.cn/install/en)
  44. - For more information, please check the resources below:
  45. - [MindSpore Tutorials](https://www.mindspore.cn/tutorial/training/en/master/index.html)
  46. - [MindSpore Python API](https://www.mindspore.cn/doc/api_python/en/master/index.html)
  47. # [Quick Start](#contents)
  48. After installing MindSpore via the official website, you can start training and evaluation in as follows. If running on GPU, please add `--device_target=GPU` in the python command or use the "_gpu" shell script ("xxx_gpu.sh").
  49. ```
  50. # The darknet53_backbone.ckpt in the follow script is got from darknet53 training like paper.
  51. # pretrained_backbone can use src/convert_weight.py, convert darknet53.conv.74 to mindspore ckpt, darknet53.conv.74 can get from `https://pjreddie.com/media/files/darknet53.conv.74` .
  52. # The parameter of training_shape define image shape for network, default is "".
  53. # It means use 10 kinds of shape as input shape, or it can be set some kind of shape.
  54. # run training example(1p) by python command.
  55. python train.py \
  56. --data_dir=./dataset/coco2014 \
  57. --pretrained_backbone=darknet53_backbone.ckpt \
  58. --is_distributed=0 \
  59. --lr=0.1 \
  60. --T_max=320 \
  61. --max_epoch=320 \
  62. --warmup_epochs=4 \
  63. --training_shape=416 \
  64. --lr_scheduler=cosine_annealing > log.txt 2>&1 &
  65. # standalone training example(1p) by shell script
  66. sh run_standalone_train.sh dataset/coco2014 darknet53_backbone.ckpt
  67. # For Ascend device, distributed training example(8p) by shell script
  68. sh run_distribute_train.sh dataset/coco2014 darknet53_backbone.ckpt rank_table_8p.json
  69. # For GPU device, distributed training example(8p) by shell script
  70. sh run_distribute_train_gpu.sh dataset/coco2014 darknet53_backbone.ckpt
  71. # run evaluation by python command
  72. python eval.py \
  73. --data_dir=./dataset/coco2014 \
  74. --pretrained=yolov3.ckpt \
  75. --testing_shape=416 > log.txt 2>&1 &
  76. # run evaluation by shell script
  77. sh run_eval.sh dataset/coco2014/ checkpoint/0-319_102400.ckpt
  78. ```
  79. # [Script Description](#contents)
  80. ## [Script and Sample Code](#contents)
  81. ```
  82. .
  83. └─yolov3_darknet53
  84. ├─README.md
  85. ├─mindspore_hub_conf.md # config for mindspore hub
  86. ├─scripts
  87. ├─run_standalone_train.sh # launch standalone training(1p) in ascend
  88. ├─run_distribute_train.sh # launch distributed training(8p) in ascend
  89. └─run_eval.sh # launch evaluating in ascend
  90. ├─run_standalone_train_gpu.sh # launch standalone training(1p) in gpu
  91. ├─run_distribute_train_gpu.sh # launch distributed training(8p) in gpu
  92. └─run_eval_gpu.sh # launch evaluating in gpu
  93. ├─src
  94. ├─__init__.py # python init file
  95. ├─config.py # parameter configuration
  96. ├─darknet.py # backbone of network
  97. ├─distributed_sampler.py # iterator of dataset
  98. ├─initializer.py # initializer of parameters
  99. ├─logger.py # log function
  100. ├─loss.py # loss function
  101. ├─lr_scheduler.py # generate learning rate
  102. ├─transforms.py # Preprocess data
  103. ├─util.py # util function
  104. ├─yolo.py # yolov3 network
  105. ├─yolo_dataset.py # create dataset for YOLOV3
  106. ├─eval.py # eval net
  107. └─train.py # train net
  108. ```
  109. ## [Script Parameters](#contents)
  110. ```
  111. Major parameters in train.py as follow.
  112. optional arguments:
  113. -h, --help show this help message and exit
  114. --device_target device where the code will be implemented: "Ascend" | "GPU", default is "Ascend"
  115. --data_dir DATA_DIR Train dataset directory.
  116. --per_batch_size PER_BATCH_SIZE
  117. Batch size for Training. Default: 32.
  118. --pretrained_backbone PRETRAINED_BACKBONE
  119. The ckpt file of DarkNet53. Default: "".
  120. --resume_yolov3 RESUME_YOLOV3
  121. The ckpt file of YOLOv3, which used to fine tune.
  122. Default: ""
  123. --lr_scheduler LR_SCHEDULER
  124. Learning rate scheduler, options: exponential,
  125. cosine_annealing. Default: exponential
  126. --lr LR Learning rate. Default: 0.001
  127. --lr_epochs LR_EPOCHS
  128. Epoch of changing of lr changing, split with ",".
  129. Default: 220,250
  130. --lr_gamma LR_GAMMA Decrease lr by a factor of exponential lr_scheduler.
  131. Default: 0.1
  132. --eta_min ETA_MIN Eta_min in cosine_annealing scheduler. Default: 0
  133. --T_max T_MAX T-max in cosine_annealing scheduler. Default: 320
  134. --max_epoch MAX_EPOCH
  135. Max epoch num to train the model. Default: 320
  136. --warmup_epochs WARMUP_EPOCHS
  137. Warmup epochs. Default: 0
  138. --weight_decay WEIGHT_DECAY
  139. Weight decay factor. Default: 0.0005
  140. --momentum MOMENTUM Momentum. Default: 0.9
  141. --loss_scale LOSS_SCALE
  142. Static loss scale. Default: 1024
  143. --label_smooth LABEL_SMOOTH
  144. Whether to use label smooth in CE. Default:0
  145. --label_smooth_factor LABEL_SMOOTH_FACTOR
  146. Smooth strength of original one-hot. Default: 0.1
  147. --log_interval LOG_INTERVAL
  148. Logging interval steps. Default: 100
  149. --ckpt_path CKPT_PATH
  150. Checkpoint save location. Default: outputs/
  151. --ckpt_interval CKPT_INTERVAL
  152. Save checkpoint interval. Default: None
  153. --is_save_on_master IS_SAVE_ON_MASTER
  154. Save ckpt on master or all rank, 1 for master, 0 for
  155. all ranks. Default: 1
  156. --is_distributed IS_DISTRIBUTED
  157. Distribute train or not, 1 for yes, 0 for no. Default:
  158. 1
  159. --rank RANK Local rank of distributed. Default: 0
  160. --group_size GROUP_SIZE
  161. World size of device. Default: 1
  162. --need_profiler NEED_PROFILER
  163. Whether use profiler. 0 for no, 1 for yes. Default: 0
  164. --training_shape TRAINING_SHAPE
  165. Fix training shape. Default: ""
  166. --resize_rate RESIZE_RATE
  167. Resize rate for multi-scale training. Default: None
  168. ```
  169. ## [Training Process](#contents)
  170. ### Training
  171. ```
  172. python train.py \
  173. --data_dir=./dataset/coco2014 \
  174. --pretrained_backbone=darknet53_backbone.ckpt \
  175. --is_distributed=0 \
  176. --lr=0.1 \
  177. --T_max=320 \
  178. --max_epoch=320 \
  179. --warmup_epochs=4 \
  180. --training_shape=416 \
  181. --lr_scheduler=cosine_annealing > log.txt 2>&1 &
  182. ```
  183. The python command above will run in the background, you can view the results through the file `log.txt`. If running on GPU, please add `--device_target=GPU` in the python command.
  184. After training, you'll get some checkpoint files under the outputs folder by default. The loss value will be achieved as follows:
  185. ```
  186. # grep "loss:" train/log.txt
  187. 2020-08-20 14:14:43,640:INFO:epoch[0], iter[0], loss:7809.262695, 0.15 imgs/sec, lr:9.746589057613164e-06
  188. 2020-08-20 14:15:05,142:INFO:epoch[0], iter[100], loss:2778.349033, 133.92 imgs/sec, lr:0.0009844054002314806
  189. 2020-08-20 14:15:31,796:INFO:epoch[0], iter[200], loss:535.517361, 130.54 imgs/sec, lr:0.0019590642768889666
  190. ...
  191. ```
  192. The model checkpoint will be saved in outputs directory.
  193. ### Distributed Training
  194. For Ascend device, distributed training example(8p) by shell script
  195. ```
  196. sh run_distribute_train.sh dataset/coco2014 darknet53_backbone.ckpt rank_table_8p.json
  197. ```
  198. For GPU device, distributed training example(8p) by shell script
  199. ```
  200. sh run_distribute_train_gpu.sh dataset/coco2014 darknet53_backbone.ckpt
  201. ```
  202. The above shell script will run distribute training in the background. You can view the results through the file `train_parallel[X]/log.txt`. The loss value will be achieved as follows:
  203. ```
  204. # distribute training result(8p)
  205. epoch[0], iter[0], loss:14623.384766, 1.23 imgs/sec, lr:7.812499825377017e-05
  206. epoch[0], iter[100], loss:1486.253051, 15.01 imgs/sec, lr:0.007890624925494194
  207. epoch[0], iter[200], loss:288.579535, 490.41 imgs/sec, lr:0.015703124925494194
  208. epoch[0], iter[300], loss:153.136754, 531.99 imgs/sec, lr:0.023515624925494194
  209. epoch[1], iter[400], loss:106.429322, 405.14 imgs/sec, lr:0.03132812678813934
  210. ...
  211. epoch[318], iter[102000], loss:34.135306, 431.06 imgs/sec, lr:9.63797629083274e-06
  212. epoch[319], iter[102100], loss:35.652469, 449.52 imgs/sec, lr:2.409552052995423e-06
  213. epoch[319], iter[102200], loss:34.652273, 384.02 imgs/sec, lr:2.409552052995423e-06
  214. epoch[319], iter[102300], loss:35.430038, 423.49 imgs/sec, lr:2.409552052995423e-06
  215. ...
  216. ```
  217. ## [Evaluation Process](#contents)
  218. ### Evaluation
  219. Before running the command below. If running on GPU, please add `--device_target=GPU` in the python command or use the "_gpu" shell script ("xxx_gpu.sh").
  220. ```
  221. python eval.py \
  222. --data_dir=./dataset/coco2014 \
  223. --pretrained=yolov3.ckpt \
  224. --testing_shape=416 > log.txt 2>&1 &
  225. OR
  226. sh run_eval.sh dataset/coco2014/ checkpoint/0-319_102400.ckpt
  227. ```
  228. The above python command will run in the background. You can view the results through the file "log.txt". The mAP of the test dataset will be as follows:
  229. ```
  230. # log.txt
  231. =============coco eval reulst=========
  232. Average Precision (AP) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.311
  233. Average Precision (AP) @[ IoU=0.50 | area= all | maxDets=100 ] = 0.528
  234. Average Precision (AP) @[ IoU=0.75 | area= all | maxDets=100 ] = 0.322
  235. Average Precision (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.127
  236. Average Precision (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.323
  237. Average Precision (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.428
  238. Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 1 ] = 0.259
  239. Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 10 ] = 0.398
  240. Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.423
  241. Average Recall (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.224
  242. Average Recall (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.442
  243. Average Recall (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.551
  244. ```
  245. # [Model Description](#contents)
  246. ## [Performance](#contents)
  247. ### Evaluation Performance
  248. | Parameters | YOLO |YOLO |
  249. | -------------------------- | ----------------------------------------------------------- |------------------------------------------------------------ |
  250. | Model Version | YOLOv3 |YOLOv3 |
  251. | Resource | Ascend 910; CPU 2.60GHz, 192cores; Memory, 755G | NV SMX2 V100-16G; CPU 2.10GHz, 96cores; Memory, 251G |
  252. | uploaded Date | 09/15/2020 (month/day/year) | 09/02/2020 (month/day/year) |
  253. | MindSpore Version | 1.0.0 | 1.0.0 |
  254. | Dataset | COCO2014 | COCO2014 |
  255. | Training Parameters | epoch=320, batch_size=32, lr=0.001, momentum=0.9 | epoch=320, batch_size=32, lr=0.001, momentum=0.9 |
  256. | Optimizer | Momentum | Momentum |
  257. | Loss Function | Sigmoid Cross Entropy with logits | Sigmoid Cross Entropy with logits |
  258. | outputs | boxes and label | boxes and label |
  259. | Loss | 34 | 34 |
  260. | Speed | 1pc: 350 ms/step; | 1pc: 600 ms/step; |
  261. | Total time | 8pc: 18.5 hours | 8pc: 18 hours(shape=416) |
  262. | Parameters (M) | 62.1 | 62.1 |
  263. | Checkpoint for Fine tuning | 474M (.ckpt file) | 474M (.ckpt file) |
  264. | Scripts | https://gitee.com/mindspore/mindspore/tree/master/model_zoo/official/cv/yolov3_darknet53 | https://gitee.com/mindspore/mindspore/tree/master/model_zoo/official/cv/yolov3_darknet53 |
  265. ### Inference Performance
  266. | Parameters | YOLO |YOLO |
  267. | ------------------- | --------------------------- |------------------------------|
  268. | Model Version | YOLOv3 | YOLOv3 |
  269. | Resource | Ascend 910 | NV SMX2 V100-16G |
  270. | Uploaded Date | 09/15/2020 (month/day/year) | 08/20/2020 (month/day/year) |
  271. | MindSpore Version | 1.0.0 | 1.0.0 |
  272. | Dataset | COCO2014, 40,504 images | COCO2014, 40,504 images |
  273. | batch_size | 1 | 1 |
  274. | outputs | mAP | mAP |
  275. | Accuracy | 8pcs: 31.1% | 8pcs: 29.7%~30.3% (shape=416)|
  276. | Model for inference | 474M (.ckpt file) | 474M (.ckpt file) |
  277. # [Description of Random Situation](#contents)
  278. There are random seeds in distributed_sampler.py, transforms.py, yolo_dataset.py files.
  279. # [ModelZoo Homepage](#contents)
  280. Please check the official [homepage](https://gitee.com/mindspore/mindspore/tree/master/model_zoo).