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_CN.md 15 kB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. # ![logo](https://www.mindspore.cn/static/img/logo_black.6a5c850d.png)
  2. <!-- TOC -->
  3. - <span id="content">[Retinanet 描述](#Retinanet-描述)</span>
  4. - [模型架构](#模型架构)
  5. - [数据集](#数据集)
  6. - [环境要求](#环境要求)
  7. - [脚本说明](#脚本说明)
  8. - [脚本和示例代码](#脚本和示例代码)
  9. - [脚本参数](#脚本参数)
  10. - [训练过程](#训练过程)
  11. - [用法](#用法)
  12. - [运行](#运行)
  13. - [结果](#结果)
  14. - [评估过程](#评估过程)
  15. - [用法](#usage)
  16. - [运行](#running)
  17. - [结果](#outcome)
  18. - [模型说明](#模型说明)
  19. - [性能](#性能)
  20. - [训练性能](#训练性能)
  21. - [推理性能](#推理性能)
  22. - [随机情况的描述](#随机情况的描述)
  23. - [ModelZoo 主页](#modelzoo-主页)
  24. <!-- /TOC -->
  25. ## [Retinanet 描述](#content)
  26. RetinaNet算法源自2018年Facebook AI Research的论文 Focal Loss for Dense Object Detection。该论文最大的贡献在于提出了Focal Loss用于解决类别不均衡问题,从而创造了RetinaNet(One Stage目标检测算法)这个精度超越经典Two Stage的Faster-RCNN的目标检测网络。
  27. [论文](https://arxiv.org/pdf/1708.02002.pdf)
  28. Lin T Y , Goyal P , Girshick R , et al. Focal Loss for Dense Object Detection[C]// 2017 IEEE International Conference on Computer Vision (ICCV). IEEE, 2017:2999-3007.
  29. ## [模型架构](#content)
  30. Retinanet的整体网络架构如下所示:
  31. [链接](https://arxiv.org/pdf/1708.02002.pdf)
  32. ## [数据集](#content)
  33. 数据集可参考文献.
  34. MSCOCO2017
  35. - 数据集大小: 19.3G, 123287张80类彩色图像
  36. - 训练:19.3G, 118287张图片
  37. - 测试:1814.3M, 5000张图片
  38. - 数据格式:RGB图像.
  39. - 注意:数据将在src/dataset.py 中被处理
  40. ## [环境要求](#content)
  41. - 硬件(Ascend)
  42. - 使用Ascend处理器准备硬件环境。
  43. - 架构
  44. - [MindSpore](https://www.mindspore.cn/install/en)
  45. - 想要获取更多信息,请检查以下资源:
  46. - [MindSpore 教程](https://www.mindspore.cn/tutorial/training/en/master/index.html)
  47. - [MindSpore Python API](https://www.mindspore.cn/doc/api_python/en/master/index.html)
  48. ## [脚本说明](#content)
  49. ### [脚本和示例代码](#content)
  50. ```shell
  51. .
  52. └─Retinanet
  53. ├─README.md
  54. ├─scripts
  55. ├─run_single_train.sh # 使用Ascend环境单卡训练
  56. ├─run_distribute_train.sh # 使用Ascend环境八卡并行训练
  57. ├─run_eval.sh # 使用Ascend环境运行推理脚本
  58. ├─src
  59. ├─config.py # 参数配置
  60. ├─dataset.py # 数据预处理
  61. ├─retinanet.py # 网络模型定义
  62. ├─init_params.py # 参数初始化
  63. ├─lr_generator.py # 学习率生成函数
  64. ├─coco_eval # coco数据集评估
  65. ├─box_utils.py # 先验框设置
  66. ├─_init_.py # 初始化
  67. ├─train.py # 网络训练脚本
  68. └─eval.py # 网络推理脚本
  69. ```
  70. ### [脚本参数](#content)
  71. ```python
  72. 在train.py和config.py脚本中使用到的主要参数是:
  73. "img_shape": [600, 600], # 图像尺寸
  74. "num_retinanet_boxes": 67995, # 设置的先验框总数
  75. "match_thershold": 0.5, # 匹配阈值
  76. "nms_thershold": 0.6, # 非极大抑制阈值
  77. "min_score": 0.1, # 最低得分
  78. "max_boxes": 100, # 检测框最大数量
  79. "global_step": 0, # 全局步数
  80. "lr_init": 1e-6, # 初始学习率
  81. "lr_end_rate": 5e-3, # 最终学习率与最大学习率的比值
  82. "warmup_epochs1": 2, # 第一阶段warmup的周期数
  83. "warmup_epochs2": 5, # 第二阶段warmup的周期数
  84. "warmup_epochs3": 23, # 第三阶段warmup的周期数
  85. "warmup_epochs4": 60, # 第四阶段warmup的周期数
  86. "warmup_epochs5": 160, # 第五阶段warmup的周期数
  87. "momentum": 0.9, # momentum
  88. "weight_decay": 1.5e-4, # 权重衰减率
  89. "num_default": [9, 9, 9, 9, 9], # 单个网格中先验框的个数
  90. "extras_out_channels": [256, 256, 256, 256, 256], # 特征层输出通道数
  91. "feature_size": [75, 38, 19, 10, 5], # 特征层尺寸
  92. "aspect_ratios": [(0.5,1.0,2.0), (0.5,1.0,2.0), (0.5,1.0,2.0), (0.5,1.0,2.0), (0.5,1.0,2.0)], # 先验框大小变化比值
  93. "steps": ( 8, 16, 32, 64, 128), # 先验框设置步长
  94. "anchor_size":(32, 64, 128, 256, 512), # 先验框尺寸
  95. "prior_scaling": (0.1, 0.2), # 用于调节回归与回归在loss中占的比值
  96. "gamma": 2.0, # focal loss中的参数
  97. "alpha": 0.75, # focal loss中的参数
  98. "mindrecord_dir": "/cache/MindRecord_COCO", # mindrecord文件路径
  99. "coco_root": "/cache/coco", # coco数据集路径
  100. "train_data_type": "train2017", # train图像的文件夹名
  101. "val_data_type": "val2017", # val图像的文件夹名
  102. "instances_set": "annotations_trainval2017/annotations/instances_{}.json", # 标签文件路径
  103. "coco_classes": ('background', 'person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', # coco数据集的种类
  104. 'train', 'truck', 'boat', 'traffic light', 'fire hydrant',
  105. 'stop sign', 'parking meter', 'bench', 'bird', 'cat', 'dog',
  106. 'horse', 'sheep', 'cow', 'elephant', 'bear', 'zebra',
  107. 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie',
  108. 'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball',
  109. 'kite', 'baseball bat', 'baseball glove', 'skateboard',
  110. 'surfboard', 'tennis racket', 'bottle', 'wine glass', 'cup',
  111. 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple',
  112. 'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza',
  113. 'donut', 'cake', 'chair', 'couch', 'potted plant', 'bed',
  114. 'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote',
  115. 'keyboard', 'cell phone', 'microwave', 'oven', 'toaster', 'sink',
  116. 'refrigerator', 'book', 'clock', 'vase', 'scissors',
  117. 'teddy bear', 'hair drier', 'toothbrush'),
  118. "num_classes": 81, # 数据集类别数
  119. "voc_root": "", # voc数据集路径
  120. "voc_dir": "",
  121. "image_dir": "", # 图像路径
  122. "anno_path": "", # 标签文件路径
  123. "save_checkpoint": True, # 保存checkpoint
  124. "save_checkpoint_epochs": 1, # 保存checkpoint epoch数
  125. "keep_checkpoint_max":1, # 保存checkpoint的最大数量
  126. "save_checkpoint_path": "./model", # 保存checkpoint的路径
  127. "finish_epoch":0, # 已经运行完成的 epoch 数
  128. "checkpoint_path":"/home/hitwh1/1.0/ckpt_0/retinanet-500_458_59.ckpt" # 用于验证的checkpoint路径
  129. ```
  130. ### [训练过程](#content)
  131. #### 用法
  132. 使用shell脚本进行训练。shell脚本的用法如下:
  133. ```训练
  134. # 八卡并行训练示例:
  135. 创建 RANK_TABLE_FILE
  136. sh run_distribute_train.sh DEVICE_NUM EPOCH_SIZE LR RANK_TABLE_FILE PRE_TRAINED(optional) PRE_TRAINED_EPOCH_SIZE(optional)
  137. # 单卡训练示例:
  138. sh run_single_train.sh DEVICE_ID EPOCH_SIZE LR PRE_TRAINED(optional) PRE_TRAINED_EPOCH_SIZE(optional)
  139. ```
  140. > 注意:
  141. RANK_TABLE_FILE相关参考资料见[链接](https://www.mindspore.cn/tutorial/training/en/master/advanced_use/distributed_training_ascend.html), 获取device_ip方法详见[链接](https://gitee.com/mindspore/mindspore/tree/master/model_zoo/utils/hccl_tools).
  142. #### 运行
  143. ```运行
  144. 训练前,先创建MindRecord文件,以COCO数据集为例
  145. python create_data.py --dataset coco
  146. Ascend:
  147. # 八卡并行训练示例(在retinanet目录下运行):
  148. sh scripts/run_distribute_train.sh 8 500 0.09 RANK_TABLE_FILE(创建的RANK_TABLE_FILE的地址) PRE_TRAINED(预训练checkpoint地址,可选) PRE_TRAINED_EPOCH_SIZE(预训练EPOCH大小,可选)
  149. 例如:sh scripts/run_distribute_train.sh 8 500 0.09 scripts/rank_table_8pcs.json
  150. # 单卡训练示例(在retinanet目录下运行):
  151. sh scripts/run_single_train.sh 0 500 0.09
  152. ```
  153. #### 结果
  154. 训练结果将存储在示例路径中。checkpoint将存储在 `./model` 路径下,训练日志将被记录到 `./log.txt` 中,训练日志部分示例如下:
  155. ```训练日志
  156. epoch: 2 step: 458, loss is 120.56251
  157. lr:[0.000003]
  158. Epoch time: 164034.415, per step time: 358.154
  159. epoch: 3 step: 458, loss is 11.834166
  160. lr:[0.000028]
  161. Epoch time: 164292.012, per step time: 358.716
  162. epoch: 4 step: 458, loss is 10.49008
  163. lr:[0.000046]
  164. Epoch time: 164822.921, per step time: 359.875
  165. epoch: 5 step: 458, loss is 12.134182
  166. lr:[0.000064]
  167. Epoch time: 164531.610, per step time: 359.239
  168. ```
  169. ### [评估过程](#content)
  170. #### <span id="usage">用法</span>
  171. 使用shell脚本进行评估。shell脚本的用法如下:
  172. ```eval
  173. sh scripts/run_eval.sh [DATASET] [DEVICE_ID]
  174. ```
  175. #### <span id="running">运行</span>
  176. ```eval运行
  177. sh scripts/run_eval.sh coco 0
  178. ```
  179. > checkpoint 可以在训练过程中产生.
  180. #### <span id="outcome">结果</span>
  181. 计算结果将存储在示例路径中,您可以在 `eval.log` 查看.
  182. ```mAP
  183. Average Precision (AP) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.347
  184. Average Precision (AP) @[ IoU=0.50 | area= all | maxDets=100 ] = 0.503
  185. Average Precision (AP) @[ IoU=0.75 | area= all | maxDets=100 ] = 0.385
  186. Average Precision (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.134
  187. Average Precision (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.366
  188. Average Precision (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.501
  189. Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 1 ] = 0.302
  190. Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 10 ] = 0.412
  191. Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.414
  192. Average Recall (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.152
  193. Average Recall (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.434
  194. Average Recall (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.598
  195. ========================================
  196. mAP: 0.34747137754625645
  197. ```
  198. ## [模型说明](#content)
  199. ### [性能](#content)
  200. #### 训练性能
  201. | 参数 | Ascend |
  202. | -------------------------- | ------------------------------------- |
  203. | 模型名称 | Retinanet |
  204. | 运行环境 | Ascend 910;CPU 2.6GHz,192cores;Memory 755G;系统 Euler2.8 |
  205. | 上传时间 | 10/01/2021 |
  206. | MindSpore 版本 | 1.2.0 |
  207. | 数据集 | 123287 张图片 |
  208. | Batch_size | 32 |
  209. | 训练参数 | src/config.py |
  210. | 优化器 | Momentum |
  211. | 损失函数 | Focal loss |
  212. | 最终损失 | 0.582 |
  213. | 精确度 (8p) | mAP[0.3475] |
  214. | 训练总时间 (8p) | 23h16m54s |
  215. | 脚本 | [链接](https://gitee.com/mindspore/mindspore/tree/master/model_zoo/official/cv/retinanet) |
  216. #### 推理性能
  217. | 参数 | Ascend |
  218. | ------------------- | --------------------------- |
  219. | 模型名称 | Retinanet |
  220. | 运行环境 | Ascend 910;CPU 2.6GHz,192cores;Memory 755G;系统 Euler2.8|
  221. | 上传时间 | 10/01/2021 |
  222. | MindSpore 版本 | 1.2.0 |
  223. | 数据集 | 5k 张图片 |
  224. | Batch_size | 32 |
  225. | 精确度 | mAP[0.3475] |
  226. | 总时间 | 10 mins and 50 seconds |
  227. ## [随机情况的描述](#content)
  228. 在 `dataset.py` 脚本中, 我们在 `create_dataset` 函数中设置了随机种子. 我们在 `train.py` 脚本中也设置了随机种子.
  229. ## [ModelZoo 主页](#content)
  230. 请核对官方 [主页](https://gitee.com/mindspore/mindspore/tree/master/model_zoo).