test_maskformer.py 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. # Copyright (c) OpenMMLab. All rights reserved.
  2. import unittest
  3. import torch
  4. from parameterized import parameterized
  5. from mmdet.registry import MODELS
  6. from mmdet.structures import DetDataSample
  7. from mmdet.testing._utils import demo_mm_inputs, get_detector_cfg
  8. from mmdet.utils import register_all_modules
  9. class TestMaskFormer(unittest.TestCase):
  10. def setUp(self):
  11. register_all_modules()
  12. def _create_model_cfg(self):
  13. cfg_path = 'maskformer/maskformer_r50_ms-16xb1-75e_coco.py'
  14. model_cfg = get_detector_cfg(cfg_path)
  15. base_channels = 32
  16. model_cfg.backbone.depth = 18
  17. model_cfg.backbone.init_cfg = None
  18. model_cfg.backbone.base_channels = base_channels
  19. model_cfg.panoptic_head.in_channels = [
  20. base_channels * 2**i for i in range(4)
  21. ]
  22. model_cfg.panoptic_head.feat_channels = base_channels
  23. model_cfg.panoptic_head.out_channels = base_channels
  24. model_cfg.panoptic_head.pixel_decoder.encoder.\
  25. layer_cfg.self_attn_cfg.embed_dims = base_channels
  26. model_cfg.panoptic_head.pixel_decoder.encoder.\
  27. layer_cfg.ffn_cfg.embed_dims = base_channels
  28. model_cfg.panoptic_head.pixel_decoder.encoder.\
  29. layer_cfg.ffn_cfg.feedforward_channels = base_channels * 8
  30. model_cfg.panoptic_head.pixel_decoder.\
  31. positional_encoding.num_feats = base_channels // 2
  32. model_cfg.panoptic_head.positional_encoding.\
  33. num_feats = base_channels // 2
  34. model_cfg.panoptic_head.transformer_decoder.\
  35. layer_cfg.self_attn_cfg.embed_dims = base_channels
  36. model_cfg.panoptic_head.transformer_decoder. \
  37. layer_cfg.cross_attn_cfg.embed_dims = base_channels
  38. model_cfg.panoptic_head.transformer_decoder.\
  39. layer_cfg.ffn_cfg.embed_dims = base_channels
  40. model_cfg.panoptic_head.transformer_decoder.\
  41. layer_cfg.ffn_cfg.feedforward_channels = base_channels * 8
  42. return model_cfg
  43. def test_init(self):
  44. model_cfg = self._create_model_cfg()
  45. detector = MODELS.build(model_cfg)
  46. detector.init_weights()
  47. assert detector.backbone
  48. assert detector.panoptic_head
  49. @parameterized.expand([('cpu', ), ('cuda', )])
  50. def test_forward_loss_mode(self, device):
  51. model_cfg = self._create_model_cfg()
  52. detector = MODELS.build(model_cfg)
  53. if device == 'cuda' and not torch.cuda.is_available():
  54. return unittest.skip('test requires GPU and torch+cuda')
  55. detector = detector.to(device)
  56. packed_inputs = demo_mm_inputs(
  57. 2,
  58. image_shapes=[(3, 128, 127), (3, 91, 92)],
  59. sem_seg_output_strides=1,
  60. with_mask=True,
  61. with_semantic=True)
  62. data = detector.data_preprocessor(packed_inputs, True)
  63. # Test loss mode
  64. losses = detector.forward(**data, mode='loss')
  65. self.assertIsInstance(losses, dict)
  66. @parameterized.expand([('cpu', ), ('cuda', )])
  67. def test_forward_predict_mode(self, device):
  68. model_cfg = self._create_model_cfg()
  69. detector = MODELS.build(model_cfg)
  70. if device == 'cuda' and not torch.cuda.is_available():
  71. return unittest.skip('test requires GPU and torch+cuda')
  72. detector = detector.to(device)
  73. packed_inputs = demo_mm_inputs(
  74. 2,
  75. image_shapes=[(3, 128, 127), (3, 91, 92)],
  76. sem_seg_output_strides=1,
  77. with_mask=True,
  78. with_semantic=True)
  79. data = detector.data_preprocessor(packed_inputs, False)
  80. # Test forward test
  81. detector.eval()
  82. with torch.no_grad():
  83. batch_results = detector.forward(**data, mode='predict')
  84. self.assertEqual(len(batch_results), 2)
  85. self.assertIsInstance(batch_results[0], DetDataSample)
  86. @parameterized.expand([('cpu', ), ('cuda', )])
  87. def test_forward_tensor_mode(self, device):
  88. model_cfg = self._create_model_cfg()
  89. detector = MODELS.build(model_cfg)
  90. if device == 'cuda' and not torch.cuda.is_available():
  91. return unittest.skip('test requires GPU and torch+cuda')
  92. detector = detector.to(device)
  93. packed_inputs = demo_mm_inputs(
  94. 2, [[3, 128, 128], [3, 125, 130]],
  95. sem_seg_output_strides=1,
  96. with_mask=True,
  97. with_semantic=True)
  98. data = detector.data_preprocessor(packed_inputs, False)
  99. out = detector.forward(**data, mode='tensor')
  100. self.assertIsInstance(out, tuple)
  101. class TestMask2Former(unittest.TestCase):
  102. def setUp(self):
  103. register_all_modules()
  104. def _create_model_cfg(self, cfg_path):
  105. model_cfg = get_detector_cfg(cfg_path)
  106. base_channels = 32
  107. model_cfg.backbone.depth = 18
  108. model_cfg.backbone.init_cfg = None
  109. model_cfg.backbone.base_channels = base_channels
  110. model_cfg.panoptic_head.in_channels = [
  111. base_channels * 2**i for i in range(4)
  112. ]
  113. model_cfg.panoptic_head.feat_channels = base_channels
  114. model_cfg.panoptic_head.out_channels = base_channels
  115. model_cfg.panoptic_head.pixel_decoder.encoder.\
  116. layer_cfg.self_attn_cfg.embed_dims = base_channels
  117. model_cfg.panoptic_head.pixel_decoder.encoder.\
  118. layer_cfg.ffn_cfg.embed_dims = base_channels
  119. model_cfg.panoptic_head.pixel_decoder.encoder.\
  120. layer_cfg.ffn_cfg.feedforward_channels = base_channels * 4
  121. model_cfg.panoptic_head.pixel_decoder.\
  122. positional_encoding.num_feats = base_channels // 2
  123. model_cfg.panoptic_head.positional_encoding.\
  124. num_feats = base_channels // 2
  125. model_cfg.panoptic_head.transformer_decoder.\
  126. layer_cfg.self_attn_cfg.embed_dims = base_channels
  127. model_cfg.panoptic_head.transformer_decoder. \
  128. layer_cfg.cross_attn_cfg.embed_dims = base_channels
  129. model_cfg.panoptic_head.transformer_decoder.\
  130. layer_cfg.ffn_cfg.embed_dims = base_channels
  131. model_cfg.panoptic_head.transformer_decoder.\
  132. layer_cfg.ffn_cfg.feedforward_channels = base_channels * 8
  133. return model_cfg
  134. def test_init(self):
  135. model_cfg = self._create_model_cfg(
  136. 'mask2former/mask2former_r50_8xb2-lsj-50e_coco-panoptic.py')
  137. detector = MODELS.build(model_cfg)
  138. detector.init_weights()
  139. assert detector.backbone
  140. assert detector.panoptic_head
  141. @parameterized.expand([
  142. ('cpu', 'mask2former/mask2former_r50_8xb2-lsj-50e_coco-panoptic.py'),
  143. ('cpu', 'mask2former/mask2former_r50_8xb2-lsj-50e_coco.py'),
  144. ('cuda', 'mask2former/mask2former_r50_8xb2-lsj-50e_coco-panoptic.py'),
  145. ('cuda', 'mask2former/mask2former_r50_8xb2-lsj-50e_coco.py')
  146. ])
  147. def test_forward_loss_mode(self, device, cfg_path):
  148. print(device, cfg_path)
  149. with_semantic = 'panoptic' in cfg_path
  150. model_cfg = self._create_model_cfg(cfg_path)
  151. detector = MODELS.build(model_cfg)
  152. if device == 'cuda' and not torch.cuda.is_available():
  153. return unittest.skip('test requires GPU and torch+cuda')
  154. detector = detector.to(device)
  155. packed_inputs = demo_mm_inputs(
  156. 2,
  157. image_shapes=[(3, 128, 127), (3, 91, 92)],
  158. sem_seg_output_strides=1,
  159. with_mask=True,
  160. with_semantic=with_semantic)
  161. data = detector.data_preprocessor(packed_inputs, True)
  162. # Test loss mode
  163. losses = detector.forward(**data, mode='loss')
  164. self.assertIsInstance(losses, dict)
  165. @parameterized.expand([
  166. ('cpu', 'mask2former/mask2former_r50_8xb2-lsj-50e_coco-panoptic.py'),
  167. ('cpu', 'mask2former/mask2former_r50_8xb2-lsj-50e_coco.py'),
  168. ('cuda', 'mask2former/mask2former_r50_8xb2-lsj-50e_coco-panoptic.py'),
  169. ('cuda', 'mask2former/mask2former_r50_8xb2-lsj-50e_coco.py')
  170. ])
  171. def test_forward_predict_mode(self, device, cfg_path):
  172. with_semantic = 'panoptic' in cfg_path
  173. model_cfg = self._create_model_cfg(cfg_path)
  174. detector = MODELS.build(model_cfg)
  175. if device == 'cuda' and not torch.cuda.is_available():
  176. return unittest.skip('test requires GPU and torch+cuda')
  177. detector = detector.to(device)
  178. packed_inputs = demo_mm_inputs(
  179. 2,
  180. image_shapes=[(3, 128, 127), (3, 91, 92)],
  181. sem_seg_output_strides=1,
  182. with_mask=True,
  183. with_semantic=with_semantic)
  184. data = detector.data_preprocessor(packed_inputs, False)
  185. # Test forward test
  186. detector.eval()
  187. with torch.no_grad():
  188. batch_results = detector.forward(**data, mode='predict')
  189. self.assertEqual(len(batch_results), 2)
  190. self.assertIsInstance(batch_results[0], DetDataSample)
  191. @parameterized.expand([
  192. ('cpu', 'mask2former/mask2former_r50_8xb2-lsj-50e_coco-panoptic.py'),
  193. ('cpu', 'mask2former/mask2former_r50_8xb2-lsj-50e_coco.py'),
  194. ('cuda', 'mask2former/mask2former_r50_8xb2-lsj-50e_coco-panoptic.py'),
  195. ('cuda', 'mask2former/mask2former_r50_8xb2-lsj-50e_coco.py')
  196. ])
  197. def test_forward_tensor_mode(self, device, cfg_path):
  198. with_semantic = 'panoptic' in cfg_path
  199. model_cfg = self._create_model_cfg(cfg_path)
  200. detector = MODELS.build(model_cfg)
  201. if device == 'cuda' and not torch.cuda.is_available():
  202. return unittest.skip('test requires GPU and torch+cuda')
  203. detector = detector.to(device)
  204. packed_inputs = demo_mm_inputs(
  205. 2, [[3, 128, 128], [3, 125, 130]],
  206. sem_seg_output_strides=1,
  207. with_mask=True,
  208. with_semantic=with_semantic)
  209. data = detector.data_preprocessor(packed_inputs, False)
  210. out = detector.forward(**data, mode='tensor')
  211. self.assertIsInstance(out, tuple)