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

2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. # 2: Train with customized datasets
  2. In this note, you will know how to inference, test, and train predefined models with customized datasets. We use the [balloon dataset](https://github.com/matterport/Mask_RCNN/tree/master/samples/balloon) as an example to describe the whole process.
  3. The basic steps are as below:
  4. 1. Prepare the customized dataset
  5. 2. Prepare a config
  6. 3. Train, test, inference models on the customized dataset.
  7. ## Prepare the customized dataset
  8. There are three ways to support a new dataset in MMDetection:
  9. 1. reorganize the dataset into COCO format.
  10. 2. reorganize the dataset into a middle format.
  11. 3. implement a new dataset.
  12. Usually we recommend to use the first two methods which are usually easier than the third.
  13. In this note, we give an example for converting the data into COCO format.
  14. **Note**: MMDetection only supports evaluating mask AP of dataset in COCO format for now.
  15. So for instance segmentation task users should convert the data into coco format.
  16. ### COCO annotation format
  17. The necessary keys of COCO format for instance segmentation is as below, for the complete details, please refer [here](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. Assume we use the balloon dataset.
  46. After downloading the data, we need to implement a function to convert the annotation format into the COCO format. Then we can use implemented COCODataset to load the data and perform training and evaluation.
  47. If you take a look at the dataset, you will find the dataset format is as below:
  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. The annotation is a JSON file where each key indicates an image's all annotations.
  132. The code to convert the balloon dataset into coco format is as below.
  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. Using the function above, users can successfully convert the annotation file into json format, then we can use `CocoDataset` to train and evaluate the model.
  178. ## Prepare a config
  179. The second step is to prepare a config thus the dataset could be successfully loaded. Assume that we want to use Mask R-CNN with FPN, the config to train the detector on balloon dataset is as below. Assume the config is under directory `configs/balloon/` and named as `mask_rcnn_r50_caffe_fpn_mstrain-poly_1x_balloon.py`, the config is as below.
  180. ```python
  181. # The new config inherits a base config to highlight the necessary modification
  182. _base_ = 'mask_rcnn/mask_rcnn_r50_caffe_fpn_mstrain-poly_1x_coco.py'
  183. # We also need to change the num_classes in head to match the dataset's annotation
  184. model = dict(
  185. roi_head=dict(
  186. bbox_head=dict(num_classes=1),
  187. mask_head=dict(num_classes=1)))
  188. # Modify dataset related settings
  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. # We can use the pre-trained Mask RCNN model to obtain higher performance
  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. ## Train a new model
  208. To train a model with the new config, you can simply run
  209. ```shell
  210. python tools/train.py configs/balloon/mask_rcnn_r50_caffe_fpn_mstrain-poly_1x_balloon.py
  211. ```
  212. For more detailed usages, please refer to the [Case 1](1_exist_data_model.md).
  213. ## Test and inference
  214. To test the trained model, you can simply run
  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. For more detailed usages, please refer to the [Case 1](1_exist_data_model.md).

No Description

Contributors (3)