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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. # Contents
  2. - [FasterRcnn Description](#fasterrcnn-description)
  3. - [Model Architecture](#model-architecture)
  4. - [Dataset](#dataset)
  5. - [Environment Requirements](#environment-requirements)
  6. - [Quick Start](#quick-start)
  7. - [Run in docker](#Run-in-docker)
  8. - [Script Description](#script-description)
  9. - [Script and Sample Code](#script-and-sample-code)
  10. - [Training Process](#training-process)
  11. - [Training Usage](#usage)
  12. - [Training Result](#result)
  13. - [Evaluation Process](#evaluation-process)
  14. - [Evaluation Usage](#usage)
  15. - [Evaluation Result](#result)
  16. - [Model Description](#model-description)
  17. - [Performance](#performance)
  18. - [Evaluation Performance](#evaluation-performance)
  19. - [Inference Performance](#inference-performance)
  20. - [ModelZoo Homepage](#modelzoo-homepage)
  21. # FasterRcnn Description
  22. Before FasterRcnn, the target detection networks rely on the region proposal algorithm to assume the location of targets, such as SPPnet and Fast R-CNN. Progress has reduced the running time of these detection networks, but it also reveals that the calculation of the region proposal is a bottleneck.
  23. FasterRcnn proposed that convolution feature maps based on region detectors (such as Fast R-CNN) can also be used to generate region proposals. At the top of these convolution features, a Region Proposal Network (RPN) is constructed by adding some additional convolution layers (which share the convolution characteristics of the entire image with the detection network, thus making it possible to make regions almost costlessProposal), outputting both region bounds and objectness score for each location.Therefore, RPN is a full convolutional network (FCN), which can be trained end-to-end, generate high-quality region proposals, and then fed into Fast R-CNN for detection.
  24. [Paper](https://arxiv.org/abs/1506.01497): Ren S , He K , Girshick R , et al. Faster R-CNN: Towards Real-Time Object Detection with Region Proposal Networks[J]. IEEE Transactions on Pattern Analysis and Machine Intelligence, 2015, 39(6).
  25. # Model Architecture
  26. FasterRcnn is a two-stage target detection network,This network uses a region proposal network (RPN), which can share the convolution features of the whole image with the detection network, so that the calculation of region proposal is almost cost free. The whole network further combines RPN and FastRcnn into a network by sharing the convolution features.
  27. # Dataset
  28. 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.
  29. Dataset used: [COCO2017](<https://cocodataset.org/>)
  30. - Dataset size:19G
  31. - Train:18G,118000 images
  32. - Val:1G,5000 images
  33. - Annotations:241M,instances,captions,person_keypoints etc
  34. - Data format:image and json files
  35. - Note:Data will be processed in dataset.py
  36. # Environment Requirements
  37. - Hardware(Ascend)
  38. - Prepare hardware environment with Ascend 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.
  39. - Docker base image
  40. - [Ascend Hub](ascend.huawei.com/ascendhub/#/home)
  41. - Install [MindSpore](https://www.mindspore.cn/install/en).
  42. - Download the dataset COCO2017.
  43. - We use COCO2017 as training dataset in this example by default, and you can also use your own datasets.
  44. 1. If coco dataset is used. **Select dataset to coco when run script.**
  45. Install Cython and pycocotool, and you can also install mmcv to process data.
  46. ```pip
  47. pip install Cython
  48. pip install pycocotools
  49. pip install mmcv==0.2.14
  50. ```
  51. And change the COCO_ROOT and other settings you need in `config.py`. The directory structure is as follows:
  52. ```path
  53. .
  54. └─cocodataset
  55. ├─annotations
  56. ├─instance_train2017.json
  57. └─instance_val2017.json
  58. ├─val2017
  59. └─train2017
  60. ```
  61. 2. If your own dataset is used. **Select dataset to other when run script.**
  62. Organize the dataset infomation into a TXT file, each row in the file is as follows:
  63. ```log
  64. train2017/0000001.jpg 0,259,401,459,7 35,28,324,201,2 0,30,59,80,2
  65. ```
  66. Each row is an image annotation which split by space, the first column is a relative path of image, the others are box and class infomations of the format [xmin,ymin,xmax,ymax,class]. We read image from an image path joined by the `IMAGE_DIR`(dataset directory) and the relative path in `ANNO_PATH`(the TXT file path), `IMAGE_DIR` and `ANNO_PATH` are setting in `config.py`.
  67. # Quick Start
  68. After installing MindSpore via the official website, you can start training and evaluation as follows:
  69. Note: 1.the first run will generate the mindeocrd file, which will take a long time.
  70. 2.pretrained model is a resnet50 checkpoint that trained over ImageNet2012.
  71. 3.VALIDATION_JSON_FILE is label file. CHECKPOINT_PATH is a checkpoint file after trained.
  72. ```shell
  73. # standalone training
  74. sh run_standalone_train_ascend.sh [PRETRAINED_MODEL]
  75. # distributed training
  76. sh run_distribute_train_ascend.sh [RANK_TABLE_FILE] [PRETRAINED_MODEL]
  77. # eval
  78. sh run_eval_ascend.sh [VALIDATION_JSON_FILE] [CHECKPOINT_PATH]
  79. # inference
  80. sh run_infer_310.sh [AIR_PATH] [DATA_PATH] [ANN_FILE_PATH]
  81. ```
  82. # Run in docker
  83. 1. Build docker images
  84. ```shell
  85. # build docker
  86. docker build -t fasterrcnn:20.1.0 . --build-arg FROM_IMAGE_NAME=ascend-mindspore-arm:20.1.0
  87. ```
  88. 2. Create a container layer over the created image and start it
  89. ```shell
  90. # start docker
  91. bash scripts/docker_start.sh fasterrcnn:20.1.0 [DATA_DIR] [MODEL_DIR]
  92. ```
  93. 3. Train
  94. ```shell
  95. # standalone training
  96. sh run_standalone_train_ascend.sh [PRETRAINED_MODEL]
  97. # distributed training
  98. sh run_distribute_train_ascend.sh [RANK_TABLE_FILE] [PRETRAINED_MODEL]
  99. ```
  100. 4. Eval
  101. ```shell
  102. # eval
  103. sh run_eval_ascend.sh [VALIDATION_JSON_FILE] [CHECKPOINT_PATH]
  104. ```
  105. 5. Inference
  106. ```shell
  107. # inference
  108. sh run_infer_310.sh [AIR_PATH] [DATA_PATH] [ANN_FILE_PATH]
  109. ```
  110. # Script Description
  111. ## Script and Sample Code
  112. ```shell
  113. .
  114. └─faster_rcnn
  115. ├─README.md // descriptions about fasterrcnn
  116. ├─ascend310_infer //application for 310 inference
  117. ├─scripts
  118. ├─run_standalone_train_ascend.sh // shell script for standalone on ascend
  119. ├─run_distribute_train_ascend.sh // shell script for distributed on ascend
  120. ├─run_infer_310.sh // shell script for 310 inference
  121. └─run_eval_ascend.sh // shell script for eval on ascend
  122. ├─src
  123. ├─FasterRcnn
  124. ├─__init__.py // init file
  125. ├─anchor_generator.py // anchor generator
  126. ├─bbox_assign_sample.py // first stage sampler
  127. ├─bbox_assign_sample_stage2.py // second stage sampler
  128. ├─faster_rcnn_r50.py // fasterrcnn network
  129. ├─fpn_neck.py //feature pyramid network
  130. ├─proposal_generator.py // proposal generator
  131. ├─rcnn.py // rcnn network
  132. ├─resnet50.py // backbone network
  133. ├─roi_align.py // roi align network
  134. └─rpn.py // region proposal network
  135. ├─aipp.cfg // aipp config file
  136. ├─config.py // total config
  137. ├─dataset.py // create dataset and process dataset
  138. ├─lr_schedule.py // learning ratio generator
  139. ├─network_define.py // network define for fasterrcnn
  140. └─util.py // routine operation
  141. ├─export.py // script to export AIR,MINDIR,ONNX model
  142. ├─eval.py //eval scripts
  143. ├─postprogress.py // post process for 310 inference
  144. └─train.py // train scripts
  145. ```
  146. ## Training Process
  147. ### Usage
  148. ```shell
  149. # standalone training on ascend
  150. sh run_standalone_train_ascend.sh [PRETRAINED_MODEL]
  151. # distributed training on ascend
  152. sh run_distribute_train_ascend.sh [RANK_TABLE_FILE] [PRETRAINED_MODEL]
  153. ```
  154. Notes:
  155. 1. Rank_table.json which is specified by RANK_TABLE_FILE is needed when you are running a distribute task. You can generate it by using the [hccl_tools](https://gitee.com/mindspore/mindspore/tree/master/model_zoo/utils/hccl_tools).
  156. 2. As for PRETRAINED_MODEL,it should be a trained ResNet50 checkpoint. If you need to load Ready-made pretrained FasterRcnn checkpoint, you may make changes to the train.py script as follows.
  157. ```python
  158. # Comment out the following code
  159. # load_path = args_opt.pre_trained
  160. # if load_path != "":
  161. # param_dict = load_checkpoint(load_path)
  162. # for item in list(param_dict.keys()):
  163. # if not item.startswith('backbone'):
  164. # param_dict.pop(item)
  165. # load_param_into_net(net, param_dict)
  166. # Add the following codes after optimizer definition since the FasterRcnn checkpoint includes optimizer parameters:
  167. lr = Tensor(dynamic_lr(config, rank_size=device_num), mstype.float32)
  168. opt = SGD(params=net.trainable_params(), learning_rate=lr, momentum=config.momentum,
  169. weight_decay=config.weight_decay, loss_scale=config.loss_scale)
  170. if load_path != "":
  171. param_dict = load_checkpoint(load_path)
  172. for item in list(param_dict.keys()):
  173. if item in ("global_step", "learning_rate") or "rcnn.reg_scores" in item or "rcnn.cls_scores" in item:
  174. param_dict.pop(item)
  175. load_param_into_net(opt, param_dict)
  176. load_param_into_net(net, param_dict)
  177. ```
  178. 3. The original dataset path needs to be in the config.py,you can select "coco_root" or "image_dir".
  179. ### Result
  180. Training result will be stored in the example path, whose folder name begins with "train" or "train_parallel". You can find checkpoint file together with result like the followings in loss_rankid.log.
  181. ```log
  182. # distribute training result(8p)
  183. epoch: 1 step: 7393, rpn_loss: 0.12054, rcnn_loss: 0.40601, rpn_cls_loss: 0.04025, rpn_reg_loss: 0.08032, rcnn_cls_loss: 0.25854, rcnn_reg_loss: 0.14746, total_loss: 0.52655
  184. epoch: 2 step: 7393, rpn_loss: 0.06561, rcnn_loss: 0.50293, rpn_cls_loss: 0.02587, rpn_reg_loss: 0.03967, rcnn_cls_loss: 0.35669, rcnn_reg_loss: 0.14624, total_loss: 0.56854
  185. epoch: 3 step: 7393, rpn_loss: 0.06940, rcnn_loss: 0.49658, rpn_cls_loss: 0.03769, rpn_reg_loss: 0.03165, rcnn_cls_loss: 0.36353, rcnn_reg_loss: 0.13318, total_loss: 0.56598
  186. ...
  187. epoch: 10 step: 7393, rpn_loss: 0.03555, rcnn_loss: 0.32666, rpn_cls_loss: 0.00697, rpn_reg_loss: 0.02859, rcnn_cls_loss: 0.16125, rcnn_reg_loss: 0.16541, total_loss: 0.36221
  188. epoch: 11 step: 7393, rpn_loss: 0.19849, rcnn_loss: 0.47827, rpn_cls_loss: 0.11639, rpn_reg_loss: 0.08209, rcnn_cls_loss: 0.29712, rcnn_reg_loss: 0.18115, total_loss: 0.67676
  189. epoch: 12 step: 7393, rpn_loss: 0.00691, rcnn_loss: 0.10168, rpn_cls_loss: 0.00529, rpn_reg_loss: 0.00162, rcnn_cls_loss: 0.05426, rcnn_reg_loss: 0.04745, total_loss: 0.10859
  190. ```
  191. ## Evaluation Process
  192. ### Usage
  193. ```shell
  194. # eval on ascend
  195. sh run_eval_ascend.sh [VALIDATION_JSON_FILE] [CHECKPOINT_PATH]
  196. ```
  197. > checkpoint can be produced in training process.
  198. ### Result
  199. Eval result will be stored in the example path, whose folder name is "eval". Under this, you can find result like the followings in log.
  200. ```log
  201. Average Precision (AP) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.360
  202. Average Precision (AP) @[ IoU=0.50 | area= all | maxDets=100 ] = 0.586
  203. Average Precision (AP) @[ IoU=0.75 | area= all | maxDets=100 ] = 0.385
  204. Average Precision (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.229
  205. Average Precision (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.402
  206. Average Precision (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.441
  207. Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 1 ] = 0.299
  208. Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 10 ] = 0.487
  209. Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.515
  210. Average Recall (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.346
  211. Average Recall (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.562
  212. Average Recall (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.631
  213. ```
  214. ## Model Export
  215. ```shell
  216. python export.py --ckpt_file [CKPT_PATH] --device_target [DEVICE_TARGET] --file_format[EXPORT_FORMAT]
  217. ```
  218. `EXPORT_FORMAT` shoule be in ["AIR", "ONNX", "MINDIR"]
  219. ## Inference Process
  220. ### Usage
  221. Before performing inference, the air file must bu exported by export script on the Ascend910 environment.
  222. ```shell
  223. # Ascend310 inference
  224. sh run_infer_310.sh [AIR_PATH] [DATA_PATH] [ANN_FILE_PATH]
  225. ```
  226. ### result
  227. Inference result is saved in current path, you can find result like this in acc.log file.
  228. ```log
  229. Average Precision (AP) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.349
  230. Average Precision (AP) @[ IoU=0.50 | area= all | maxDets=100 ] = 0.570
  231. Average Precision (AP) @[ IoU=0.75 | area= all | maxDets=100 ] = 0.369
  232. Average Precision (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.211
  233. Average Precision (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.391
  234. Average Precision (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.435
  235. Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 1 ] = 0.295
  236. Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 10 ] = 0.476
  237. Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.503
  238. Average Recall (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.330
  239. Average Recall (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.547
  240. Average Recall (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.622
  241. ```
  242. # Model Description
  243. ## Performance
  244. ### Evaluation Performance
  245. | Parameters | Ascend |
  246. | -------------------------- | ----------------------------------------------------------- |
  247. | Model Version | V1 |
  248. | Resource | Ascend 910 ;CPU 2.60GHz,192cores;Memory,755G |
  249. | uploaded Date | 08/31/2020 (month/day/year) |
  250. | MindSpore Version | 1.0.0 |
  251. | Dataset | COCO2017 |
  252. | Training Parameters | epoch=12, batch_size=2 |
  253. | Optimizer | SGD |
  254. | Loss Function | Softmax Cross Entropy ,Sigmoid Cross Entropy,SmoothL1Loss |
  255. | Speed | 1pc: 190 ms/step; 8pcs: 200 ms/step |
  256. | Total time | 1pc: 37.17 hours; 8pcs: 4.89 hours |
  257. | Parameters (M) | 250 |
  258. | Scripts | [fasterrcnn script](https://gitee.com/mindspore/mindspore/tree/master/model_zoo/official/cv/faster_rcnn) |
  259. ### Inference Performance
  260. | Parameters | Ascend |
  261. | ------------------- | --------------------------- |
  262. | Model Version | V1 |
  263. | Resource | Ascend 910 |
  264. | Uploaded Date | 08/31/2020 (month/day/year) |
  265. | MindSpore Version | 1.0.0 |
  266. | Dataset | COCO2017 |
  267. | batch_size | 2 |
  268. | outputs | mAP |
  269. | Accuracy | IoU=0.50: 57.6% |
  270. | Model for inference | 250M (.ckpt file) |
  271. # [ModelZoo Homepage](#contents)
  272. Please check the official [homepage](https://gitee.com/mindspore/mindspore/tree/master/model_zoo).