test_fcos_head.py 4.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. # Copyright (c) OpenMMLab. All rights reserved.
  2. from unittest import TestCase
  3. import torch
  4. from mmengine.structures import InstanceData
  5. from mmdet.models.dense_heads import FCOSHead
  6. class TestFCOSHead(TestCase):
  7. def test_fcos_head_loss(self):
  8. """Tests fcos head loss when truth is empty and non-empty."""
  9. s = 256
  10. img_metas = [{
  11. 'img_shape': (s, s, 3),
  12. 'pad_shape': (s, s, 3),
  13. 'scale_factor': 1,
  14. }]
  15. fcos_head = FCOSHead(
  16. num_classes=4,
  17. in_channels=1,
  18. feat_channels=1,
  19. stacked_convs=1,
  20. norm_cfg=None)
  21. # Fcos head expects a multiple levels of features per image
  22. feats = (
  23. torch.rand(1, 1, s // stride[1], s // stride[0])
  24. for stride in fcos_head.prior_generator.strides)
  25. cls_scores, bbox_preds, centernesses = fcos_head.forward(feats)
  26. # Test that empty ground truth encourages the network to
  27. # predict background
  28. gt_instances = InstanceData()
  29. gt_instances.bboxes = torch.empty((0, 4))
  30. gt_instances.labels = torch.LongTensor([])
  31. empty_gt_losses = fcos_head.loss_by_feat(cls_scores, bbox_preds,
  32. centernesses, [gt_instances],
  33. img_metas)
  34. # When there is no truth, the cls loss should be nonzero but
  35. # box loss and centerness loss should be zero
  36. empty_cls_loss = empty_gt_losses['loss_cls'].item()
  37. empty_box_loss = empty_gt_losses['loss_bbox'].item()
  38. empty_ctr_loss = empty_gt_losses['loss_centerness'].item()
  39. self.assertGreater(empty_cls_loss, 0, 'cls loss should be non-zero')
  40. self.assertEqual(
  41. empty_box_loss, 0,
  42. 'there should be no box loss when there are no true boxes')
  43. self.assertEqual(
  44. empty_ctr_loss, 0,
  45. 'there should be no centerness loss when there are no true boxes')
  46. # When truth is non-empty then all cls, box loss and centerness loss
  47. # should be nonzero for random inputs
  48. gt_instances = InstanceData()
  49. gt_instances.bboxes = torch.Tensor(
  50. [[23.6667, 23.8757, 238.6326, 151.8874]])
  51. gt_instances.labels = torch.LongTensor([2])
  52. one_gt_losses = fcos_head.loss_by_feat(cls_scores, bbox_preds,
  53. centernesses, [gt_instances],
  54. img_metas)
  55. onegt_cls_loss = one_gt_losses['loss_cls'].item()
  56. onegt_box_loss = one_gt_losses['loss_bbox'].item()
  57. onegt_ctr_loss = one_gt_losses['loss_centerness'].item()
  58. self.assertGreater(onegt_cls_loss, 0, 'cls loss should be non-zero')
  59. self.assertGreater(onegt_box_loss, 0, 'box loss should be non-zero')
  60. self.assertGreater(onegt_ctr_loss, 0,
  61. 'centerness loss should be non-zero')
  62. # Test the `center_sampling` works fine.
  63. fcos_head.center_sampling = True
  64. ctrsamp_losses = fcos_head.loss_by_feat(cls_scores, bbox_preds,
  65. centernesses, [gt_instances],
  66. img_metas)
  67. ctrsamp_cls_loss = ctrsamp_losses['loss_cls'].item()
  68. ctrsamp_box_loss = ctrsamp_losses['loss_bbox'].item()
  69. ctrsamp_ctr_loss = ctrsamp_losses['loss_centerness'].item()
  70. self.assertGreater(ctrsamp_cls_loss, 0, 'cls loss should be non-zero')
  71. self.assertGreater(ctrsamp_box_loss, 0, 'box loss should be non-zero')
  72. self.assertGreater(ctrsamp_ctr_loss, 0,
  73. 'centerness loss should be non-zero')
  74. # Test the `norm_on_bbox` works fine.
  75. fcos_head.norm_on_bbox = True
  76. normbox_losses = fcos_head.loss_by_feat(cls_scores, bbox_preds,
  77. centernesses, [gt_instances],
  78. img_metas)
  79. normbox_cls_loss = normbox_losses['loss_cls'].item()
  80. normbox_box_loss = normbox_losses['loss_bbox'].item()
  81. normbox_ctr_loss = normbox_losses['loss_centerness'].item()
  82. self.assertGreater(normbox_cls_loss, 0, 'cls loss should be non-zero')
  83. self.assertGreater(normbox_box_loss, 0, 'box loss should be non-zero')
  84. self.assertGreater(normbox_ctr_loss, 0,
  85. 'centerness loss should be non-zero')