test_mask_structures.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. from unittest import TestCase
  2. import numpy as np
  3. from mmengine.testing import assert_allclose
  4. from mmdet.structures.mask import BitmapMasks, PolygonMasks
  5. class TestMaskStructures(TestCase):
  6. def test_bitmap_translate_same_size(self):
  7. mask_array = np.zeros((5, 10, 10), dtype=np.uint8)
  8. mask_array[:, 0:5, 0:5] = 1
  9. mask_target = np.zeros((5, 10, 10), dtype=np.uint8)
  10. mask_target[:, 0:5, 5:10] = 1
  11. mask = BitmapMasks(mask_array, 10, 10)
  12. mask = mask.translate((10, 10), 5)
  13. assert mask.masks.shape == (5, 10, 10)
  14. assert_allclose(mask_target, mask.masks)
  15. def test_bitmap_translate_diff_size(self):
  16. # test out shape larger
  17. mask_array = np.zeros((5, 10, 10), dtype=np.uint8)
  18. mask_array[:, 0:5, 0:5] = 1
  19. mask_target = np.zeros((5, 20, 20), dtype=np.uint8)
  20. mask_target[:, 0:5, 5:10] = 1
  21. mask = BitmapMasks(mask_array, 10, 10)
  22. mask = mask.translate((20, 20), 5)
  23. assert mask.masks.shape == (5, 20, 20)
  24. assert_allclose(mask_target, mask.masks)
  25. # test out shape smaller
  26. mask_array = np.zeros((5, 10, 10), dtype=np.uint8)
  27. mask_array[:, 0:5, 0:5] = 1
  28. mask_target = np.zeros((5, 20, 8), dtype=np.uint8)
  29. mask_target[:, 0:5, 5:] = 1
  30. mask = BitmapMasks(mask_array, 10, 10)
  31. mask = mask.translate((20, 8), 5)
  32. assert mask.masks.shape == (5, 20, 8)
  33. assert_allclose(mask_target, mask.masks)
  34. def test_bitmap_cat(self):
  35. # test invalid inputs
  36. with self.assertRaises(AssertionError):
  37. BitmapMasks.cat(BitmapMasks.random(4))
  38. with self.assertRaises(ValueError):
  39. BitmapMasks.cat([])
  40. with self.assertRaises(AssertionError):
  41. BitmapMasks.cat([BitmapMasks.random(2), PolygonMasks.random(3)])
  42. masks = [BitmapMasks.random(num_masks=3) for _ in range(5)]
  43. cat_mask = BitmapMasks.cat(masks)
  44. assert len(cat_mask) == 3 * 5
  45. for i, m in enumerate(masks):
  46. assert_allclose(m.masks, cat_mask.masks[i * 3:(i + 1) * 3])
  47. def test_polygon_cat(self):
  48. # test invalid inputs
  49. with self.assertRaises(AssertionError):
  50. PolygonMasks.cat(PolygonMasks.random(4))
  51. with self.assertRaises(ValueError):
  52. PolygonMasks.cat([])
  53. with self.assertRaises(AssertionError):
  54. PolygonMasks.cat([BitmapMasks.random(2), PolygonMasks.random(3)])
  55. masks = [PolygonMasks.random(num_masks=3) for _ in range(5)]
  56. cat_mask = PolygonMasks.cat(masks)
  57. assert len(cat_mask) == 3 * 5
  58. for i, m in enumerate(masks):
  59. assert_allclose(m.masks, cat_mask.masks[i * 3:(i + 1) * 3])