test_sparse_roi_head.py 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. # Copyright (c) OpenMMLab. All rights reserved.
  2. import unittest
  3. from unittest import TestCase
  4. import torch
  5. import torch.nn as nn
  6. from parameterized import parameterized
  7. from mmdet.models.roi_heads import StandardRoIHead # noqa
  8. from mmdet.registry import MODELS
  9. from mmdet.testing import demo_mm_inputs, demo_mm_proposals, get_roi_head_cfg
  10. class TestCascadeRoIHead(TestCase):
  11. @parameterized.expand(['queryinst/queryinst_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. roi_head.init_weights()
  18. assert roi_head.with_bbox
  19. assert roi_head.with_mask
  20. @parameterized.expand(['queryinst/queryinst_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. # add import elements into proposal
  45. init_proposal_features = nn.Embedding(100, 256).cuda().weight.clone()
  46. for proposal in proposal_list:
  47. proposal.features = init_proposal_features
  48. proposal.imgs_whwh = feats[0].new_tensor([[s, s, s,
  49. s]]).repeat(100, 1)
  50. batch_data_samples = demo_mm_inputs(
  51. batch_size=1,
  52. image_shapes=[(3, s, s)],
  53. num_items=[1],
  54. num_classes=4,
  55. with_mask=True,
  56. device='cuda')['data_samples']
  57. out = roi_head.loss(feats, proposal_list, batch_data_samples)
  58. for name, value in out.items():
  59. if 'loss' in name:
  60. self.assertGreaterEqual(
  61. value.sum(), 0, msg='loss should be non-zero')
  62. # When there is no truth, the cls loss should be nonzero but
  63. # there should be no box and mask loss.
  64. proposal_list = demo_mm_proposals(img_shape_list, 100, device='cuda')
  65. # add import elements into proposal
  66. init_proposal_features = nn.Embedding(100, 256).cuda().weight.clone()
  67. for proposal in proposal_list:
  68. proposal.features = init_proposal_features
  69. proposal.imgs_whwh = feats[0].new_tensor([[s, s, s,
  70. s]]).repeat(100, 1)
  71. batch_data_samples = demo_mm_inputs(
  72. batch_size=1,
  73. image_shapes=[(3, s, s)],
  74. num_items=[0],
  75. num_classes=4,
  76. with_mask=True,
  77. device='cuda')['data_samples']
  78. out = roi_head.loss(feats, proposal_list, batch_data_samples)
  79. for name, value in out.items():
  80. if 'loss_cls' in name:
  81. self.assertGreaterEqual(
  82. value.sum(), 0, msg='loss should be non-zero')
  83. elif 'loss_bbox' in name or 'loss_mask' in name:
  84. self.assertEqual(value.sum(), 0)