You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_coder.py 5.8 kB

2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. # Copyright (c) OpenMMLab. All rights reserved.
  2. import pytest
  3. import torch
  4. from mmdet.core.bbox.coder import (DeltaXYWHBBoxCoder, DistancePointBBoxCoder,
  5. TBLRBBoxCoder, YOLOBBoxCoder)
  6. def test_yolo_bbox_coder():
  7. coder = YOLOBBoxCoder()
  8. bboxes = torch.Tensor([[-42., -29., 74., 61.], [-10., -29., 106., 61.],
  9. [22., -29., 138., 61.], [54., -29., 170., 61.]])
  10. pred_bboxes = torch.Tensor([[0.4709, 0.6152, 0.1690, -0.4056],
  11. [0.5399, 0.6653, 0.1162, -0.4162],
  12. [0.4654, 0.6618, 0.1548, -0.4301],
  13. [0.4786, 0.6197, 0.1896, -0.4479]])
  14. grid_size = 32
  15. expected_decode_bboxes = torch.Tensor(
  16. [[-53.6102, -10.3096, 83.7478, 49.6824],
  17. [-15.8700, -8.3901, 114.4236, 50.9693],
  18. [11.1822, -8.0924, 146.6034, 50.4476],
  19. [41.2068, -8.9232, 181.4236, 48.5840]])
  20. assert expected_decode_bboxes.allclose(
  21. coder.decode(bboxes, pred_bboxes, grid_size))
  22. def test_delta_bbox_coder():
  23. coder = DeltaXYWHBBoxCoder()
  24. rois = torch.Tensor([[0., 0., 1., 1.], [0., 0., 1., 1.], [0., 0., 1., 1.],
  25. [5., 5., 5., 5.]])
  26. deltas = torch.Tensor([[0., 0., 0., 0.], [1., 1., 1., 1.],
  27. [0., 0., 2., -1.], [0.7, -1.9, -0.5, 0.3]])
  28. expected_decode_bboxes = torch.Tensor([[0.0000, 0.0000, 1.0000, 1.0000],
  29. [0.1409, 0.1409, 2.8591, 2.8591],
  30. [0.0000, 0.3161, 4.1945, 0.6839],
  31. [5.0000, 5.0000, 5.0000, 5.0000]])
  32. out = coder.decode(rois, deltas, max_shape=(32, 32))
  33. assert expected_decode_bboxes.allclose(out, atol=1e-04)
  34. out = coder.decode(rois, deltas, max_shape=torch.Tensor((32, 32)))
  35. assert expected_decode_bboxes.allclose(out, atol=1e-04)
  36. batch_rois = rois.unsqueeze(0).repeat(2, 1, 1)
  37. batch_deltas = deltas.unsqueeze(0).repeat(2, 1, 1)
  38. batch_out = coder.decode(batch_rois, batch_deltas, max_shape=(32, 32))[0]
  39. assert out.allclose(batch_out)
  40. batch_out = coder.decode(
  41. batch_rois, batch_deltas, max_shape=[(32, 32), (32, 32)])[0]
  42. assert out.allclose(batch_out)
  43. # test max_shape is not equal to batch
  44. with pytest.raises(AssertionError):
  45. coder.decode(
  46. batch_rois, batch_deltas, max_shape=[(32, 32), (32, 32), (32, 32)])
  47. rois = torch.zeros((0, 4))
  48. deltas = torch.zeros((0, 4))
  49. out = coder.decode(rois, deltas, max_shape=(32, 32))
  50. assert rois.shape == out.shape
  51. # test add_ctr_clamp
  52. coder = DeltaXYWHBBoxCoder(add_ctr_clamp=True, ctr_clamp=2)
  53. rois = torch.Tensor([[0., 0., 6., 6.], [0., 0., 1., 1.], [0., 0., 1., 1.],
  54. [5., 5., 5., 5.]])
  55. deltas = torch.Tensor([[1., 1., 2., 2.], [1., 1., 1., 1.],
  56. [0., 0., 2., -1.], [0.7, -1.9, -0.5, 0.3]])
  57. expected_decode_bboxes = torch.Tensor([[0.0000, 0.0000, 27.1672, 27.1672],
  58. [0.1409, 0.1409, 2.8591, 2.8591],
  59. [0.0000, 0.3161, 4.1945, 0.6839],
  60. [5.0000, 5.0000, 5.0000, 5.0000]])
  61. out = coder.decode(rois, deltas, max_shape=(32, 32))
  62. assert expected_decode_bboxes.allclose(out, atol=1e-04)
  63. def test_tblr_bbox_coder():
  64. coder = TBLRBBoxCoder(normalizer=15.)
  65. rois = torch.Tensor([[0., 0., 1., 1.], [0., 0., 1., 1.], [0., 0., 1., 1.],
  66. [5., 5., 5., 5.]])
  67. deltas = torch.Tensor([[0., 0., 0., 0.], [1., 1., 1., 1.],
  68. [0., 0., 2., -1.], [0.7, -1.9, -0.5, 0.3]])
  69. expected_decode_bboxes = torch.Tensor([[0.5000, 0.5000, 0.5000, 0.5000],
  70. [0.0000, 0.0000, 12.0000, 13.0000],
  71. [0.0000, 0.5000, 0.0000, 0.5000],
  72. [5.0000, 5.0000, 5.0000, 5.0000]])
  73. out = coder.decode(rois, deltas, max_shape=(13, 12))
  74. assert expected_decode_bboxes.allclose(out)
  75. out = coder.decode(rois, deltas, max_shape=torch.Tensor((13, 12)))
  76. assert expected_decode_bboxes.allclose(out)
  77. batch_rois = rois.unsqueeze(0).repeat(2, 1, 1)
  78. batch_deltas = deltas.unsqueeze(0).repeat(2, 1, 1)
  79. batch_out = coder.decode(batch_rois, batch_deltas, max_shape=(13, 12))[0]
  80. assert out.allclose(batch_out)
  81. batch_out = coder.decode(
  82. batch_rois, batch_deltas, max_shape=[(13, 12), (13, 12)])[0]
  83. assert out.allclose(batch_out)
  84. # test max_shape is not equal to batch
  85. with pytest.raises(AssertionError):
  86. coder.decode(batch_rois, batch_deltas, max_shape=[(13, 12)])
  87. rois = torch.zeros((0, 4))
  88. deltas = torch.zeros((0, 4))
  89. out = coder.decode(rois, deltas, max_shape=(32, 32))
  90. assert rois.shape == out.shape
  91. def test_distance_point_bbox_coder():
  92. coder = DistancePointBBoxCoder()
  93. points = torch.Tensor([[74., 61.], [-29., 106.], [138., 61.], [29., 170.]])
  94. gt_bboxes = torch.Tensor([[74., 61., 75., 62.], [0., 104., 0., 112.],
  95. [100., 90., 100., 120.], [0., 120., 100., 120.]])
  96. expected_distance = torch.Tensor([[0., 0., 1., 1.], [0., 2., 29., 6.],
  97. [38., 0., 0., 50.], [29., 50., 50., 0.]])
  98. out_distance = coder.encode(points, gt_bboxes, max_dis=50, eps=0)
  99. assert expected_distance.allclose(out_distance)
  100. distance = torch.Tensor([[0., 0, 1., 1.], [1., 2., 10., 6.],
  101. [22., -29., 138., 61.], [54., -29., 170., 61.]])
  102. out_bbox = coder.decode(points, distance, max_shape=(120, 100))
  103. assert gt_bboxes.allclose(out_bbox)

No Description

Contributors (2)