test_scnet_roi_head.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. # Copyright (c) OpenMMLab. All rights reserved.
  2. import unittest
  3. from unittest import TestCase
  4. import torch
  5. from parameterized import parameterized
  6. from mmdet.models.roi_heads import SCNetRoIHead # noqa
  7. from mmdet.registry import MODELS
  8. from mmdet.testing import demo_mm_inputs, demo_mm_proposals, get_roi_head_cfg
  9. class TestSCNetRoIHead(TestCase):
  10. @parameterized.expand(['scnet/scnet_r50_fpn_1x_coco.py'])
  11. def test_init(self, cfg_file):
  12. """Test init scnet RoI head."""
  13. # Normal Cascade Mask R-CNN RoI head
  14. roi_head_cfg = get_roi_head_cfg(cfg_file)
  15. roi_head = MODELS.build(roi_head_cfg)
  16. assert roi_head.with_bbox
  17. assert roi_head.with_mask
  18. assert roi_head.with_semantic
  19. assert roi_head.with_feat_relay
  20. assert roi_head.with_glbctx
  21. @parameterized.expand(['scnet/scnet_r50_fpn_1x_coco.py'])
  22. def test_scnet_roi_head_loss(self, cfg_file):
  23. """Tests htc roi head loss when truth is empty and non-empty."""
  24. if not torch.cuda.is_available():
  25. # RoI pooling only support in GPU
  26. return unittest.skip('test requires GPU and torch+cuda')
  27. s = 256
  28. img_metas = [{
  29. 'img_shape': (s, s, 3),
  30. 'scale_factor': 1,
  31. }]
  32. roi_head_cfg = get_roi_head_cfg(cfg_file)
  33. roi_head = MODELS.build(roi_head_cfg)
  34. roi_head = roi_head.cuda()
  35. feats = []
  36. for i in range(len(roi_head_cfg.bbox_roi_extractor.featmap_strides)):
  37. feats.append(
  38. torch.rand(1, 256, s // (2**(i + 2)),
  39. s // (2**(i + 2))).to(device='cuda'))
  40. feats = tuple(feats)
  41. # When truth is non-empty then both cls, box, and mask loss
  42. # should be nonzero for random inputs
  43. img_shape_list = [(3, s, s) for _ in img_metas]
  44. proposal_list = demo_mm_proposals(img_shape_list, 100, device='cuda')
  45. batch_data_samples = demo_mm_inputs(
  46. batch_size=1,
  47. image_shapes=[(3, s, s)],
  48. num_items=[1],
  49. num_classes=4,
  50. with_mask=True,
  51. with_semantic=True,
  52. device='cuda')['data_samples']
  53. out = roi_head.loss(feats, proposal_list, batch_data_samples)
  54. for name, value in out.items():
  55. if 'loss' in name:
  56. self.assertGreaterEqual(
  57. value.sum(), 0, msg='loss should be non-zero')
  58. # When there is no truth, the cls loss should be nonzero but
  59. # there should be no box and mask loss.
  60. proposal_list = demo_mm_proposals(img_shape_list, 100, device='cuda')
  61. batch_data_samples = demo_mm_inputs(
  62. batch_size=1,
  63. image_shapes=[(3, s, s)],
  64. num_items=[0],
  65. num_classes=4,
  66. with_mask=True,
  67. with_semantic=True,
  68. device='cuda')['data_samples']
  69. out = roi_head.loss(feats, proposal_list, batch_data_samples)
  70. for name, value in out.items():
  71. if 'loss_cls' in name:
  72. self.assertGreaterEqual(
  73. value.sum(), 0, msg='loss should be non-zero')
  74. elif 'loss_bbox' in name or 'loss_mask' in name:
  75. self.assertEqual(value.sum(), 0)
  76. @parameterized.expand(['scnet/scnet_r50_fpn_1x_coco.py'])
  77. def test_scnet_roi_head_predict(self, cfg_file):
  78. if not torch.cuda.is_available():
  79. # RoI pooling only support in GPU
  80. return unittest.skip('test requires GPU and torch+cuda')
  81. s = 256
  82. img_metas = [{
  83. 'img_shape': (s, s, 3),
  84. 'scale_factor': 1,
  85. }]
  86. roi_head_cfg = get_roi_head_cfg(cfg_file)
  87. roi_head = MODELS.build(roi_head_cfg)
  88. roi_head = roi_head.cuda()
  89. feats = []
  90. for i in range(len(roi_head_cfg.bbox_roi_extractor.featmap_strides)):
  91. feats.append(
  92. torch.rand(1, 256, s // (2**(i + 2)),
  93. s // (2**(i + 2))).to(device='cuda'))
  94. feats = tuple(feats)
  95. img_shape_list = [(3, s, s) for _ in img_metas]
  96. proposal_list = demo_mm_proposals(img_shape_list, 100, device='cuda')
  97. batch_data_samples = demo_mm_inputs(
  98. batch_size=1,
  99. image_shapes=[(3, s, s)],
  100. num_items=[1],
  101. num_classes=4,
  102. with_mask=True,
  103. device='cuda')['data_samples']
  104. results = roi_head.predict(
  105. feats, proposal_list, batch_data_samples, rescale=True)
  106. self.assertEqual(results[0].masks.shape[-2:], (s, s))