test_cascade_roi_head.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 StandardRoIHead # noqa
  7. from mmdet.registry import MODELS
  8. from mmdet.testing import demo_mm_inputs, demo_mm_proposals, get_roi_head_cfg
  9. class TestCascadeRoIHead(TestCase):
  10. @parameterized.expand(
  11. ['cascade_rcnn/cascade-mask-rcnn_r50_fpn_1x_coco.py'])
  12. def test_init(self, cfg_file):
  13. """Test init standard RoI head."""
  14. # Normal Cascade Mask R-CNN RoI head
  15. roi_head_cfg = get_roi_head_cfg(cfg_file)
  16. roi_head = MODELS.build(roi_head_cfg)
  17. assert roi_head.with_bbox
  18. assert roi_head.with_mask
  19. @parameterized.expand(
  20. ['cascade_rcnn/cascade-mask-rcnn_r50_fpn_1x_coco.py'])
  21. def test_cascade_roi_head_loss(self, cfg_file):
  22. """Tests standard roi head loss when truth is empty and non-empty."""
  23. if not torch.cuda.is_available():
  24. # RoI pooling only support in GPU
  25. return unittest.skip('test requires GPU and torch+cuda')
  26. s = 256
  27. img_metas = [{
  28. 'img_shape': (s, s, 3),
  29. 'scale_factor': 1,
  30. }]
  31. roi_head_cfg = get_roi_head_cfg(cfg_file)
  32. roi_head = MODELS.build(roi_head_cfg)
  33. roi_head = roi_head.cuda()
  34. feats = []
  35. for i in range(len(roi_head_cfg.bbox_roi_extractor.featmap_strides)):
  36. feats.append(
  37. torch.rand(1, 1, s // (2**(i + 2)),
  38. s // (2**(i + 2))).to(device='cuda'))
  39. feats = tuple(feats)
  40. # When truth is non-empty then both cls, box, and mask loss
  41. # should be nonzero for random inputs
  42. img_shape_list = [(3, s, s) for _ in img_metas]
  43. proposal_list = demo_mm_proposals(img_shape_list, 100, device='cuda')
  44. batch_data_samples = demo_mm_inputs(
  45. batch_size=1,
  46. image_shapes=[(3, s, s)],
  47. num_items=[1],
  48. num_classes=4,
  49. with_mask=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. device='cuda')['data_samples']
  66. out = roi_head.loss(feats, proposal_list, batch_data_samples)
  67. for name, value in out.items():
  68. if 'loss_cls' in name:
  69. self.assertGreaterEqual(
  70. value.sum(), 0, msg='loss should be non-zero')
  71. elif 'loss_bbox' in name or 'loss_mask' in name:
  72. self.assertEqual(value.sum(), 0)