test_htc_roi_head.py 4.4 KB

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