maskformer_r50_ms-16xb1-75e_coco.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. _base_ = [
  2. '../_base_/datasets/coco_panoptic.py', '../_base_/default_runtime.py'
  3. ]
  4. data_preprocessor = dict(
  5. type='DetDataPreprocessor',
  6. mean=[123.675, 116.28, 103.53],
  7. std=[58.395, 57.12, 57.375],
  8. bgr_to_rgb=True,
  9. pad_size_divisor=1,
  10. pad_mask=True,
  11. mask_pad_value=0,
  12. pad_seg=True,
  13. seg_pad_value=255)
  14. num_things_classes = 80
  15. num_stuff_classes = 53
  16. num_classes = num_things_classes + num_stuff_classes
  17. model = dict(
  18. type='MaskFormer',
  19. data_preprocessor=data_preprocessor,
  20. backbone=dict(
  21. type='ResNet',
  22. depth=50,
  23. num_stages=4,
  24. out_indices=(0, 1, 2, 3),
  25. frozen_stages=-1,
  26. norm_cfg=dict(type='BN', requires_grad=False),
  27. norm_eval=True,
  28. style='pytorch',
  29. init_cfg=dict(type='Pretrained', checkpoint='torchvision://resnet50')),
  30. panoptic_head=dict(
  31. type='MaskFormerHead',
  32. in_channels=[256, 512, 1024, 2048], # pass to pixel_decoder inside
  33. feat_channels=256,
  34. out_channels=256,
  35. num_things_classes=num_things_classes,
  36. num_stuff_classes=num_stuff_classes,
  37. num_queries=100,
  38. pixel_decoder=dict(
  39. type='TransformerEncoderPixelDecoder',
  40. norm_cfg=dict(type='GN', num_groups=32),
  41. act_cfg=dict(type='ReLU'),
  42. encoder=dict( # DetrTransformerEncoder
  43. num_layers=6,
  44. layer_cfg=dict( # DetrTransformerEncoderLayer
  45. self_attn_cfg=dict( # MultiheadAttention
  46. embed_dims=256,
  47. num_heads=8,
  48. dropout=0.1,
  49. batch_first=True),
  50. ffn_cfg=dict(
  51. embed_dims=256,
  52. feedforward_channels=2048,
  53. num_fcs=2,
  54. ffn_drop=0.1,
  55. act_cfg=dict(type='ReLU', inplace=True)))),
  56. positional_encoding=dict(num_feats=128, normalize=True)),
  57. enforce_decoder_input_project=False,
  58. positional_encoding=dict(num_feats=128, normalize=True),
  59. transformer_decoder=dict( # DetrTransformerDecoder
  60. num_layers=6,
  61. layer_cfg=dict( # DetrTransformerDecoderLayer
  62. self_attn_cfg=dict( # MultiheadAttention
  63. embed_dims=256,
  64. num_heads=8,
  65. dropout=0.1,
  66. batch_first=True),
  67. cross_attn_cfg=dict( # MultiheadAttention
  68. embed_dims=256,
  69. num_heads=8,
  70. dropout=0.1,
  71. batch_first=True),
  72. ffn_cfg=dict(
  73. embed_dims=256,
  74. feedforward_channels=2048,
  75. num_fcs=2,
  76. ffn_drop=0.1,
  77. act_cfg=dict(type='ReLU', inplace=True))),
  78. return_intermediate=True),
  79. loss_cls=dict(
  80. type='CrossEntropyLoss',
  81. use_sigmoid=False,
  82. loss_weight=1.0,
  83. reduction='mean',
  84. class_weight=[1.0] * num_classes + [0.1]),
  85. loss_mask=dict(
  86. type='FocalLoss',
  87. use_sigmoid=True,
  88. gamma=2.0,
  89. alpha=0.25,
  90. reduction='mean',
  91. loss_weight=20.0),
  92. loss_dice=dict(
  93. type='DiceLoss',
  94. use_sigmoid=True,
  95. activate=True,
  96. reduction='mean',
  97. naive_dice=True,
  98. eps=1.0,
  99. loss_weight=1.0)),
  100. panoptic_fusion_head=dict(
  101. type='MaskFormerFusionHead',
  102. num_things_classes=num_things_classes,
  103. num_stuff_classes=num_stuff_classes,
  104. loss_panoptic=None,
  105. init_cfg=None),
  106. train_cfg=dict(
  107. assigner=dict(
  108. type='HungarianAssigner',
  109. match_costs=[
  110. dict(type='ClassificationCost', weight=1.0),
  111. dict(type='FocalLossCost', weight=20.0, binary_input=True),
  112. dict(type='DiceCost', weight=1.0, pred_act=True, eps=1.0)
  113. ]),
  114. sampler=dict(type='MaskPseudoSampler')),
  115. test_cfg=dict(
  116. panoptic_on=True,
  117. # For now, the dataset does not support
  118. # evaluating semantic segmentation metric.
  119. semantic_on=False,
  120. instance_on=False,
  121. # max_per_image is for instance segmentation.
  122. max_per_image=100,
  123. object_mask_thr=0.8,
  124. iou_thr=0.8,
  125. # In MaskFormer's panoptic postprocessing,
  126. # it will not filter masks whose score is smaller than 0.5 .
  127. filter_low_score=False),
  128. init_cfg=None)
  129. # dataset settings
  130. train_pipeline = [
  131. dict(type='LoadImageFromFile'),
  132. dict(
  133. type='LoadPanopticAnnotations',
  134. with_bbox=True,
  135. with_mask=True,
  136. with_seg=True),
  137. dict(type='RandomFlip', prob=0.5),
  138. dict(
  139. type='RandomChoice',
  140. transforms=[[
  141. dict(
  142. type='RandomChoiceResize',
  143. scales=[(480, 1333), (512, 1333), (544, 1333), (576, 1333),
  144. (608, 1333), (640, 1333), (672, 1333), (704, 1333),
  145. (736, 1333), (768, 1333), (800, 1333)],
  146. keep_ratio=True)
  147. ],
  148. [
  149. dict(
  150. type='RandomChoiceResize',
  151. scales=[(400, 1333), (500, 1333), (600, 1333)],
  152. keep_ratio=True),
  153. dict(
  154. type='RandomCrop',
  155. crop_type='absolute_range',
  156. crop_size=(384, 600),
  157. allow_negative_crop=True),
  158. dict(
  159. type='RandomChoiceResize',
  160. scales=[(480, 1333), (512, 1333), (544, 1333),
  161. (576, 1333), (608, 1333), (640, 1333),
  162. (672, 1333), (704, 1333), (736, 1333),
  163. (768, 1333), (800, 1333)],
  164. keep_ratio=True)
  165. ]]),
  166. dict(type='PackDetInputs')
  167. ]
  168. train_dataloader = dict(
  169. batch_size=1, num_workers=1, dataset=dict(pipeline=train_pipeline))
  170. val_dataloader = dict(batch_size=1, num_workers=1)
  171. test_dataloader = val_dataloader
  172. # optimizer
  173. optim_wrapper = dict(
  174. type='OptimWrapper',
  175. optimizer=dict(
  176. type='AdamW',
  177. lr=0.0001,
  178. weight_decay=0.0001,
  179. eps=1e-8,
  180. betas=(0.9, 0.999)),
  181. paramwise_cfg=dict(
  182. custom_keys={
  183. 'backbone': dict(lr_mult=0.1, decay_mult=1.0),
  184. 'query_embed': dict(lr_mult=1.0, decay_mult=0.0)
  185. },
  186. norm_decay_mult=0.0),
  187. clip_grad=dict(max_norm=0.01, norm_type=2))
  188. max_epochs = 75
  189. # learning rate
  190. param_scheduler = dict(
  191. type='MultiStepLR',
  192. begin=0,
  193. end=max_epochs,
  194. by_epoch=True,
  195. milestones=[50],
  196. gamma=0.1)
  197. train_cfg = dict(
  198. type='EpochBasedTrainLoop', max_epochs=max_epochs, val_interval=1)
  199. val_cfg = dict(type='ValLoop')
  200. test_cfg = dict(type='TestLoop')
  201. # Default setting for scaling LR automatically
  202. # - `enable` means enable scaling LR automatically
  203. # or not by default.
  204. # - `base_batch_size` = (16 GPUs) x (1 samples per GPU).
  205. auto_scale_lr = dict(enable=False, base_batch_size=16)