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.

2_new_data_model.md 7.0 kB

2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. # 2: 在自定义数据集上进行训练
  2. 通过本文档,你将会知道如何使用自定义数据集对预先定义好的模型进行推理,测试以及训练。我们使用 [balloon dataset](https://github.com/matterport/Mask_RCNN/tree/master/samples/balloon) 作为例子来描述整个过程。
  3. 基本步骤如下:
  4. 1. 准备自定义数据集
  5. 2. 准备配置文件
  6. 3. 在自定义数据集上进行训练,测试和推理。
  7. ## 准备自定义数据集
  8. MMDetection 一共支持三种形式应用新数据集:
  9. 1. 将数据集重新组织为 COCO 格式。
  10. 2. 将数据集重新组织为一个中间格式。
  11. 3. 实现一个新的数据集。
  12. 我们通常建议使用前面两种方法,因为它们通常来说比第三种方法要简单。
  13. 在本文档中,我们展示一个例子来说明如何将数据转化为 COCO 格式。
  14. **注意**:MMDetection 现只支持对 COCO 格式的数据集进行 mask AP 的评测。
  15. 所以用户如果要进行实例分割,只能将数据转成 COCO 格式。
  16. ### COCO标注格式
  17. 用于实例分割的 COCO 数据集格式如下所示,其中的键(key)都是必要的,参考[这里](https://cocodataset.org/#format-data)来获取更多细节。
  18. ```json
  19. {
  20. "images": [image],
  21. "annotations": [annotation],
  22. "categories": [category]
  23. }
  24. image = {
  25. "id": int,
  26. "width": int,
  27. "height": int,
  28. "file_name": str,
  29. }
  30. annotation = {
  31. "id": int,
  32. "image_id": int,
  33. "category_id": int,
  34. "segmentation": RLE or [polygon],
  35. "area": float,
  36. "bbox": [x,y,width,height],
  37. "iscrowd": 0 or 1,
  38. }
  39. categories = [{
  40. "id": int,
  41. "name": str,
  42. "supercategory": str,
  43. }]
  44. ```
  45. 现在假设我们使用 balloon dataset。
  46. 下载了数据集之后,我们需要实现一个函数将标注格式转化为 COCO 格式。然后我们就可以使用已经实现的 `COCODataset` 类来加载数据并进行训练以及评测。
  47. 如果你浏览过新数据集,你会发现格式如下:
  48. ```json
  49. {'base64_img_data': '',
  50. 'file_attributes': {},
  51. 'filename': '34020010494_e5cb88e1c4_k.jpg',
  52. 'fileref': '',
  53. 'regions': {'0': {'region_attributes': {},
  54. 'shape_attributes': {'all_points_x': [1020,
  55. 1000,
  56. 994,
  57. 1003,
  58. 1023,
  59. 1050,
  60. 1089,
  61. 1134,
  62. 1190,
  63. 1265,
  64. 1321,
  65. 1361,
  66. 1403,
  67. 1428,
  68. 1442,
  69. 1445,
  70. 1441,
  71. 1427,
  72. 1400,
  73. 1361,
  74. 1316,
  75. 1269,
  76. 1228,
  77. 1198,
  78. 1207,
  79. 1210,
  80. 1190,
  81. 1177,
  82. 1172,
  83. 1174,
  84. 1170,
  85. 1153,
  86. 1127,
  87. 1104,
  88. 1061,
  89. 1032,
  90. 1020],
  91. 'all_points_y': [963,
  92. 899,
  93. 841,
  94. 787,
  95. 738,
  96. 700,
  97. 663,
  98. 638,
  99. 621,
  100. 619,
  101. 643,
  102. 672,
  103. 720,
  104. 765,
  105. 800,
  106. 860,
  107. 896,
  108. 942,
  109. 990,
  110. 1035,
  111. 1079,
  112. 1112,
  113. 1129,
  114. 1134,
  115. 1144,
  116. 1153,
  117. 1166,
  118. 1166,
  119. 1150,
  120. 1136,
  121. 1129,
  122. 1122,
  123. 1112,
  124. 1084,
  125. 1037,
  126. 989,
  127. 963],
  128. 'name': 'polygon'}}},
  129. 'size': 1115004}
  130. ```
  131. 标注文件时是 JSON 格式的,其中所有键(key)组成了一张图片的所有标注。
  132. 其中将 balloon dataset 转化为 COCO 格式的代码如下所示。
  133. ```python
  134. import os.path as osp
  135. def convert_balloon_to_coco(ann_file, out_file, image_prefix):
  136. data_infos = mmcv.load(ann_file)
  137. annotations = []
  138. images = []
  139. obj_count = 0
  140. for idx, v in enumerate(mmcv.track_iter_progress(data_infos.values())):
  141. filename = v['filename']
  142. img_path = osp.join(image_prefix, filename)
  143. height, width = mmcv.imread(img_path).shape[:2]
  144. images.append(dict(
  145. id=idx,
  146. file_name=filename,
  147. height=height,
  148. width=width))
  149. bboxes = []
  150. labels = []
  151. masks = []
  152. for _, obj in v['regions'].items():
  153. assert not obj['region_attributes']
  154. obj = obj['shape_attributes']
  155. px = obj['all_points_x']
  156. py = obj['all_points_y']
  157. poly = [(x + 0.5, y + 0.5) for x, y in zip(px, py)]
  158. poly = [p for x in poly for p in x]
  159. x_min, y_min, x_max, y_max = (
  160. min(px), min(py), max(px), max(py))
  161. data_anno = dict(
  162. image_id=idx,
  163. id=obj_count,
  164. category_id=0,
  165. bbox=[x_min, y_min, x_max - x_min, y_max - y_min],
  166. area=(x_max - x_min) * (y_max - y_min),
  167. segmentation=[poly],
  168. iscrowd=0)
  169. annotations.append(data_anno)
  170. obj_count += 1
  171. coco_format_json = dict(
  172. images=images,
  173. annotations=annotations,
  174. categories=[{'id':0, 'name': 'balloon'}])
  175. mmcv.dump(coco_format_json, out_file)
  176. ```
  177. 使用如上的函数,用户可以成功将标注文件转化为 JSON 格式,之后可以使用 `CocoDataset` 对模型进行训练和评测。
  178. ## 准备配置文件
  179. 第二步需要准备一个配置文件来成功加载数据集。假设我们想要用 balloon dataset 来训练配备了 FPN 的 Mask R-CNN ,如下是我们的配置文件。假设配置文件命名为 `mask_rcnn_r50_caffe_fpn_mstrain-poly_1x_balloon.py`,相应保存路径为 `configs/ballon/`,配置文件内容如下所示。
  180. ```python
  181. # 这个新的配置文件继承自一个原始配置文件,只需要突出必要的修改部分即可
  182. _base_ = 'mask_rcnn/mask_rcnn_r50_caffe_fpn_mstrain-poly_1x_coco.py'
  183. # 我们需要对头中的类别数量进行修改来匹配数据集的标注
  184. model = dict(
  185. roi_head=dict(
  186. bbox_head=dict(num_classes=1),
  187. mask_head=dict(num_classes=1)))
  188. # 修改数据集相关设置
  189. dataset_type = 'COCODataset'
  190. classes = ('balloon',)
  191. data = dict(
  192. train=dict(
  193. img_prefix='balloon/train/',
  194. classes=classes,
  195. ann_file='balloon/train/annotation_coco.json'),
  196. val=dict(
  197. img_prefix='balloon/val/',
  198. classes=classes,
  199. ann_file='balloon/val/annotation_coco.json'),
  200. test=dict(
  201. img_prefix='balloon/val/',
  202. classes=classes,
  203. ann_file='balloon/val/annotation_coco.json'))
  204. # 我们可以使用预训练的 Mask R-CNN 来获取更好的性能
  205. load_from = 'checkpoints/mask_rcnn_r50_caffe_fpn_mstrain-poly_3x_coco_bbox_mAP-0.408__segm_mAP-0.37_20200504_163245-42aa3d00.pth'
  206. ```
  207. ## 训练一个新的模型
  208. 为了使用新的配置方法来对模型进行训练,你只需要运行如下命令。
  209. ```shell
  210. python tools/train.py configs/balloon/mask_rcnn_r50_caffe_fpn_mstrain-poly_1x_balloon.py
  211. ```
  212. 参考[情况 1](./1_exist_data_model.md)来获取更多详细的使用方法。
  213. ## 测试以及推理
  214. 为了测试训练完毕的模型,你只需要运行如下命令。
  215. ```shell
  216. python tools/test.py configs/balloon/mask_rcnn_r50_caffe_fpn_mstrain-poly_1x_balloon.py work_dirs/mask_rcnn_r50_caffe_fpn_mstrain-poly_1x_balloon.py/latest.pth --eval bbox segm
  217. ```
  218. 参考[情况 1](./1_exist_data_model.md)来获取更多详细的使用方法。

No Description

Contributors (2)