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_masks.py 28 kB

2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  1. # Copyright (c) OpenMMLab. All rights reserved.
  2. import numpy as np
  3. import pytest
  4. import torch
  5. from mmdet.core import BitmapMasks, PolygonMasks
  6. def dummy_raw_bitmap_masks(size):
  7. """
  8. Args:
  9. size (tuple): expected shape of dummy masks, (H, W) or (N, H, W)
  10. Return:
  11. ndarray: dummy mask
  12. """
  13. return np.random.randint(0, 2, size, dtype=np.uint8)
  14. def dummy_raw_polygon_masks(size):
  15. """
  16. Args:
  17. size (tuple): expected shape of dummy masks, (N, H, W)
  18. Return:
  19. list[list[ndarray]]: dummy mask
  20. """
  21. num_obj, height, width = size
  22. polygons = []
  23. for _ in range(num_obj):
  24. num_points = np.random.randint(5) * 2 + 6
  25. polygons.append([np.random.uniform(0, min(height, width), num_points)])
  26. return polygons
  27. def dummy_bboxes(num, max_height, max_width):
  28. x1y1 = np.random.randint(0, min(max_height // 2, max_width // 2), (num, 2))
  29. wh = np.random.randint(0, min(max_height // 2, max_width // 2), (num, 2))
  30. x2y2 = x1y1 + wh
  31. return np.concatenate([x1y1, x2y2], axis=1).squeeze().astype(np.float32)
  32. def test_bitmap_mask_init():
  33. # init with empty ndarray masks
  34. raw_masks = np.empty((0, 28, 28), dtype=np.uint8)
  35. bitmap_masks = BitmapMasks(raw_masks, 28, 28)
  36. assert len(bitmap_masks) == 0
  37. assert bitmap_masks.height == 28
  38. assert bitmap_masks.width == 28
  39. # init with empty list masks
  40. raw_masks = []
  41. bitmap_masks = BitmapMasks(raw_masks, 28, 28)
  42. assert len(bitmap_masks) == 0
  43. assert bitmap_masks.height == 28
  44. assert bitmap_masks.width == 28
  45. # init with ndarray masks contain 3 instances
  46. raw_masks = dummy_raw_bitmap_masks((3, 28, 28))
  47. bitmap_masks = BitmapMasks(raw_masks, 28, 28)
  48. assert len(bitmap_masks) == 3
  49. assert bitmap_masks.height == 28
  50. assert bitmap_masks.width == 28
  51. # init with list masks contain 3 instances
  52. raw_masks = [dummy_raw_bitmap_masks((28, 28)) for _ in range(3)]
  53. bitmap_masks = BitmapMasks(raw_masks, 28, 28)
  54. assert len(bitmap_masks) == 3
  55. assert bitmap_masks.height == 28
  56. assert bitmap_masks.width == 28
  57. # init with raw masks of unsupported type
  58. with pytest.raises(AssertionError):
  59. raw_masks = [[dummy_raw_bitmap_masks((28, 28))]]
  60. BitmapMasks(raw_masks, 28, 28)
  61. def test_bitmap_mask_rescale():
  62. # rescale with empty bitmap masks
  63. raw_masks = dummy_raw_bitmap_masks((0, 28, 28))
  64. bitmap_masks = BitmapMasks(raw_masks, 28, 28)
  65. rescaled_masks = bitmap_masks.rescale((56, 72))
  66. assert len(rescaled_masks) == 0
  67. assert rescaled_masks.height == 56
  68. assert rescaled_masks.width == 56
  69. # rescale with bitmap masks contain 1 instances
  70. raw_masks = np.array([[[1, 0, 0, 0], [0, 1, 0, 1]]])
  71. bitmap_masks = BitmapMasks(raw_masks, 2, 4)
  72. rescaled_masks = bitmap_masks.rescale((8, 8))
  73. assert len(rescaled_masks) == 1
  74. assert rescaled_masks.height == 4
  75. assert rescaled_masks.width == 8
  76. truth = np.array([[[1, 1, 0, 0, 0, 0, 0, 0], [1, 1, 0, 0, 0, 0, 0, 0],
  77. [0, 0, 1, 1, 0, 0, 1, 1], [0, 0, 1, 1, 0, 0, 1, 1]]])
  78. assert (rescaled_masks.masks == truth).all()
  79. def test_bitmap_mask_resize():
  80. # resize with empty bitmap masks
  81. raw_masks = dummy_raw_bitmap_masks((0, 28, 28))
  82. bitmap_masks = BitmapMasks(raw_masks, 28, 28)
  83. resized_masks = bitmap_masks.resize((56, 72))
  84. assert len(resized_masks) == 0
  85. assert resized_masks.height == 56
  86. assert resized_masks.width == 72
  87. # resize with bitmap masks contain 1 instances
  88. raw_masks = np.diag(np.ones(4, dtype=np.uint8))[np.newaxis, ...]
  89. bitmap_masks = BitmapMasks(raw_masks, 4, 4)
  90. resized_masks = bitmap_masks.resize((8, 8))
  91. assert len(resized_masks) == 1
  92. assert resized_masks.height == 8
  93. assert resized_masks.width == 8
  94. truth = np.array([[[1, 1, 0, 0, 0, 0, 0, 0], [1, 1, 0, 0, 0, 0, 0, 0],
  95. [0, 0, 1, 1, 0, 0, 0, 0], [0, 0, 1, 1, 0, 0, 0, 0],
  96. [0, 0, 0, 0, 1, 1, 0, 0], [0, 0, 0, 0, 1, 1, 0, 0],
  97. [0, 0, 0, 0, 0, 0, 1, 1], [0, 0, 0, 0, 0, 0, 1, 1]]])
  98. assert (resized_masks.masks == truth).all()
  99. # resize to non-square
  100. raw_masks = np.diag(np.ones(4, dtype=np.uint8))[np.newaxis, ...]
  101. bitmap_masks = BitmapMasks(raw_masks, 4, 4)
  102. resized_masks = bitmap_masks.resize((4, 8))
  103. assert len(resized_masks) == 1
  104. assert resized_masks.height == 4
  105. assert resized_masks.width == 8
  106. truth = np.array([[[1, 1, 0, 0, 0, 0, 0, 0], [0, 0, 1, 1, 0, 0, 0, 0],
  107. [0, 0, 0, 0, 1, 1, 0, 0], [0, 0, 0, 0, 0, 0, 1, 1]]])
  108. assert (resized_masks.masks == truth).all()
  109. def test_bitmap_mask_get_bboxes():
  110. # resize with empty bitmap masks
  111. raw_masks = dummy_raw_bitmap_masks((0, 28, 28))
  112. bitmap_masks = BitmapMasks(raw_masks, 28, 28)
  113. bboxes = bitmap_masks.get_bboxes()
  114. assert len(bboxes) == 0
  115. # resize with bitmap masks contain 1 instances
  116. raw_masks = np.array([[[0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 1, 1, 0, 0, 0, 0],
  117. [0, 0, 1, 1, 0, 0, 0, 0], [0, 0, 1, 1, 1, 0, 0, 0],
  118. [0, 0, 1, 1, 1, 1, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0],
  119. [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0,
  120. 0]]])
  121. bitmap_masks = BitmapMasks(raw_masks, 8, 8)
  122. bboxes = bitmap_masks.get_bboxes()
  123. assert len(bboxes) == 1
  124. truth = np.array([[1, 1, 6, 6]])
  125. assert (bboxes == truth).all()
  126. # resize to non-square
  127. raw_masks = np.array([[[1, 1, 0, 0, 0, 0, 0, 0], [0, 0, 1, 1, 0, 0, 0, 0],
  128. [0, 0, 0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0,
  129. 0]]])
  130. bitmap_masks = BitmapMasks(raw_masks, 4, 8)
  131. bboxes = bitmap_masks.get_bboxes()
  132. truth = np.array([[0, 0, 6, 3]])
  133. assert (bboxes == truth).all()
  134. def test_bitmap_mask_flip():
  135. # flip with empty bitmap masks
  136. raw_masks = dummy_raw_bitmap_masks((0, 28, 28))
  137. bitmap_masks = BitmapMasks(raw_masks, 28, 28)
  138. flipped_masks = bitmap_masks.flip(flip_direction='horizontal')
  139. assert len(flipped_masks) == 0
  140. assert flipped_masks.height == 28
  141. assert flipped_masks.width == 28
  142. # horizontally flip with bitmap masks contain 3 instances
  143. raw_masks = dummy_raw_bitmap_masks((3, 28, 28))
  144. bitmap_masks = BitmapMasks(raw_masks, 28, 28)
  145. flipped_masks = bitmap_masks.flip(flip_direction='horizontal')
  146. flipped_flipped_masks = flipped_masks.flip(flip_direction='horizontal')
  147. assert flipped_masks.masks.shape == (3, 28, 28)
  148. assert (bitmap_masks.masks == flipped_flipped_masks.masks).all()
  149. assert (flipped_masks.masks == raw_masks[:, :, ::-1]).all()
  150. # vertically flip with bitmap masks contain 3 instances
  151. raw_masks = dummy_raw_bitmap_masks((3, 28, 28))
  152. bitmap_masks = BitmapMasks(raw_masks, 28, 28)
  153. flipped_masks = bitmap_masks.flip(flip_direction='vertical')
  154. flipped_flipped_masks = flipped_masks.flip(flip_direction='vertical')
  155. assert len(flipped_masks) == 3
  156. assert flipped_masks.height == 28
  157. assert flipped_masks.width == 28
  158. assert (bitmap_masks.masks == flipped_flipped_masks.masks).all()
  159. assert (flipped_masks.masks == raw_masks[:, ::-1, :]).all()
  160. # diagonal flip with bitmap masks contain 3 instances
  161. raw_masks = dummy_raw_bitmap_masks((3, 28, 28))
  162. bitmap_masks = BitmapMasks(raw_masks, 28, 28)
  163. flipped_masks = bitmap_masks.flip(flip_direction='diagonal')
  164. flipped_flipped_masks = flipped_masks.flip(flip_direction='diagonal')
  165. assert len(flipped_masks) == 3
  166. assert flipped_masks.height == 28
  167. assert flipped_masks.width == 28
  168. assert (bitmap_masks.masks == flipped_flipped_masks.masks).all()
  169. assert (flipped_masks.masks == raw_masks[:, ::-1, ::-1]).all()
  170. def test_bitmap_mask_pad():
  171. # pad with empty bitmap masks
  172. raw_masks = dummy_raw_bitmap_masks((0, 28, 28))
  173. bitmap_masks = BitmapMasks(raw_masks, 28, 28)
  174. padded_masks = bitmap_masks.pad((56, 56))
  175. assert len(padded_masks) == 0
  176. assert padded_masks.height == 56
  177. assert padded_masks.width == 56
  178. # pad with bitmap masks contain 3 instances
  179. raw_masks = dummy_raw_bitmap_masks((3, 28, 28))
  180. bitmap_masks = BitmapMasks(raw_masks, 28, 28)
  181. padded_masks = bitmap_masks.pad((56, 56))
  182. assert len(padded_masks) == 3
  183. assert padded_masks.height == 56
  184. assert padded_masks.width == 56
  185. assert (padded_masks.masks[:, 28:, 28:] == 0).all()
  186. def test_bitmap_mask_crop():
  187. # crop with empty bitmap masks
  188. dummy_bbox = np.array([0, 10, 10, 27], dtype=np.int)
  189. raw_masks = dummy_raw_bitmap_masks((0, 28, 28))
  190. bitmap_masks = BitmapMasks(raw_masks, 28, 28)
  191. cropped_masks = bitmap_masks.crop(dummy_bbox)
  192. assert len(cropped_masks) == 0
  193. assert cropped_masks.height == 17
  194. assert cropped_masks.width == 10
  195. # crop with bitmap masks contain 3 instances
  196. raw_masks = dummy_raw_bitmap_masks((3, 28, 28))
  197. bitmap_masks = BitmapMasks(raw_masks, 28, 28)
  198. cropped_masks = bitmap_masks.crop(dummy_bbox)
  199. assert len(cropped_masks) == 3
  200. assert cropped_masks.height == 17
  201. assert cropped_masks.width == 10
  202. x1, y1, x2, y2 = dummy_bbox
  203. assert (cropped_masks.masks == raw_masks[:, y1:y2, x1:x2]).all()
  204. # crop with invalid bbox
  205. with pytest.raises(AssertionError):
  206. dummy_bbox = dummy_bboxes(2, 28, 28)
  207. bitmap_masks.crop(dummy_bbox)
  208. def test_bitmap_mask_crop_and_resize():
  209. dummy_bbox = dummy_bboxes(5, 28, 28)
  210. inds = np.random.randint(0, 3, (5, ))
  211. # crop and resize with empty bitmap masks
  212. raw_masks = dummy_raw_bitmap_masks((0, 28, 28))
  213. bitmap_masks = BitmapMasks(raw_masks, 28, 28)
  214. cropped_resized_masks = bitmap_masks.crop_and_resize(
  215. dummy_bbox, (56, 56), inds)
  216. assert len(cropped_resized_masks) == 0
  217. assert cropped_resized_masks.height == 56
  218. assert cropped_resized_masks.width == 56
  219. # crop and resize with bitmap masks contain 3 instances
  220. raw_masks = dummy_raw_bitmap_masks((3, 28, 28))
  221. bitmap_masks = BitmapMasks(raw_masks, 28, 28)
  222. cropped_resized_masks = bitmap_masks.crop_and_resize(
  223. dummy_bbox, (56, 56), inds)
  224. assert len(cropped_resized_masks) == 5
  225. assert cropped_resized_masks.height == 56
  226. assert cropped_resized_masks.width == 56
  227. def test_bitmap_mask_expand():
  228. # expand with empty bitmap masks
  229. raw_masks = dummy_raw_bitmap_masks((0, 28, 28))
  230. bitmap_masks = BitmapMasks(raw_masks, 28, 28)
  231. expanded_masks = bitmap_masks.expand(56, 56, 12, 14)
  232. assert len(expanded_masks) == 0
  233. assert expanded_masks.height == 56
  234. assert expanded_masks.width == 56
  235. # expand with bitmap masks contain 3 instances
  236. raw_masks = dummy_raw_bitmap_masks((3, 28, 28))
  237. bitmap_masks = BitmapMasks(raw_masks, 28, 28)
  238. expanded_masks = bitmap_masks.expand(56, 56, 12, 14)
  239. assert len(expanded_masks) == 3
  240. assert expanded_masks.height == 56
  241. assert expanded_masks.width == 56
  242. assert (expanded_masks.masks[:, :12, :14] == 0).all()
  243. assert (expanded_masks.masks[:, 12 + 28:, 14 + 28:] == 0).all()
  244. def test_bitmap_mask_area():
  245. # area of empty bitmap mask
  246. raw_masks = dummy_raw_bitmap_masks((0, 28, 28))
  247. bitmap_masks = BitmapMasks(raw_masks, 28, 28)
  248. assert bitmap_masks.areas.sum() == 0
  249. # area of bitmap masks contain 3 instances
  250. raw_masks = dummy_raw_bitmap_masks((3, 28, 28))
  251. bitmap_masks = BitmapMasks(raw_masks, 28, 28)
  252. areas = bitmap_masks.areas
  253. assert len(areas) == 3
  254. assert (areas == raw_masks.sum((1, 2))).all()
  255. def test_bitmap_mask_to_ndarray():
  256. # empty bitmap masks to ndarray
  257. raw_masks = dummy_raw_bitmap_masks((0, 28, 28))
  258. bitmap_masks = BitmapMasks(raw_masks, 28, 28)
  259. ndarray_masks = bitmap_masks.to_ndarray()
  260. assert isinstance(ndarray_masks, np.ndarray)
  261. assert ndarray_masks.shape == (0, 28, 28)
  262. # bitmap masks contain 3 instances to ndarray
  263. raw_masks = dummy_raw_bitmap_masks((3, 28, 28))
  264. bitmap_masks = BitmapMasks(raw_masks, 28, 28)
  265. ndarray_masks = bitmap_masks.to_ndarray()
  266. assert isinstance(ndarray_masks, np.ndarray)
  267. assert ndarray_masks.shape == (3, 28, 28)
  268. assert (ndarray_masks == raw_masks).all()
  269. def test_bitmap_mask_to_tensor():
  270. # empty bitmap masks to tensor
  271. raw_masks = dummy_raw_bitmap_masks((0, 28, 28))
  272. bitmap_masks = BitmapMasks(raw_masks, 28, 28)
  273. tensor_masks = bitmap_masks.to_tensor(dtype=torch.uint8, device='cpu')
  274. assert isinstance(tensor_masks, torch.Tensor)
  275. assert tensor_masks.shape == (0, 28, 28)
  276. # bitmap masks contain 3 instances to tensor
  277. raw_masks = dummy_raw_bitmap_masks((3, 28, 28))
  278. bitmap_masks = BitmapMasks(raw_masks, 28, 28)
  279. tensor_masks = bitmap_masks.to_tensor(dtype=torch.uint8, device='cpu')
  280. assert isinstance(tensor_masks, torch.Tensor)
  281. assert tensor_masks.shape == (3, 28, 28)
  282. assert (tensor_masks.numpy() == raw_masks).all()
  283. def test_bitmap_mask_index():
  284. raw_masks = dummy_raw_bitmap_masks((3, 28, 28))
  285. bitmap_masks = BitmapMasks(raw_masks, 28, 28)
  286. assert (bitmap_masks[0].masks == raw_masks[0]).all()
  287. assert (bitmap_masks[range(2)].masks == raw_masks[range(2)]).all()
  288. def test_bitmap_mask_iter():
  289. raw_masks = dummy_raw_bitmap_masks((3, 28, 28))
  290. bitmap_masks = BitmapMasks(raw_masks, 28, 28)
  291. for i, bitmap_mask in enumerate(bitmap_masks):
  292. assert bitmap_mask.shape == (28, 28)
  293. assert (bitmap_mask == raw_masks[i]).all()
  294. def test_polygon_mask_init():
  295. # init with empty masks
  296. raw_masks = []
  297. polygon_masks = BitmapMasks(raw_masks, 28, 28)
  298. assert len(polygon_masks) == 0
  299. assert polygon_masks.height == 28
  300. assert polygon_masks.width == 28
  301. # init with masks contain 3 instances
  302. raw_masks = dummy_raw_polygon_masks((3, 28, 28))
  303. polygon_masks = PolygonMasks(raw_masks, 28, 28)
  304. assert isinstance(polygon_masks.masks, list)
  305. assert isinstance(polygon_masks.masks[0], list)
  306. assert isinstance(polygon_masks.masks[0][0], np.ndarray)
  307. assert len(polygon_masks) == 3
  308. assert polygon_masks.height == 28
  309. assert polygon_masks.width == 28
  310. assert polygon_masks.to_ndarray().shape == (3, 28, 28)
  311. # init with raw masks of unsupported type
  312. with pytest.raises(AssertionError):
  313. raw_masks = [[[]]]
  314. PolygonMasks(raw_masks, 28, 28)
  315. raw_masks = [dummy_raw_polygon_masks((3, 28, 28))]
  316. PolygonMasks(raw_masks, 28, 28)
  317. def test_polygon_mask_rescale():
  318. # rescale with empty polygon masks
  319. raw_masks = dummy_raw_polygon_masks((0, 28, 28))
  320. polygon_masks = PolygonMasks(raw_masks, 28, 28)
  321. rescaled_masks = polygon_masks.rescale((56, 72))
  322. assert len(rescaled_masks) == 0
  323. assert rescaled_masks.height == 56
  324. assert rescaled_masks.width == 56
  325. assert rescaled_masks.to_ndarray().shape == (0, 56, 56)
  326. # rescale with polygon masks contain 3 instances
  327. raw_masks = [[np.array([1, 1, 3, 1, 4, 3, 2, 4, 1, 3], dtype=np.float)]]
  328. polygon_masks = PolygonMasks(raw_masks, 5, 5)
  329. rescaled_masks = polygon_masks.rescale((12, 10))
  330. assert len(rescaled_masks) == 1
  331. assert rescaled_masks.height == 10
  332. assert rescaled_masks.width == 10
  333. assert rescaled_masks.to_ndarray().shape == (1, 10, 10)
  334. truth = np.array(
  335. [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  336. [0, 0, 1, 1, 1, 1, 0, 0, 0, 0], [0, 0, 1, 1, 1, 1, 1, 0, 0, 0],
  337. [0, 0, 1, 1, 1, 1, 1, 0, 0, 0], [0, 0, 1, 1, 1, 1, 1, 1, 0, 0],
  338. [0, 0, 0, 1, 1, 1, 1, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
  339. [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]],
  340. np.uint8)
  341. assert (rescaled_masks.to_ndarray() == truth).all()
  342. def test_polygon_mask_resize():
  343. # resize with empty polygon masks
  344. raw_masks = dummy_raw_polygon_masks((0, 28, 28))
  345. polygon_masks = PolygonMasks(raw_masks, 28, 28)
  346. resized_masks = polygon_masks.resize((56, 72))
  347. assert len(resized_masks) == 0
  348. assert resized_masks.height == 56
  349. assert resized_masks.width == 72
  350. assert resized_masks.to_ndarray().shape == (0, 56, 72)
  351. assert len(resized_masks.get_bboxes()) == 0
  352. # resize with polygon masks contain 1 instance 1 part
  353. raw_masks1 = [[np.array([1, 1, 3, 1, 4, 3, 2, 4, 1, 3], dtype=np.float)]]
  354. polygon_masks1 = PolygonMasks(raw_masks1, 5, 5)
  355. resized_masks1 = polygon_masks1.resize((10, 10))
  356. assert len(resized_masks1) == 1
  357. assert resized_masks1.height == 10
  358. assert resized_masks1.width == 10
  359. assert resized_masks1.to_ndarray().shape == (1, 10, 10)
  360. truth1 = np.array(
  361. [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  362. [0, 0, 1, 1, 1, 1, 0, 0, 0, 0], [0, 0, 1, 1, 1, 1, 1, 0, 0, 0],
  363. [0, 0, 1, 1, 1, 1, 1, 0, 0, 0], [0, 0, 1, 1, 1, 1, 1, 1, 0, 0],
  364. [0, 0, 0, 1, 1, 1, 1, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
  365. [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]],
  366. np.uint8)
  367. assert (resized_masks1.to_ndarray() == truth1).all()
  368. bboxes = resized_masks1.get_bboxes()
  369. bbox_truth = np.array([[2, 2, 8, 8]])
  370. assert (bboxes == bbox_truth).all()
  371. # resize with polygon masks contain 1 instance 2 part
  372. raw_masks2 = [[
  373. np.array([0., 0., 1., 0., 1., 1.]),
  374. np.array([1., 1., 2., 1., 2., 2., 1., 2.])
  375. ]]
  376. polygon_masks2 = PolygonMasks(raw_masks2, 3, 3)
  377. resized_masks2 = polygon_masks2.resize((6, 6))
  378. assert len(resized_masks2) == 1
  379. assert resized_masks2.height == 6
  380. assert resized_masks2.width == 6
  381. assert resized_masks2.to_ndarray().shape == (1, 6, 6)
  382. truth2 = np.array(
  383. [[0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 1, 1, 0, 0],
  384. [0, 0, 1, 1, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]], np.uint8)
  385. assert (resized_masks2.to_ndarray() == truth2).all()
  386. # resize with polygon masks contain 2 instances
  387. raw_masks3 = [raw_masks1[0], raw_masks2[0]]
  388. polygon_masks3 = PolygonMasks(raw_masks3, 5, 5)
  389. resized_masks3 = polygon_masks3.resize((10, 10))
  390. assert len(resized_masks3) == 2
  391. assert resized_masks3.height == 10
  392. assert resized_masks3.width == 10
  393. assert resized_masks3.to_ndarray().shape == (2, 10, 10)
  394. truth3 = np.stack([truth1, np.pad(truth2, ((0, 4), (0, 4)), 'constant')])
  395. assert (resized_masks3.to_ndarray() == truth3).all()
  396. # resize to non-square
  397. raw_masks4 = [[np.array([1, 1, 3, 1, 4, 3, 2, 4, 1, 3], dtype=np.float)]]
  398. polygon_masks4 = PolygonMasks(raw_masks4, 5, 5)
  399. resized_masks4 = polygon_masks4.resize((5, 10))
  400. assert len(resized_masks4) == 1
  401. assert resized_masks4.height == 5
  402. assert resized_masks4.width == 10
  403. assert resized_masks4.to_ndarray().shape == (1, 5, 10)
  404. truth4 = np.array(
  405. [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 1, 1, 1, 1, 0, 0, 0],
  406. [0, 0, 1, 1, 1, 1, 1, 1, 0, 0], [0, 0, 0, 1, 1, 1, 0, 0, 0, 0],
  407. [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], np.uint8)
  408. assert (resized_masks4.to_ndarray() == truth4).all()
  409. def test_polygon_mask_flip():
  410. # flip with empty polygon masks
  411. raw_masks = dummy_raw_polygon_masks((0, 28, 28))
  412. polygon_masks = PolygonMasks(raw_masks, 28, 28)
  413. flipped_masks = polygon_masks.flip(flip_direction='horizontal')
  414. assert len(flipped_masks) == 0
  415. assert flipped_masks.height == 28
  416. assert flipped_masks.width == 28
  417. assert flipped_masks.to_ndarray().shape == (0, 28, 28)
  418. # TODO: fixed flip correctness checking after v2.0_coord is merged
  419. # horizontally flip with polygon masks contain 3 instances
  420. raw_masks = dummy_raw_polygon_masks((3, 28, 28))
  421. polygon_masks = PolygonMasks(raw_masks, 28, 28)
  422. flipped_masks = polygon_masks.flip(flip_direction='horizontal')
  423. flipped_flipped_masks = flipped_masks.flip(flip_direction='horizontal')
  424. assert len(flipped_masks) == 3
  425. assert flipped_masks.height == 28
  426. assert flipped_masks.width == 28
  427. assert flipped_masks.to_ndarray().shape == (3, 28, 28)
  428. assert (polygon_masks.to_ndarray() == flipped_flipped_masks.to_ndarray()
  429. ).all()
  430. # vertically flip with polygon masks contain 3 instances
  431. raw_masks = dummy_raw_polygon_masks((3, 28, 28))
  432. polygon_masks = PolygonMasks(raw_masks, 28, 28)
  433. flipped_masks = polygon_masks.flip(flip_direction='vertical')
  434. flipped_flipped_masks = flipped_masks.flip(flip_direction='vertical')
  435. assert len(flipped_masks) == 3
  436. assert flipped_masks.height == 28
  437. assert flipped_masks.width == 28
  438. assert flipped_masks.to_ndarray().shape == (3, 28, 28)
  439. assert (polygon_masks.to_ndarray() == flipped_flipped_masks.to_ndarray()
  440. ).all()
  441. # diagonal flip with polygon masks contain 3 instances
  442. raw_masks = dummy_raw_polygon_masks((3, 28, 28))
  443. polygon_masks = PolygonMasks(raw_masks, 28, 28)
  444. flipped_masks = polygon_masks.flip(flip_direction='diagonal')
  445. flipped_flipped_masks = flipped_masks.flip(flip_direction='diagonal')
  446. assert len(flipped_masks) == 3
  447. assert flipped_masks.height == 28
  448. assert flipped_masks.width == 28
  449. assert flipped_masks.to_ndarray().shape == (3, 28, 28)
  450. assert (polygon_masks.to_ndarray() == flipped_flipped_masks.to_ndarray()
  451. ).all()
  452. def test_polygon_mask_crop():
  453. dummy_bbox = np.array([0, 10, 10, 27], dtype=np.int)
  454. # crop with empty polygon masks
  455. raw_masks = dummy_raw_polygon_masks((0, 28, 28))
  456. polygon_masks = PolygonMasks(raw_masks, 28, 28)
  457. cropped_masks = polygon_masks.crop(dummy_bbox)
  458. assert len(cropped_masks) == 0
  459. assert cropped_masks.height == 17
  460. assert cropped_masks.width == 10
  461. assert cropped_masks.to_ndarray().shape == (0, 17, 10)
  462. # crop with polygon masks contain 1 instances
  463. raw_masks = [[np.array([1., 3., 5., 1., 5., 6., 1, 6])]]
  464. polygon_masks = PolygonMasks(raw_masks, 7, 7)
  465. bbox = np.array([0, 0, 3, 4])
  466. cropped_masks = polygon_masks.crop(bbox)
  467. assert len(cropped_masks) == 1
  468. assert cropped_masks.height == 4
  469. assert cropped_masks.width == 3
  470. assert cropped_masks.to_ndarray().shape == (1, 4, 3)
  471. truth = np.array([[0, 0, 0], [0, 0, 0], [0, 0, 1], [0, 1, 1]])
  472. assert (cropped_masks.to_ndarray() == truth).all()
  473. # crop with invalid bbox
  474. with pytest.raises(AssertionError):
  475. dummy_bbox = dummy_bboxes(2, 28, 28)
  476. polygon_masks.crop(dummy_bbox)
  477. def test_polygon_mask_pad():
  478. # pad with empty polygon masks
  479. raw_masks = dummy_raw_polygon_masks((0, 28, 28))
  480. polygon_masks = PolygonMasks(raw_masks, 28, 28)
  481. padded_masks = polygon_masks.pad((56, 56))
  482. assert len(padded_masks) == 0
  483. assert padded_masks.height == 56
  484. assert padded_masks.width == 56
  485. assert padded_masks.to_ndarray().shape == (0, 56, 56)
  486. # pad with polygon masks contain 3 instances
  487. raw_masks = dummy_raw_polygon_masks((3, 28, 28))
  488. polygon_masks = PolygonMasks(raw_masks, 28, 28)
  489. padded_masks = polygon_masks.pad((56, 56))
  490. assert len(padded_masks) == 3
  491. assert padded_masks.height == 56
  492. assert padded_masks.width == 56
  493. assert padded_masks.to_ndarray().shape == (3, 56, 56)
  494. assert (padded_masks.to_ndarray()[:, 28:, 28:] == 0).all()
  495. def test_polygon_mask_expand():
  496. with pytest.raises(NotImplementedError):
  497. raw_masks = dummy_raw_polygon_masks((0, 28, 28))
  498. polygon_masks = PolygonMasks(raw_masks, 28, 28)
  499. polygon_masks.expand(56, 56, 10, 17)
  500. def test_polygon_mask_crop_and_resize():
  501. dummy_bbox = dummy_bboxes(5, 28, 28)
  502. inds = np.random.randint(0, 3, (5, ))
  503. # crop and resize with empty polygon masks
  504. raw_masks = dummy_raw_polygon_masks((0, 28, 28))
  505. polygon_masks = PolygonMasks(raw_masks, 28, 28)
  506. cropped_resized_masks = polygon_masks.crop_and_resize(
  507. dummy_bbox, (56, 56), inds)
  508. assert len(cropped_resized_masks) == 0
  509. assert cropped_resized_masks.height == 56
  510. assert cropped_resized_masks.width == 56
  511. assert cropped_resized_masks.to_ndarray().shape == (0, 56, 56)
  512. # crop and resize with polygon masks contain 3 instances
  513. raw_masks = dummy_raw_polygon_masks((3, 28, 28))
  514. polygon_masks = PolygonMasks(raw_masks, 28, 28)
  515. cropped_resized_masks = polygon_masks.crop_and_resize(
  516. dummy_bbox, (56, 56), inds)
  517. assert len(cropped_resized_masks) == 5
  518. assert cropped_resized_masks.height == 56
  519. assert cropped_resized_masks.width == 56
  520. assert cropped_resized_masks.to_ndarray().shape == (5, 56, 56)
  521. def test_polygon_mask_area():
  522. # area of empty polygon masks
  523. raw_masks = dummy_raw_polygon_masks((0, 28, 28))
  524. polygon_masks = PolygonMasks(raw_masks, 28, 28)
  525. assert polygon_masks.areas.sum() == 0
  526. # area of polygon masks contain 1 instance
  527. # here we hack a case that the gap between the area of bitmap and polygon
  528. # is minor
  529. raw_masks = [[np.array([1, 1, 5, 1, 3, 4])]]
  530. polygon_masks = PolygonMasks(raw_masks, 6, 6)
  531. polygon_area = polygon_masks.areas
  532. bitmap_area = polygon_masks.to_bitmap().areas
  533. assert len(polygon_area) == 1
  534. assert np.isclose(polygon_area, bitmap_area).all()
  535. def test_polygon_mask_to_bitmap():
  536. # polygon masks contain 3 instances to bitmap
  537. raw_masks = dummy_raw_polygon_masks((3, 28, 28))
  538. polygon_masks = PolygonMasks(raw_masks, 28, 28)
  539. bitmap_masks = polygon_masks.to_bitmap()
  540. assert (polygon_masks.to_ndarray() == bitmap_masks.to_ndarray()).all()
  541. def test_polygon_mask_to_ndarray():
  542. # empty polygon masks to ndarray
  543. raw_masks = dummy_raw_polygon_masks((0, 28, 28))
  544. polygon_masks = PolygonMasks(raw_masks, 28, 28)
  545. ndarray_masks = polygon_masks.to_ndarray()
  546. assert isinstance(ndarray_masks, np.ndarray)
  547. assert ndarray_masks.shape == (0, 28, 28)
  548. # polygon masks contain 3 instances to ndarray
  549. raw_masks = dummy_raw_polygon_masks((3, 28, 28))
  550. polygon_masks = PolygonMasks(raw_masks, 28, 28)
  551. ndarray_masks = polygon_masks.to_ndarray()
  552. assert isinstance(ndarray_masks, np.ndarray)
  553. assert ndarray_masks.shape == (3, 28, 28)
  554. def test_polygon_to_tensor():
  555. # empty polygon masks to tensor
  556. raw_masks = dummy_raw_polygon_masks((0, 28, 28))
  557. polygon_masks = PolygonMasks(raw_masks, 28, 28)
  558. tensor_masks = polygon_masks.to_tensor(dtype=torch.uint8, device='cpu')
  559. assert isinstance(tensor_masks, torch.Tensor)
  560. assert tensor_masks.shape == (0, 28, 28)
  561. # polygon masks contain 3 instances to tensor
  562. raw_masks = dummy_raw_polygon_masks((3, 28, 28))
  563. polygon_masks = PolygonMasks(raw_masks, 28, 28)
  564. tensor_masks = polygon_masks.to_tensor(dtype=torch.uint8, device='cpu')
  565. assert isinstance(tensor_masks, torch.Tensor)
  566. assert tensor_masks.shape == (3, 28, 28)
  567. assert (tensor_masks.numpy() == polygon_masks.to_ndarray()).all()
  568. def test_polygon_mask_index():
  569. raw_masks = dummy_raw_polygon_masks((3, 28, 28))
  570. polygon_masks = PolygonMasks(raw_masks, 28, 28)
  571. # index by integer
  572. polygon_masks[0]
  573. # index by list
  574. polygon_masks[[0, 1]]
  575. # index by ndarray
  576. polygon_masks[np.asarray([0, 1])]
  577. with pytest.raises(ValueError):
  578. # invalid index
  579. polygon_masks[torch.Tensor([1, 2])]
  580. def test_polygon_mask_iter():
  581. raw_masks = dummy_raw_polygon_masks((3, 28, 28))
  582. polygon_masks = PolygonMasks(raw_masks, 28, 28)
  583. for i, polygon_mask in enumerate(polygon_masks):
  584. assert np.equal(polygon_mask, raw_masks[i]).all()

No Description

Contributors (2)