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.

3_exist_data_new_model.md 10 kB

2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. # 3: Train with customized models and standard datasets
  2. In this note, you will know how to train, test and inference your own customized models under standard datasets. We use the cityscapes dataset to train a customized Cascade Mask R-CNN R50 model as an example to demonstrate the whole process, which using [`AugFPN`](https://github.com/Gus-Guo/AugFPN) to replace the default `FPN` as neck, and add `Rotate` or `Translate` as training-time auto augmentation.
  3. The basic steps are as below:
  4. 1. Prepare the standard dataset
  5. 2. Prepare your own customized model
  6. 3. Prepare a config
  7. 4. Train, test, and inference models on the standard dataset.
  8. ## Prepare the standard dataset
  9. In this note, as we use the standard cityscapes dataset as an example.
  10. It is recommended to symlink the dataset root to `$MMDETECTION/data`.
  11. If your folder structure is different, you may need to change the corresponding paths in config files.
  12. ```none
  13. mmdetection
  14. ├── mmdet
  15. ├── tools
  16. ├── configs
  17. ├── data
  18. │ ├── coco
  19. │ │ ├── annotations
  20. │ │ ├── train2017
  21. │ │ ├── val2017
  22. │ │ ├── test2017
  23. │ ├── cityscapes
  24. │ │ ├── annotations
  25. │ │ ├── leftImg8bit
  26. │ │ │ ├── train
  27. │ │ │ ├── val
  28. │ │ ├── gtFine
  29. │ │ │ ├── train
  30. │ │ │ ├── val
  31. │ ├── VOCdevkit
  32. │ │ ├── VOC2007
  33. │ │ ├── VOC2012
  34. ```
  35. The cityscapes annotations have to be converted into the coco format using `tools/dataset_converters/cityscapes.py`:
  36. ```shell
  37. pip install cityscapesscripts
  38. python tools/dataset_converters/cityscapes.py ./data/cityscapes --nproc 8 --out-dir ./data/cityscapes/annotations
  39. ```
  40. Currently the config files in `cityscapes` use COCO pre-trained weights to initialize.
  41. You could download the pre-trained models in advance if network is unavailable or slow, otherwise it would cause errors at the beginning of training.
  42. ## Prepare your own customized model
  43. The second step is to use your own module or training setting. Assume that we want to implement a new neck called `AugFPN` to replace with the default `FPN` under the existing detector Cascade Mask R-CNN R50. The following implements`AugFPN` under MMDetection.
  44. ### 1. Define a new neck (e.g. AugFPN)
  45. Firstly create a new file `mmdet/models/necks/augfpn.py`.
  46. ```python
  47. from ..builder import NECKS
  48. @NECKS.register_module()
  49. class AugFPN(nn.Module):
  50. def __init__(self,
  51. in_channels,
  52. out_channels,
  53. num_outs,
  54. start_level=0,
  55. end_level=-1,
  56. add_extra_convs=False):
  57. pass
  58. def forward(self, inputs):
  59. # implementation is ignored
  60. pass
  61. ```
  62. ### 2. Import the module
  63. You can either add the following line to `mmdet/models/necks/__init__.py`,
  64. ```python
  65. from .augfpn import AugFPN
  66. ```
  67. or alternatively add
  68. ```python
  69. custom_imports = dict(
  70. imports=['mmdet.models.necks.augfpn.py'],
  71. allow_failed_imports=False)
  72. ```
  73. to the config file and avoid modifying the original code.
  74. ### 3. Modify the config file
  75. ```python
  76. neck=dict(
  77. type='AugFPN',
  78. in_channels=[256, 512, 1024, 2048],
  79. out_channels=256,
  80. num_outs=5)
  81. ```
  82. For more detailed usages about customize your own models (e.g. implement a new backbone, head, loss, etc) and runtime training settings (e.g. define a new optimizer, use gradient clip, customize training schedules and hooks, etc), please refer to the guideline [Customize Models](tutorials/customize_models.md) and [Customize Runtime Settings](tutorials/customize_runtime.md) respectively.
  83. ## Prepare a config
  84. The third step is to prepare a config for your own training setting. Assume that we want to add `AugFPN` and `Rotate` or `Translate` augmentation to existing Cascade Mask R-CNN R50 to train the cityscapes dataset, and assume the config is under directory `configs/cityscapes/` and named as `cascade_mask_rcnn_r50_augfpn_autoaug_10e_cityscapes.py`, the config is as below.
  85. ```python
  86. # The new config inherits the base configs to highlight the necessary modification
  87. _base_ = [
  88. '../_base_/models/cascade_mask_rcnn_r50_fpn.py',
  89. '../_base_/datasets/cityscapes_instance.py', '../_base_/default_runtime.py'
  90. ]
  91. model = dict(
  92. # set None to avoid loading ImageNet pretrained backbone,
  93. # instead here we set `load_from` to load from COCO pretrained detectors.
  94. backbone=dict(init_cfg=None),
  95. # replace neck from defaultly `FPN` to our new implemented module `AugFPN`
  96. neck=dict(
  97. type='AugFPN',
  98. in_channels=[256, 512, 1024, 2048],
  99. out_channels=256,
  100. num_outs=5),
  101. # We also need to change the num_classes in head from 80 to 8, to match the
  102. # cityscapes dataset's annotation. This modification involves `bbox_head` and `mask_head`.
  103. roi_head=dict(
  104. bbox_head=[
  105. dict(
  106. type='Shared2FCBBoxHead',
  107. in_channels=256,
  108. fc_out_channels=1024,
  109. roi_feat_size=7,
  110. # change the number of classes from defaultly COCO to cityscapes
  111. num_classes=8,
  112. bbox_coder=dict(
  113. type='DeltaXYWHBBoxCoder',
  114. target_means=[0., 0., 0., 0.],
  115. target_stds=[0.1, 0.1, 0.2, 0.2]),
  116. reg_class_agnostic=True,
  117. loss_cls=dict(
  118. type='CrossEntropyLoss',
  119. use_sigmoid=False,
  120. loss_weight=1.0),
  121. loss_bbox=dict(type='SmoothL1Loss', beta=1.0,
  122. loss_weight=1.0)),
  123. dict(
  124. type='Shared2FCBBoxHead',
  125. in_channels=256,
  126. fc_out_channels=1024,
  127. roi_feat_size=7,
  128. # change the number of classes from defaultly COCO to cityscapes
  129. num_classes=8,
  130. bbox_coder=dict(
  131. type='DeltaXYWHBBoxCoder',
  132. target_means=[0., 0., 0., 0.],
  133. target_stds=[0.05, 0.05, 0.1, 0.1]),
  134. reg_class_agnostic=True,
  135. loss_cls=dict(
  136. type='CrossEntropyLoss',
  137. use_sigmoid=False,
  138. loss_weight=1.0),
  139. loss_bbox=dict(type='SmoothL1Loss', beta=1.0,
  140. loss_weight=1.0)),
  141. dict(
  142. type='Shared2FCBBoxHead',
  143. in_channels=256,
  144. fc_out_channels=1024,
  145. roi_feat_size=7,
  146. # change the number of classes from defaultly COCO to cityscapes
  147. num_classes=8,
  148. bbox_coder=dict(
  149. type='DeltaXYWHBBoxCoder',
  150. target_means=[0., 0., 0., 0.],
  151. target_stds=[0.033, 0.033, 0.067, 0.067]),
  152. reg_class_agnostic=True,
  153. loss_cls=dict(
  154. type='CrossEntropyLoss',
  155. use_sigmoid=False,
  156. loss_weight=1.0),
  157. loss_bbox=dict(type='SmoothL1Loss', beta=1.0, loss_weight=1.0))
  158. ],
  159. mask_head=dict(
  160. type='FCNMaskHead',
  161. num_convs=4,
  162. in_channels=256,
  163. conv_out_channels=256,
  164. # change the number of classes from defaultly COCO to cityscapes
  165. num_classes=8,
  166. loss_mask=dict(
  167. type='CrossEntropyLoss', use_mask=True, loss_weight=1.0))))
  168. # over-write `train_pipeline` for new added `AutoAugment` training setting
  169. img_norm_cfg = dict(
  170. mean=[123.675, 116.28, 103.53], std=[58.395, 57.12, 57.375], to_rgb=True)
  171. train_pipeline = [
  172. dict(type='LoadImageFromFile'),
  173. dict(type='LoadAnnotations', with_bbox=True, with_mask=True),
  174. dict(
  175. type='AutoAugment',
  176. policies=[
  177. [dict(
  178. type='Rotate',
  179. level=5,
  180. img_fill_val=(124, 116, 104),
  181. prob=0.5,
  182. scale=1)
  183. ],
  184. [dict(type='Rotate', level=7, img_fill_val=(124, 116, 104)),
  185. dict(
  186. type='Translate',
  187. level=5,
  188. prob=0.5,
  189. img_fill_val=(124, 116, 104))
  190. ],
  191. ]),
  192. dict(
  193. type='Resize', img_scale=[(2048, 800), (2048, 1024)], keep_ratio=True),
  194. dict(type='RandomFlip', flip_ratio=0.5),
  195. dict(type='Normalize', **img_norm_cfg),
  196. dict(type='Pad', size_divisor=32),
  197. dict(type='DefaultFormatBundle'),
  198. dict(type='Collect', keys=['img', 'gt_bboxes', 'gt_labels', 'gt_masks']),
  199. ]
  200. # set batch_size per gpu, and set new training pipeline
  201. data = dict(
  202. samples_per_gpu=1,
  203. workers_per_gpu=3,
  204. # over-write `pipeline` with new training pipeline setting
  205. train=dict(dataset=dict(pipeline=train_pipeline)))
  206. # Set optimizer
  207. optimizer = dict(type='SGD', lr=0.01, momentum=0.9, weight_decay=0.0001)
  208. optimizer_config = dict(grad_clip=None)
  209. # Set customized learning policy
  210. lr_config = dict(
  211. policy='step',
  212. warmup='linear',
  213. warmup_iters=500,
  214. warmup_ratio=0.001,
  215. step=[8])
  216. runner = dict(type='EpochBasedRunner', max_epochs=10)
  217. # We can use the COCO pretrained Cascade Mask R-CNN R50 model for more stable performance initialization
  218. load_from = 'https://download.openmmlab.com/mmdetection/v2.0/cascade_rcnn/cascade_mask_rcnn_r50_fpn_1x_coco/cascade_mask_rcnn_r50_fpn_1x_coco_20200203-9d4dcb24.pth'
  219. ```
  220. ## Train a new model
  221. To train a model with the new config, you can simply run
  222. ```shell
  223. python tools/train.py configs/cityscapes/cascade_mask_rcnn_r50_augfpn_autoaug_10e_cityscapes.py
  224. ```
  225. For more detailed usages, please refer to the [Case 1](1_exist_data_model.md).
  226. ## Test and inference
  227. To test the trained model, you can simply run
  228. ```shell
  229. python tools/test.py configs/cityscapes/cascade_mask_rcnn_r50_augfpn_autoaug_10e_cityscapes.py work_dirs/cascade_mask_rcnn_r50_augfpn_autoaug_10e_cityscapes.py/latest.pth --eval bbox segm
  230. ```
  231. For more detailed usages, please refer to the [Case 1](1_exist_data_model.md).

No Description

Contributors (3)