test_geometric.py 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  1. # Copyright (c) OpenMMLab. All rights reserved.
  2. import copy
  3. import unittest
  4. import numpy as np
  5. from mmdet.datasets.transforms import (GeomTransform, Rotate, ShearX, ShearY,
  6. TranslateX, TranslateY)
  7. from mmdet.structures.bbox import HorizontalBoxes
  8. from mmdet.structures.mask import BitmapMasks, PolygonMasks
  9. from .utils import check_result_same, construct_toy_data
  10. class TestGeomTransform(unittest.TestCase):
  11. def setUp(self):
  12. """Setup the model and optimizer which are used in every test method.
  13. TestCase calls functions in this order: setUp() -> testMethod() ->
  14. tearDown() -> cleanUp()
  15. """
  16. self.check_keys = ('img', 'gt_bboxes', 'gt_bboxes_labels', 'gt_masks',
  17. 'gt_ignore_flags', 'gt_seg_map')
  18. self.results_mask = construct_toy_data(poly2mask=True)
  19. self.img_border_value = (104, 116, 124)
  20. self.seg_ignore_label = 255
  21. def test_geomtransform(self):
  22. # test assertion for invalid prob
  23. with self.assertRaises(AssertionError):
  24. transform = GeomTransform(
  25. prob=-0.5, level=1, min_mag=0.0, max_mag=1.0)
  26. # test assertion for invalid value of level
  27. with self.assertRaises(AssertionError):
  28. transform = GeomTransform(
  29. prob=0.5, level=-1, min_mag=0.0, max_mag=1.0)
  30. # test assertion for invalid value of min_mag and max_mag
  31. with self.assertRaises(AssertionError):
  32. transform = ShearX(prob=0.5, level=2, min_mag=1.0, max_mag=0.0)
  33. # test assertion for the num of elements in tuple img_border_value
  34. with self.assertRaises(AssertionError):
  35. transform = GeomTransform(
  36. prob=0.5,
  37. level=1,
  38. min_mag=0.0,
  39. max_mag=1.0,
  40. img_border_value=(128, 128, 128, 128))
  41. # test ValueError for invalid type of img_border_value
  42. with self.assertRaises(ValueError):
  43. transform = GeomTransform(
  44. prob=0.5,
  45. level=1,
  46. min_mag=0.0,
  47. max_mag=1.0,
  48. img_border_value=[128, 128, 128])
  49. # test assertion for invalid value of img_border_value
  50. with self.assertRaises(AssertionError):
  51. transform = GeomTransform(
  52. prob=0.5,
  53. level=1,
  54. min_mag=0.0,
  55. max_mag=1.0,
  56. img_border_value=(128, -1, 256))
  57. # test case when no aug (prob=0)
  58. transform = GeomTransform(
  59. prob=0.,
  60. level=10,
  61. min_mag=0.0,
  62. max_mag=1.0,
  63. img_border_value=self.img_border_value)
  64. results_wo_aug = transform(copy.deepcopy(self.results_mask))
  65. check_result_same(self.results_mask, results_wo_aug, self.check_keys)
  66. def test_repr(self):
  67. transform = GeomTransform(
  68. prob=0.5,
  69. level=5,
  70. min_mag=0.0,
  71. max_mag=1.0,
  72. )
  73. self.assertEqual(
  74. repr(transform), ('GeomTransform(prob=0.5, '
  75. 'level=5, '
  76. 'min_mag=0.0, '
  77. 'max_mag=1.0, '
  78. 'reversal_prob=0.5, '
  79. 'img_border_value=(128.0, 128.0, 128.0), '
  80. 'mask_border_value=0, '
  81. 'seg_ignore_label=255, '
  82. 'interpolation=bilinear)'))
  83. class TestShearX(unittest.TestCase):
  84. def setUp(self):
  85. """Setup the model and optimizer which are used in every test method.
  86. TestCase calls functions in this order: setUp() -> testMethod() ->
  87. tearDown() -> cleanUp()
  88. """
  89. self.check_keys = ('img', 'gt_bboxes', 'gt_bboxes_labels', 'gt_masks',
  90. 'gt_ignore_flags', 'gt_seg_map')
  91. self.results_mask = construct_toy_data(poly2mask=True)
  92. self.results_poly = construct_toy_data(poly2mask=False)
  93. self.results_mask_boxtype = construct_toy_data(
  94. poly2mask=True, use_box_type=True)
  95. self.img_border_value = (104, 116, 124)
  96. self.seg_ignore_label = 255
  97. def test_shearx(self):
  98. # test assertion for invalid value of min_mag
  99. with self.assertRaises(AssertionError):
  100. transform = ShearX(prob=0.5, level=2, min_mag=-30.)
  101. # test assertion for invalid value of max_mag
  102. with self.assertRaises(AssertionError):
  103. transform = ShearX(prob=0.5, level=2, max_mag=100.)
  104. # test case when no shear horizontally (level=0)
  105. transform = ShearX(
  106. prob=1.0,
  107. level=0,
  108. img_border_value=self.img_border_value,
  109. seg_ignore_label=self.seg_ignore_label,
  110. )
  111. results_wo_shearx = transform(copy.deepcopy(self.results_mask))
  112. check_result_same(self.results_mask, results_wo_shearx,
  113. self.check_keys)
  114. # test shear horizontally, magnitude=-1
  115. transform = ShearX(
  116. prob=1.0,
  117. level=10,
  118. max_mag=45.,
  119. reversal_prob=1.0,
  120. img_border_value=self.img_border_value)
  121. results_sheared = transform(copy.deepcopy(self.results_mask))
  122. results_gt = copy.deepcopy(self.results_mask)
  123. img_gt = np.array([[1, 2, 3, 4], [0, 5, 6, 7], [0, 0, 9, 10]],
  124. dtype=np.uint8)
  125. img_gt = np.stack([img_gt, img_gt, img_gt], axis=-1)
  126. img_gt[1, 0, :] = np.array(self.img_border_value)
  127. img_gt[2, 0, :] = np.array(self.img_border_value)
  128. img_gt[2, 1, :] = np.array(self.img_border_value)
  129. results_gt['img'] = img_gt
  130. results_gt['gt_bboxes'] = np.array([[1, 0, 4, 2]], dtype=np.float32)
  131. results_gt['gt_bboxes_labels'] = np.array([13], dtype=np.int64)
  132. gt_masks = np.array([[0, 1, 0, 0], [0, 0, 1, 1], [0, 0, 0, 1]],
  133. dtype=np.uint8)[None, :, :]
  134. results_gt['gt_masks'] = BitmapMasks(gt_masks, 3, 4)
  135. results_gt['gt_ignore_flags'] = np.array(np.array([1], dtype=bool))
  136. results_gt['gt_seg_map'] = np.array(
  137. [[255, 13, 255, 255], [255, 255, 13, 13], [255, 255, 255, 13]],
  138. dtype=self.results_mask['gt_seg_map'].dtype)
  139. check_result_same(results_gt, results_sheared, self.check_keys)
  140. # test PolygonMasks with shear horizontally, magnitude=1
  141. results_sheared = transform(copy.deepcopy(self.results_poly))
  142. gt_masks = [[np.array([3, 2, 1, 0, 3, 1], dtype=np.float32)]]
  143. results_gt['gt_masks'] = PolygonMasks(gt_masks, 3, 4)
  144. check_result_same(results_gt, results_sheared, self.check_keys)
  145. def test_shearx_use_box_type(self):
  146. # test case when no shear horizontally (level=0)
  147. transform = ShearX(
  148. prob=1.0,
  149. level=0,
  150. img_border_value=self.img_border_value,
  151. seg_ignore_label=self.seg_ignore_label,
  152. )
  153. results_wo_shearx = transform(copy.deepcopy(self.results_mask_boxtype))
  154. check_result_same(self.results_mask_boxtype, results_wo_shearx,
  155. self.check_keys)
  156. # test shear horizontally, magnitude=-1
  157. transform = ShearX(
  158. prob=1.0,
  159. level=10,
  160. max_mag=45.,
  161. reversal_prob=1.0,
  162. img_border_value=self.img_border_value)
  163. results_sheared = transform(copy.deepcopy(self.results_mask_boxtype))
  164. results_gt = copy.deepcopy(self.results_mask_boxtype)
  165. img_gt = np.array([[1, 2, 3, 4], [0, 5, 6, 7], [0, 0, 9, 10]],
  166. dtype=np.uint8)
  167. img_gt = np.stack([img_gt, img_gt, img_gt], axis=-1)
  168. img_gt[1, 0, :] = np.array(self.img_border_value)
  169. img_gt[2, 0, :] = np.array(self.img_border_value)
  170. img_gt[2, 1, :] = np.array(self.img_border_value)
  171. results_gt['img'] = img_gt
  172. results_gt['gt_bboxes'] = HorizontalBoxes(
  173. np.array([[1, 0, 4, 2]], dtype=np.float32))
  174. results_gt['gt_bboxes_labels'] = np.array([13], dtype=np.int64)
  175. gt_masks = np.array([[0, 1, 0, 0], [0, 0, 1, 1], [0, 0, 0, 1]],
  176. dtype=np.uint8)[None, :, :]
  177. results_gt['gt_masks'] = BitmapMasks(gt_masks, 3, 4)
  178. results_gt['gt_ignore_flags'] = np.array(np.array([1], dtype=bool))
  179. results_gt['gt_seg_map'] = np.array(
  180. [[255, 13, 255, 255], [255, 255, 13, 13], [255, 255, 255, 13]],
  181. dtype=self.results_mask['gt_seg_map'].dtype)
  182. check_result_same(results_gt, results_sheared, self.check_keys)
  183. def test_repr(self):
  184. transform = ShearX(prob=0.5, level=10)
  185. self.assertEqual(
  186. repr(transform), ('ShearX(prob=0.5, '
  187. 'level=10, '
  188. 'min_mag=0.0, '
  189. 'max_mag=30.0, '
  190. 'reversal_prob=0.5, '
  191. 'img_border_value=(128.0, 128.0, 128.0), '
  192. 'mask_border_value=0, '
  193. 'seg_ignore_label=255, '
  194. 'interpolation=bilinear)'))
  195. class TestShearY(unittest.TestCase):
  196. def setUp(self):
  197. """Setup the model and optimizer which are used in every test method.
  198. TestCase calls functions in this order: setUp() -> testMethod() ->
  199. tearDown() -> cleanUp()
  200. """
  201. self.check_keys = ('img', 'gt_bboxes', 'gt_bboxes_labels', 'gt_masks',
  202. 'gt_ignore_flags', 'gt_seg_map')
  203. self.results_mask = construct_toy_data(poly2mask=True)
  204. self.results_poly = construct_toy_data(poly2mask=False)
  205. self.results_mask_boxtype = construct_toy_data(
  206. poly2mask=True, use_box_type=True)
  207. self.img_border_value = (104, 116, 124)
  208. self.seg_ignore_label = 255
  209. def test_sheary(self):
  210. # test assertion for invalid value of min_mag
  211. with self.assertRaises(AssertionError):
  212. transform = ShearY(prob=0.5, level=2, min_mag=-30.)
  213. # test assertion for invalid value of max_mag
  214. with self.assertRaises(AssertionError):
  215. transform = ShearY(prob=0.5, level=2, max_mag=100.)
  216. # test case when no shear vertically (level=0)
  217. transform = ShearY(
  218. prob=1.0,
  219. level=0,
  220. img_border_value=self.img_border_value,
  221. seg_ignore_label=self.seg_ignore_label,
  222. )
  223. results_wo_sheary = transform(copy.deepcopy(self.results_mask))
  224. check_result_same(self.results_mask, results_wo_sheary,
  225. self.check_keys)
  226. # test shear vertically, magnitude=1
  227. transform = ShearY(prob=1., level=10, max_mag=45., reversal_prob=0.)
  228. results_sheared = transform(copy.deepcopy(self.results_mask))
  229. results_gt = copy.deepcopy(self.results_mask)
  230. img_gt = np.array(
  231. [[1, 6, 11, 128], [5, 10, 128, 128], [9, 128, 128, 128]],
  232. dtype=np.uint8)
  233. img_gt = np.stack([img_gt, img_gt, img_gt], axis=-1)
  234. results_gt['img'] = img_gt
  235. results_gt['gt_bboxes'] = np.array([[1, 0, 2, 1]], dtype=np.float32)
  236. results_gt['gt_bboxes_labels'] = np.array([13], dtype=np.int64)
  237. gt_masks = np.array([[0, 1, 0, 0], [0, 1, 0, 0], [0, 0, 0, 0]],
  238. dtype=np.uint8)[None, :, :]
  239. results_gt['gt_masks'] = BitmapMasks(gt_masks, 3, 4)
  240. results_gt['gt_ignore_flags'] = np.array(np.array([1], dtype=bool))
  241. results_gt['gt_seg_map'] = np.array(
  242. [[255, 13, 255, 255], [255, 13, 255, 255], [255, 255, 255, 255]],
  243. dtype=self.results_mask['gt_seg_map'].dtype)
  244. check_result_same(results_gt, results_sheared, self.check_keys)
  245. # test PolygonMasks with shear vertically, magnitude=-1
  246. results_sheared = transform(copy.deepcopy(self.results_poly))
  247. gt_masks = [[np.array([1, 1, 1, 0, 2, 0], dtype=np.float32)]]
  248. results_gt['gt_masks'] = PolygonMasks(gt_masks, 3, 4)
  249. check_result_same(results_gt, results_sheared, self.check_keys)
  250. def test_sheary_use_box_type(self):
  251. # test case when no shear vertically (level=0)
  252. transform = ShearY(
  253. prob=1.0,
  254. level=0,
  255. img_border_value=self.img_border_value,
  256. seg_ignore_label=self.seg_ignore_label,
  257. )
  258. results_wo_sheary = transform(copy.deepcopy(self.results_mask_boxtype))
  259. check_result_same(self.results_mask_boxtype, results_wo_sheary,
  260. self.check_keys)
  261. # test shear vertically, magnitude=1
  262. transform = ShearY(prob=1., level=10, max_mag=45., reversal_prob=0.)
  263. results_sheared = transform(copy.deepcopy(self.results_mask_boxtype))
  264. results_gt = copy.deepcopy(self.results_mask_boxtype)
  265. img_gt = np.array(
  266. [[1, 6, 11, 128], [5, 10, 128, 128], [9, 128, 128, 128]],
  267. dtype=np.uint8)
  268. img_gt = np.stack([img_gt, img_gt, img_gt], axis=-1)
  269. results_gt['img'] = img_gt
  270. results_gt['gt_bboxes'] = HorizontalBoxes(
  271. np.array([[1, 0, 2, 1]], dtype=np.float32))
  272. results_gt['gt_bboxes_labels'] = np.array([13], dtype=np.int64)
  273. gt_masks = np.array([[0, 1, 0, 0], [0, 1, 0, 0], [0, 0, 0, 0]],
  274. dtype=np.uint8)[None, :, :]
  275. results_gt['gt_masks'] = BitmapMasks(gt_masks, 3, 4)
  276. results_gt['gt_ignore_flags'] = np.array(np.array([1], dtype=bool))
  277. results_gt['gt_seg_map'] = np.array(
  278. [[255, 13, 255, 255], [255, 13, 255, 255], [255, 255, 255, 255]],
  279. dtype=self.results_mask['gt_seg_map'].dtype)
  280. check_result_same(results_gt, results_sheared, self.check_keys)
  281. def test_repr(self):
  282. transform = ShearY(prob=0.5, level=10)
  283. self.assertEqual(
  284. repr(transform), ('ShearY(prob=0.5, '
  285. 'level=10, '
  286. 'min_mag=0.0, '
  287. 'max_mag=30.0, '
  288. 'reversal_prob=0.5, '
  289. 'img_border_value=(128.0, 128.0, 128.0), '
  290. 'mask_border_value=0, '
  291. 'seg_ignore_label=255, '
  292. 'interpolation=bilinear)'))
  293. class TestRotate(unittest.TestCase):
  294. def setUp(self):
  295. """Setup the model and optimizer which are used in every test method.
  296. TestCase calls functions in this order: setUp() -> testMethod() ->
  297. tearDown() -> cleanUp()
  298. """
  299. self.check_keys = ('img', 'gt_bboxes', 'gt_bboxes_labels', 'gt_masks',
  300. 'gt_ignore_flags', 'gt_seg_map')
  301. self.results_mask = construct_toy_data(poly2mask=True)
  302. self.results_poly = construct_toy_data(poly2mask=False)
  303. self.results_mask_boxtype = construct_toy_data(
  304. poly2mask=True, use_box_type=True)
  305. self.img_border_value = (104, 116, 124)
  306. self.seg_ignore_label = 255
  307. def test_rotate(self):
  308. # test assertion for invalid value of min_mag
  309. with self.assertRaises(AssertionError):
  310. transform = ShearY(prob=0.5, level=2, min_mag=-90.0)
  311. # test assertion for invalid value of max_mag
  312. with self.assertRaises(AssertionError):
  313. transform = ShearY(prob=0.5, level=2, max_mag=270.0)
  314. # test case when no rotate aug (level=0)
  315. transform = Rotate(
  316. prob=1.,
  317. level=0,
  318. img_border_value=self.img_border_value,
  319. seg_ignore_label=self.seg_ignore_label,
  320. )
  321. results_wo_rotate = transform(copy.deepcopy(self.results_mask))
  322. check_result_same(self.results_mask, results_wo_rotate,
  323. self.check_keys)
  324. # test clockwise rotation with angle 90
  325. transform = Rotate(
  326. prob=1.,
  327. level=10,
  328. max_mag=90.0,
  329. # set reversal_prob to 1 for clockwise rotation
  330. reversal_prob=1.,
  331. )
  332. results_rotated = transform(copy.deepcopy(self.results_mask))
  333. # The image, masks, and semantic segmentation map
  334. # will be bilinearly interpolated.
  335. img_gt = np.array([[69, 8, 4, 65], [69, 9, 5, 65],
  336. [70, 10, 6, 66]]).astype(np.uint8)
  337. img_gt = np.stack([img_gt, img_gt, img_gt], axis=-1)
  338. results_gt = copy.deepcopy(self.results_mask)
  339. results_gt['img'] = img_gt
  340. results_gt['gt_bboxes'] = np.array([[0.5, 0.5, 2.5, 1.5]],
  341. dtype=np.float32)
  342. gt_masks = np.array([[0, 1, 1, 0], [0, 1, 1, 0], [0, 0, 0, 0]],
  343. dtype=np.uint8)[None, :, :]
  344. results_gt['gt_masks'] = BitmapMasks(gt_masks, 3, 4)
  345. results_gt['gt_ignore_flags'] = np.array(np.array([1], dtype=bool))
  346. results_gt['gt_seg_map'] = np.array(
  347. [[255, 13, 13, 13], [255, 255, 13, 255],
  348. [255, 255, 255,
  349. 255]]).astype(self.results_mask['gt_seg_map'].dtype)
  350. check_result_same(results_gt, results_rotated, self.check_keys)
  351. # test clockwise rotation with angle 90, PolygonMasks
  352. results_rotated = transform(copy.deepcopy(self.results_poly))
  353. gt_masks = [[np.array([0, 1, 0, 1, 0, 2], dtype=np.float)]]
  354. results_gt['gt_masks'] = PolygonMasks(gt_masks, 3, 4)
  355. check_result_same(results_gt, results_rotated, self.check_keys)
  356. # test counter-clockwise rotation with angle 90
  357. transform = Rotate(
  358. prob=1.0,
  359. level=10,
  360. max_mag=90.0,
  361. # set reversal_prob to 0 for counter-clockwise rotation
  362. reversal_prob=0.0,
  363. )
  364. results_rotated = transform(copy.deepcopy(self.results_mask))
  365. # The image, masks, and semantic segmentation map
  366. # will be bilinearly interpolated.
  367. img_gt = np.array([[66, 6, 10, 70], [65, 5, 9, 69],
  368. [65, 4, 8, 69]]).astype(np.uint8)
  369. img_gt = np.stack([img_gt, img_gt, img_gt], axis=-1)
  370. results_gt = copy.deepcopy(self.results_mask)
  371. results_gt['img'] = img_gt
  372. results_gt['gt_bboxes'] = np.array([[0.5, 0.5, 2.5, 1.5]],
  373. dtype=np.float32)
  374. gt_masks = np.array([[0, 0, 0, 0], [0, 1, 1, 0], [0, 1, 1, 0]],
  375. dtype=np.uint8)[None, :, :]
  376. results_gt['gt_masks'] = BitmapMasks(gt_masks, 3, 4)
  377. results_gt['gt_ignore_flags'] = np.array(np.array([1], dtype=bool))
  378. results_gt['gt_seg_map'] = np.array(
  379. [[255, 255, 255, 255], [255, 13, 255, 255],
  380. [13, 13, 13, 255]]).astype(self.results_mask['gt_seg_map'].dtype)
  381. check_result_same(results_gt, results_rotated, self.check_keys)
  382. # test counter-clockwise rotation with angle 90, PolygonMasks
  383. results_rotated = transform(copy.deepcopy(self.results_poly))
  384. gt_masks = [[np.array([2, 0, 0, 0, 1, 0], dtype=np.float)]]
  385. results_gt['gt_masks'] = PolygonMasks(gt_masks, 3, 4)
  386. check_result_same(results_gt, results_rotated, self.check_keys)
  387. def test_rotate_use_box_type(self):
  388. # test case when no rotate aug (level=0)
  389. transform = Rotate(
  390. prob=1.,
  391. level=0,
  392. img_border_value=self.img_border_value,
  393. seg_ignore_label=self.seg_ignore_label,
  394. )
  395. results_wo_rotate = transform(copy.deepcopy(self.results_mask_boxtype))
  396. check_result_same(self.results_mask_boxtype, results_wo_rotate,
  397. self.check_keys)
  398. # test clockwise rotation with angle 90
  399. transform = Rotate(
  400. prob=1.,
  401. level=10,
  402. max_mag=90.0,
  403. # set reversal_prob to 1 for clockwise rotation
  404. reversal_prob=1.,
  405. )
  406. results_rotated = transform(copy.deepcopy(self.results_mask_boxtype))
  407. # The image, masks, and semantic segmentation map
  408. # will be bilinearly interpolated.
  409. img_gt = np.array([[69, 8, 4, 65], [69, 9, 5, 65],
  410. [70, 10, 6, 66]]).astype(np.uint8)
  411. img_gt = np.stack([img_gt, img_gt, img_gt], axis=-1)
  412. results_gt = copy.deepcopy(self.results_mask_boxtype)
  413. results_gt['img'] = img_gt
  414. results_gt['gt_bboxes'] = HorizontalBoxes(
  415. np.array([[0.5, 0.5, 2.5, 1.5]], dtype=np.float32))
  416. gt_masks = np.array([[0, 1, 1, 0], [0, 1, 1, 0], [0, 0, 0, 0]],
  417. dtype=np.uint8)[None, :, :]
  418. results_gt['gt_masks'] = BitmapMasks(gt_masks, 3, 4)
  419. results_gt['gt_ignore_flags'] = np.array(np.array([1], dtype=bool))
  420. results_gt['gt_seg_map'] = np.array(
  421. [[255, 13, 13, 13], [255, 255, 13, 255],
  422. [255, 255, 255,
  423. 255]]).astype(self.results_mask['gt_seg_map'].dtype)
  424. check_result_same(results_gt, results_rotated, self.check_keys)
  425. # test counter-clockwise rotation with angle 90
  426. transform = Rotate(
  427. prob=1.0,
  428. level=10,
  429. max_mag=90.0,
  430. # set reversal_prob to 0 for counter-clockwise rotation
  431. reversal_prob=0.0,
  432. )
  433. results_rotated = transform(copy.deepcopy(self.results_mask_boxtype))
  434. # The image, masks, and semantic segmentation map
  435. # will be bilinearly interpolated.
  436. img_gt = np.array([[66, 6, 10, 70], [65, 5, 9, 69],
  437. [65, 4, 8, 69]]).astype(np.uint8)
  438. img_gt = np.stack([img_gt, img_gt, img_gt], axis=-1)
  439. results_gt = copy.deepcopy(self.results_mask_boxtype)
  440. results_gt['img'] = img_gt
  441. results_gt['gt_bboxes'] = HorizontalBoxes(
  442. np.array([[0.5, 0.5, 2.5, 1.5]], dtype=np.float32))
  443. gt_masks = np.array([[0, 0, 0, 0], [0, 1, 1, 0], [0, 1, 1, 0]],
  444. dtype=np.uint8)[None, :, :]
  445. results_gt['gt_masks'] = BitmapMasks(gt_masks, 3, 4)
  446. results_gt['gt_ignore_flags'] = np.array(np.array([1], dtype=bool))
  447. results_gt['gt_seg_map'] = np.array(
  448. [[255, 255, 255, 255], [255, 13, 255, 255],
  449. [13, 13, 13, 255]]).astype(self.results_mask['gt_seg_map'].dtype)
  450. check_result_same(results_gt, results_rotated, self.check_keys)
  451. def test_repr(self):
  452. transform = Rotate(prob=0.5, level=5)
  453. self.assertEqual(
  454. repr(transform), ('Rotate(prob=0.5, '
  455. 'level=5, '
  456. 'min_mag=0.0, '
  457. 'max_mag=30.0, '
  458. 'reversal_prob=0.5, '
  459. 'img_border_value=(128.0, 128.0, 128.0), '
  460. 'mask_border_value=0, '
  461. 'seg_ignore_label=255, '
  462. 'interpolation=bilinear)'))
  463. class TestTranslateX(unittest.TestCase):
  464. def setUp(self):
  465. """Setup the model and optimizer which are used in every test method.
  466. TestCase calls functions in this order: setUp() -> testMethod() ->
  467. tearDown() -> cleanUp()
  468. """
  469. self.check_keys = ('img', 'gt_bboxes', 'gt_bboxes_labels', 'gt_masks',
  470. 'gt_ignore_flags', 'gt_seg_map')
  471. self.results_mask = construct_toy_data(poly2mask=True)
  472. self.results_poly = construct_toy_data(poly2mask=False)
  473. self.results_mask_boxtype = construct_toy_data(
  474. poly2mask=True, use_box_type=True)
  475. self.img_border_value = (104, 116, 124)
  476. self.seg_ignore_label = 255
  477. def test_translatex(self):
  478. # test assertion for invalid value of min_mag
  479. with self.assertRaises(AssertionError):
  480. transform = TranslateX(prob=0.5, level=2, min_mag=-1.)
  481. # test assertion for invalid value of max_mag
  482. with self.assertRaises(AssertionError):
  483. transform = TranslateX(prob=0.5, level=2, max_mag=1.1)
  484. # test case when level=0 (without translate aug)
  485. transform = TranslateX(
  486. prob=1.0,
  487. level=0,
  488. img_border_value=self.img_border_value,
  489. seg_ignore_label=self.seg_ignore_label)
  490. results_wo_translatex = transform(copy.deepcopy(self.results_mask))
  491. check_result_same(self.results_mask, results_wo_translatex,
  492. self.check_keys)
  493. # test translate horizontally, magnitude=-1
  494. transform = TranslateX(
  495. prob=1.0,
  496. level=10,
  497. max_mag=0.3,
  498. reversal_prob=0.0,
  499. img_border_value=self.img_border_value,
  500. seg_ignore_label=self.seg_ignore_label)
  501. results_translated = transform(copy.deepcopy(self.results_mask))
  502. img_gt = np.array([[2, 3, 4, 0], [6, 7, 8, 0], [10, 11, 12,
  503. 0]]).astype(np.uint8)
  504. img_gt = np.stack([img_gt, img_gt, img_gt], axis=-1)
  505. img_gt[:, 3, :] = np.array(self.img_border_value)
  506. results_gt = copy.deepcopy(self.results_mask)
  507. results_gt['img'] = img_gt
  508. results_gt['gt_bboxes'] = np.array([[0, 0, 1, 2]], dtype=np.float32)
  509. gt_masks = np.array([[1, 0, 0, 0], [1, 1, 0, 0], [1, 0, 0, 0]],
  510. dtype=np.uint8)[None, :, :]
  511. results_gt['gt_masks'] = BitmapMasks(gt_masks, 3, 4)
  512. results_gt['gt_ignore_flags'] = np.array(np.array([1], dtype=bool))
  513. results_gt['gt_seg_map'] = np.array(
  514. [[13, 255, 255, 255], [13, 13, 255, 255],
  515. [13, 255, 255,
  516. 255]]).astype(self.results_mask['gt_seg_map'].dtype)
  517. check_result_same(results_gt, results_translated, self.check_keys)
  518. # test PolygonMasks with translate horizontally.
  519. results_translated = transform(copy.deepcopy(self.results_poly))
  520. gt_masks = [[np.array([0, 2, 0, 0, 1, 1], dtype=np.float32)]]
  521. results_gt['gt_masks'] = PolygonMasks(gt_masks, 3, 4)
  522. check_result_same(results_gt, results_translated, self.check_keys)
  523. def test_translatex_use_box_type(self):
  524. # test case when level=0 (without translate aug)
  525. transform = TranslateX(
  526. prob=1.0,
  527. level=0,
  528. img_border_value=self.img_border_value,
  529. seg_ignore_label=self.seg_ignore_label)
  530. results_wo_translatex = transform(
  531. copy.deepcopy(self.results_mask_boxtype))
  532. check_result_same(self.results_mask_boxtype, results_wo_translatex,
  533. self.check_keys)
  534. # test translate horizontally, magnitude=-1
  535. transform = TranslateX(
  536. prob=1.0,
  537. level=10,
  538. max_mag=0.3,
  539. reversal_prob=0.0,
  540. img_border_value=self.img_border_value,
  541. seg_ignore_label=self.seg_ignore_label)
  542. results_translated = transform(
  543. copy.deepcopy(self.results_mask_boxtype))
  544. img_gt = np.array([[2, 3, 4, 0], [6, 7, 8, 0], [10, 11, 12,
  545. 0]]).astype(np.uint8)
  546. img_gt = np.stack([img_gt, img_gt, img_gt], axis=-1)
  547. img_gt[:, 3, :] = np.array(self.img_border_value)
  548. results_gt = copy.deepcopy(self.results_mask)
  549. results_gt['img'] = img_gt
  550. results_gt['gt_bboxes'] = HorizontalBoxes(
  551. np.array([[0, 0, 1, 2]], dtype=np.float32))
  552. gt_masks = np.array([[1, 0, 0, 0], [1, 1, 0, 0], [1, 0, 0, 0]],
  553. dtype=np.uint8)[None, :, :]
  554. results_gt['gt_masks'] = BitmapMasks(gt_masks, 3, 4)
  555. results_gt['gt_ignore_flags'] = np.array(np.array([1], dtype=bool))
  556. results_gt['gt_seg_map'] = np.array(
  557. [[13, 255, 255, 255], [13, 13, 255, 255],
  558. [13, 255, 255,
  559. 255]]).astype(self.results_mask['gt_seg_map'].dtype)
  560. check_result_same(results_gt, results_translated, self.check_keys)
  561. def test_repr(self):
  562. transform = TranslateX(prob=0.5, level=5)
  563. self.assertEqual(
  564. repr(transform), ('TranslateX(prob=0.5, '
  565. 'level=5, '
  566. 'min_mag=0.0, '
  567. 'max_mag=0.1, '
  568. 'reversal_prob=0.5, '
  569. 'img_border_value=(128.0, 128.0, 128.0), '
  570. 'mask_border_value=0, '
  571. 'seg_ignore_label=255, '
  572. 'interpolation=bilinear)'))
  573. class TestTranslateY(unittest.TestCase):
  574. def setUp(self):
  575. """Setup the model and optimizer which are used in every test method.
  576. TestCase calls functions in this order: setUp() -> testMethod() ->
  577. tearDown() -> cleanUp()
  578. """
  579. self.check_keys = ('img', 'gt_bboxes', 'gt_bboxes_labels', 'gt_masks',
  580. 'gt_ignore_flags', 'gt_seg_map')
  581. self.results_mask = construct_toy_data(poly2mask=True)
  582. self.results_poly = construct_toy_data(poly2mask=False)
  583. self.results_mask_boxtype = construct_toy_data(
  584. poly2mask=True, use_box_type=True)
  585. self.img_border_value = (104, 116, 124)
  586. self.seg_ignore_label = 255
  587. def test_translatey(self):
  588. # test assertion for invalid value of min_mag
  589. with self.assertRaises(AssertionError):
  590. transform = TranslateY(prob=0.5, level=2, min_mag=-1.0)
  591. # test assertion for invalid value of max_mag
  592. with self.assertRaises(AssertionError):
  593. transform = TranslateY(prob=0.5, level=2, max_mag=1.1)
  594. # test case when level=0 (without translate aug)
  595. transform = TranslateY(
  596. prob=1.0,
  597. level=0,
  598. img_border_value=self.img_border_value,
  599. seg_ignore_label=self.seg_ignore_label)
  600. results_wo_translatey = transform(copy.deepcopy(self.results_mask))
  601. check_result_same(self.results_mask, results_wo_translatey,
  602. self.check_keys)
  603. # test translate vertically, magnitude=-1
  604. transform = TranslateY(
  605. prob=1.0,
  606. level=10,
  607. max_mag=0.4,
  608. reversal_prob=0.0,
  609. seg_ignore_label=self.seg_ignore_label)
  610. results_translated = transform(copy.deepcopy(self.results_mask))
  611. img_gt = np.array([[5, 6, 7, 8], [9, 10, 11, 12],
  612. [128, 128, 128, 128]]).astype(np.uint8)
  613. img_gt = np.stack([img_gt, img_gt, img_gt], axis=-1)
  614. results_gt = copy.deepcopy(self.results_mask)
  615. results_gt['img'] = img_gt
  616. results_gt['gt_bboxes'] = np.array([[1, 0, 2, 1]], dtype=np.float32)
  617. gt_masks = np.array([[0, 1, 1, 0], [0, 1, 0, 0], [0, 0, 0, 0]],
  618. dtype=np.uint8)[None, :, :]
  619. results_gt['gt_masks'] = BitmapMasks(gt_masks, 3, 4)
  620. results_gt['gt_ignore_flags'] = np.array(np.array([1], dtype=bool))
  621. results_gt['gt_seg_map'] = np.array(
  622. [[255, 13, 13, 255], [255, 13, 255, 255],
  623. [255, 255, 255,
  624. 255]]).astype(self.results_mask['gt_seg_map'].dtype)
  625. check_result_same(results_gt, results_translated, self.check_keys)
  626. # test PolygonMasks with translate vertically.
  627. results_translated = transform(copy.deepcopy(self.results_poly))
  628. gt_masks = [[np.array([1, 1, 1, 0, 2, 0], dtype=np.float32)]]
  629. results_gt['gt_masks'] = PolygonMasks(gt_masks, 3, 4)
  630. check_result_same(results_gt, results_translated, self.check_keys)
  631. def test_translatey_use_box_type(self):
  632. # test case when level=0 (without translate aug)
  633. transform = TranslateY(
  634. prob=1.0,
  635. level=0,
  636. img_border_value=self.img_border_value,
  637. seg_ignore_label=self.seg_ignore_label)
  638. results_wo_translatey = transform(
  639. copy.deepcopy(self.results_mask_boxtype))
  640. check_result_same(self.results_mask_boxtype, results_wo_translatey,
  641. self.check_keys)
  642. # test translate vertically, magnitude=-1
  643. transform = TranslateY(
  644. prob=1.0,
  645. level=10,
  646. max_mag=0.4,
  647. reversal_prob=0.0,
  648. seg_ignore_label=self.seg_ignore_label)
  649. results_translated = transform(
  650. copy.deepcopy(self.results_mask_boxtype))
  651. img_gt = np.array([[5, 6, 7, 8], [9, 10, 11, 12],
  652. [128, 128, 128, 128]]).astype(np.uint8)
  653. img_gt = np.stack([img_gt, img_gt, img_gt], axis=-1)
  654. results_gt = copy.deepcopy(self.results_mask_boxtype)
  655. results_gt['img'] = img_gt
  656. results_gt['gt_bboxes'] = HorizontalBoxes(
  657. np.array([[1, 0, 2, 1]], dtype=np.float32))
  658. gt_masks = np.array([[0, 1, 1, 0], [0, 1, 0, 0], [0, 0, 0, 0]],
  659. dtype=np.uint8)[None, :, :]
  660. results_gt['gt_masks'] = BitmapMasks(gt_masks, 3, 4)
  661. results_gt['gt_ignore_flags'] = np.array(np.array([1], dtype=bool))
  662. results_gt['gt_seg_map'] = np.array(
  663. [[255, 13, 13, 255], [255, 13, 255, 255],
  664. [255, 255, 255,
  665. 255]]).astype(self.results_mask['gt_seg_map'].dtype)
  666. check_result_same(results_gt, results_translated, self.check_keys)
  667. def test_repr(self):
  668. transform = TranslateX(prob=0.5, level=5)
  669. self.assertEqual(
  670. repr(transform), ('TranslateX(prob=0.5, '
  671. 'level=5, '
  672. 'min_mag=0.0, '
  673. 'max_mag=0.1, '
  674. 'reversal_prob=0.5, '
  675. 'img_border_value=(128.0, 128.0, 128.0), '
  676. 'mask_border_value=0, '
  677. 'seg_ignore_label=255, '
  678. 'interpolation=bilinear)'))