123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752 |
- # Copyright (c) OpenMMLab. All rights reserved.
- import copy
- import unittest
- import numpy as np
- from mmdet.datasets.transforms import (GeomTransform, Rotate, ShearX, ShearY,
- TranslateX, TranslateY)
- from mmdet.structures.bbox import HorizontalBoxes
- from mmdet.structures.mask import BitmapMasks, PolygonMasks
- from .utils import check_result_same, construct_toy_data
- class TestGeomTransform(unittest.TestCase):
- def setUp(self):
- """Setup the model and optimizer which are used in every test method.
- TestCase calls functions in this order: setUp() -> testMethod() ->
- tearDown() -> cleanUp()
- """
- self.check_keys = ('img', 'gt_bboxes', 'gt_bboxes_labels', 'gt_masks',
- 'gt_ignore_flags', 'gt_seg_map')
- self.results_mask = construct_toy_data(poly2mask=True)
- self.img_border_value = (104, 116, 124)
- self.seg_ignore_label = 255
- def test_geomtransform(self):
- # test assertion for invalid prob
- with self.assertRaises(AssertionError):
- transform = GeomTransform(
- prob=-0.5, level=1, min_mag=0.0, max_mag=1.0)
- # test assertion for invalid value of level
- with self.assertRaises(AssertionError):
- transform = GeomTransform(
- prob=0.5, level=-1, min_mag=0.0, max_mag=1.0)
- # test assertion for invalid value of min_mag and max_mag
- with self.assertRaises(AssertionError):
- transform = ShearX(prob=0.5, level=2, min_mag=1.0, max_mag=0.0)
- # test assertion for the num of elements in tuple img_border_value
- with self.assertRaises(AssertionError):
- transform = GeomTransform(
- prob=0.5,
- level=1,
- min_mag=0.0,
- max_mag=1.0,
- img_border_value=(128, 128, 128, 128))
- # test ValueError for invalid type of img_border_value
- with self.assertRaises(ValueError):
- transform = GeomTransform(
- prob=0.5,
- level=1,
- min_mag=0.0,
- max_mag=1.0,
- img_border_value=[128, 128, 128])
- # test assertion for invalid value of img_border_value
- with self.assertRaises(AssertionError):
- transform = GeomTransform(
- prob=0.5,
- level=1,
- min_mag=0.0,
- max_mag=1.0,
- img_border_value=(128, -1, 256))
- # test case when no aug (prob=0)
- transform = GeomTransform(
- prob=0.,
- level=10,
- min_mag=0.0,
- max_mag=1.0,
- img_border_value=self.img_border_value)
- results_wo_aug = transform(copy.deepcopy(self.results_mask))
- check_result_same(self.results_mask, results_wo_aug, self.check_keys)
- def test_repr(self):
- transform = GeomTransform(
- prob=0.5,
- level=5,
- min_mag=0.0,
- max_mag=1.0,
- )
- self.assertEqual(
- repr(transform), ('GeomTransform(prob=0.5, '
- 'level=5, '
- 'min_mag=0.0, '
- 'max_mag=1.0, '
- 'reversal_prob=0.5, '
- 'img_border_value=(128.0, 128.0, 128.0), '
- 'mask_border_value=0, '
- 'seg_ignore_label=255, '
- 'interpolation=bilinear)'))
- class TestShearX(unittest.TestCase):
- def setUp(self):
- """Setup the model and optimizer which are used in every test method.
- TestCase calls functions in this order: setUp() -> testMethod() ->
- tearDown() -> cleanUp()
- """
- self.check_keys = ('img', 'gt_bboxes', 'gt_bboxes_labels', 'gt_masks',
- 'gt_ignore_flags', 'gt_seg_map')
- self.results_mask = construct_toy_data(poly2mask=True)
- self.results_poly = construct_toy_data(poly2mask=False)
- self.results_mask_boxtype = construct_toy_data(
- poly2mask=True, use_box_type=True)
- self.img_border_value = (104, 116, 124)
- self.seg_ignore_label = 255
- def test_shearx(self):
- # test assertion for invalid value of min_mag
- with self.assertRaises(AssertionError):
- transform = ShearX(prob=0.5, level=2, min_mag=-30.)
- # test assertion for invalid value of max_mag
- with self.assertRaises(AssertionError):
- transform = ShearX(prob=0.5, level=2, max_mag=100.)
- # test case when no shear horizontally (level=0)
- transform = ShearX(
- prob=1.0,
- level=0,
- img_border_value=self.img_border_value,
- seg_ignore_label=self.seg_ignore_label,
- )
- results_wo_shearx = transform(copy.deepcopy(self.results_mask))
- check_result_same(self.results_mask, results_wo_shearx,
- self.check_keys)
- # test shear horizontally, magnitude=-1
- transform = ShearX(
- prob=1.0,
- level=10,
- max_mag=45.,
- reversal_prob=1.0,
- img_border_value=self.img_border_value)
- results_sheared = transform(copy.deepcopy(self.results_mask))
- results_gt = copy.deepcopy(self.results_mask)
- img_gt = np.array([[1, 2, 3, 4], [0, 5, 6, 7], [0, 0, 9, 10]],
- dtype=np.uint8)
- img_gt = np.stack([img_gt, img_gt, img_gt], axis=-1)
- img_gt[1, 0, :] = np.array(self.img_border_value)
- img_gt[2, 0, :] = np.array(self.img_border_value)
- img_gt[2, 1, :] = np.array(self.img_border_value)
- results_gt['img'] = img_gt
- results_gt['gt_bboxes'] = np.array([[1, 0, 4, 2]], dtype=np.float32)
- results_gt['gt_bboxes_labels'] = np.array([13], dtype=np.int64)
- gt_masks = np.array([[0, 1, 0, 0], [0, 0, 1, 1], [0, 0, 0, 1]],
- dtype=np.uint8)[None, :, :]
- results_gt['gt_masks'] = BitmapMasks(gt_masks, 3, 4)
- results_gt['gt_ignore_flags'] = np.array(np.array([1], dtype=bool))
- results_gt['gt_seg_map'] = np.array(
- [[255, 13, 255, 255], [255, 255, 13, 13], [255, 255, 255, 13]],
- dtype=self.results_mask['gt_seg_map'].dtype)
- check_result_same(results_gt, results_sheared, self.check_keys)
- # test PolygonMasks with shear horizontally, magnitude=1
- results_sheared = transform(copy.deepcopy(self.results_poly))
- gt_masks = [[np.array([3, 2, 1, 0, 3, 1], dtype=np.float32)]]
- results_gt['gt_masks'] = PolygonMasks(gt_masks, 3, 4)
- check_result_same(results_gt, results_sheared, self.check_keys)
- def test_shearx_use_box_type(self):
- # test case when no shear horizontally (level=0)
- transform = ShearX(
- prob=1.0,
- level=0,
- img_border_value=self.img_border_value,
- seg_ignore_label=self.seg_ignore_label,
- )
- results_wo_shearx = transform(copy.deepcopy(self.results_mask_boxtype))
- check_result_same(self.results_mask_boxtype, results_wo_shearx,
- self.check_keys)
- # test shear horizontally, magnitude=-1
- transform = ShearX(
- prob=1.0,
- level=10,
- max_mag=45.,
- reversal_prob=1.0,
- img_border_value=self.img_border_value)
- results_sheared = transform(copy.deepcopy(self.results_mask_boxtype))
- results_gt = copy.deepcopy(self.results_mask_boxtype)
- img_gt = np.array([[1, 2, 3, 4], [0, 5, 6, 7], [0, 0, 9, 10]],
- dtype=np.uint8)
- img_gt = np.stack([img_gt, img_gt, img_gt], axis=-1)
- img_gt[1, 0, :] = np.array(self.img_border_value)
- img_gt[2, 0, :] = np.array(self.img_border_value)
- img_gt[2, 1, :] = np.array(self.img_border_value)
- results_gt['img'] = img_gt
- results_gt['gt_bboxes'] = HorizontalBoxes(
- np.array([[1, 0, 4, 2]], dtype=np.float32))
- results_gt['gt_bboxes_labels'] = np.array([13], dtype=np.int64)
- gt_masks = np.array([[0, 1, 0, 0], [0, 0, 1, 1], [0, 0, 0, 1]],
- dtype=np.uint8)[None, :, :]
- results_gt['gt_masks'] = BitmapMasks(gt_masks, 3, 4)
- results_gt['gt_ignore_flags'] = np.array(np.array([1], dtype=bool))
- results_gt['gt_seg_map'] = np.array(
- [[255, 13, 255, 255], [255, 255, 13, 13], [255, 255, 255, 13]],
- dtype=self.results_mask['gt_seg_map'].dtype)
- check_result_same(results_gt, results_sheared, self.check_keys)
- def test_repr(self):
- transform = ShearX(prob=0.5, level=10)
- self.assertEqual(
- repr(transform), ('ShearX(prob=0.5, '
- 'level=10, '
- 'min_mag=0.0, '
- 'max_mag=30.0, '
- 'reversal_prob=0.5, '
- 'img_border_value=(128.0, 128.0, 128.0), '
- 'mask_border_value=0, '
- 'seg_ignore_label=255, '
- 'interpolation=bilinear)'))
- class TestShearY(unittest.TestCase):
- def setUp(self):
- """Setup the model and optimizer which are used in every test method.
- TestCase calls functions in this order: setUp() -> testMethod() ->
- tearDown() -> cleanUp()
- """
- self.check_keys = ('img', 'gt_bboxes', 'gt_bboxes_labels', 'gt_masks',
- 'gt_ignore_flags', 'gt_seg_map')
- self.results_mask = construct_toy_data(poly2mask=True)
- self.results_poly = construct_toy_data(poly2mask=False)
- self.results_mask_boxtype = construct_toy_data(
- poly2mask=True, use_box_type=True)
- self.img_border_value = (104, 116, 124)
- self.seg_ignore_label = 255
- def test_sheary(self):
- # test assertion for invalid value of min_mag
- with self.assertRaises(AssertionError):
- transform = ShearY(prob=0.5, level=2, min_mag=-30.)
- # test assertion for invalid value of max_mag
- with self.assertRaises(AssertionError):
- transform = ShearY(prob=0.5, level=2, max_mag=100.)
- # test case when no shear vertically (level=0)
- transform = ShearY(
- prob=1.0,
- level=0,
- img_border_value=self.img_border_value,
- seg_ignore_label=self.seg_ignore_label,
- )
- results_wo_sheary = transform(copy.deepcopy(self.results_mask))
- check_result_same(self.results_mask, results_wo_sheary,
- self.check_keys)
- # test shear vertically, magnitude=1
- transform = ShearY(prob=1., level=10, max_mag=45., reversal_prob=0.)
- results_sheared = transform(copy.deepcopy(self.results_mask))
- results_gt = copy.deepcopy(self.results_mask)
- img_gt = np.array(
- [[1, 6, 11, 128], [5, 10, 128, 128], [9, 128, 128, 128]],
- dtype=np.uint8)
- img_gt = np.stack([img_gt, img_gt, img_gt], axis=-1)
- results_gt['img'] = img_gt
- results_gt['gt_bboxes'] = np.array([[1, 0, 2, 1]], dtype=np.float32)
- results_gt['gt_bboxes_labels'] = np.array([13], dtype=np.int64)
- gt_masks = np.array([[0, 1, 0, 0], [0, 1, 0, 0], [0, 0, 0, 0]],
- dtype=np.uint8)[None, :, :]
- results_gt['gt_masks'] = BitmapMasks(gt_masks, 3, 4)
- results_gt['gt_ignore_flags'] = np.array(np.array([1], dtype=bool))
- results_gt['gt_seg_map'] = np.array(
- [[255, 13, 255, 255], [255, 13, 255, 255], [255, 255, 255, 255]],
- dtype=self.results_mask['gt_seg_map'].dtype)
- check_result_same(results_gt, results_sheared, self.check_keys)
- # test PolygonMasks with shear vertically, magnitude=-1
- results_sheared = transform(copy.deepcopy(self.results_poly))
- gt_masks = [[np.array([1, 1, 1, 0, 2, 0], dtype=np.float32)]]
- results_gt['gt_masks'] = PolygonMasks(gt_masks, 3, 4)
- check_result_same(results_gt, results_sheared, self.check_keys)
- def test_sheary_use_box_type(self):
- # test case when no shear vertically (level=0)
- transform = ShearY(
- prob=1.0,
- level=0,
- img_border_value=self.img_border_value,
- seg_ignore_label=self.seg_ignore_label,
- )
- results_wo_sheary = transform(copy.deepcopy(self.results_mask_boxtype))
- check_result_same(self.results_mask_boxtype, results_wo_sheary,
- self.check_keys)
- # test shear vertically, magnitude=1
- transform = ShearY(prob=1., level=10, max_mag=45., reversal_prob=0.)
- results_sheared = transform(copy.deepcopy(self.results_mask_boxtype))
- results_gt = copy.deepcopy(self.results_mask_boxtype)
- img_gt = np.array(
- [[1, 6, 11, 128], [5, 10, 128, 128], [9, 128, 128, 128]],
- dtype=np.uint8)
- img_gt = np.stack([img_gt, img_gt, img_gt], axis=-1)
- results_gt['img'] = img_gt
- results_gt['gt_bboxes'] = HorizontalBoxes(
- np.array([[1, 0, 2, 1]], dtype=np.float32))
- results_gt['gt_bboxes_labels'] = np.array([13], dtype=np.int64)
- gt_masks = np.array([[0, 1, 0, 0], [0, 1, 0, 0], [0, 0, 0, 0]],
- dtype=np.uint8)[None, :, :]
- results_gt['gt_masks'] = BitmapMasks(gt_masks, 3, 4)
- results_gt['gt_ignore_flags'] = np.array(np.array([1], dtype=bool))
- results_gt['gt_seg_map'] = np.array(
- [[255, 13, 255, 255], [255, 13, 255, 255], [255, 255, 255, 255]],
- dtype=self.results_mask['gt_seg_map'].dtype)
- check_result_same(results_gt, results_sheared, self.check_keys)
- def test_repr(self):
- transform = ShearY(prob=0.5, level=10)
- self.assertEqual(
- repr(transform), ('ShearY(prob=0.5, '
- 'level=10, '
- 'min_mag=0.0, '
- 'max_mag=30.0, '
- 'reversal_prob=0.5, '
- 'img_border_value=(128.0, 128.0, 128.0), '
- 'mask_border_value=0, '
- 'seg_ignore_label=255, '
- 'interpolation=bilinear)'))
- class TestRotate(unittest.TestCase):
- def setUp(self):
- """Setup the model and optimizer which are used in every test method.
- TestCase calls functions in this order: setUp() -> testMethod() ->
- tearDown() -> cleanUp()
- """
- self.check_keys = ('img', 'gt_bboxes', 'gt_bboxes_labels', 'gt_masks',
- 'gt_ignore_flags', 'gt_seg_map')
- self.results_mask = construct_toy_data(poly2mask=True)
- self.results_poly = construct_toy_data(poly2mask=False)
- self.results_mask_boxtype = construct_toy_data(
- poly2mask=True, use_box_type=True)
- self.img_border_value = (104, 116, 124)
- self.seg_ignore_label = 255
- def test_rotate(self):
- # test assertion for invalid value of min_mag
- with self.assertRaises(AssertionError):
- transform = ShearY(prob=0.5, level=2, min_mag=-90.0)
- # test assertion for invalid value of max_mag
- with self.assertRaises(AssertionError):
- transform = ShearY(prob=0.5, level=2, max_mag=270.0)
- # test case when no rotate aug (level=0)
- transform = Rotate(
- prob=1.,
- level=0,
- img_border_value=self.img_border_value,
- seg_ignore_label=self.seg_ignore_label,
- )
- results_wo_rotate = transform(copy.deepcopy(self.results_mask))
- check_result_same(self.results_mask, results_wo_rotate,
- self.check_keys)
- # test clockwise rotation with angle 90
- transform = Rotate(
- prob=1.,
- level=10,
- max_mag=90.0,
- # set reversal_prob to 1 for clockwise rotation
- reversal_prob=1.,
- )
- results_rotated = transform(copy.deepcopy(self.results_mask))
- # The image, masks, and semantic segmentation map
- # will be bilinearly interpolated.
- img_gt = np.array([[69, 8, 4, 65], [69, 9, 5, 65],
- [70, 10, 6, 66]]).astype(np.uint8)
- img_gt = np.stack([img_gt, img_gt, img_gt], axis=-1)
- results_gt = copy.deepcopy(self.results_mask)
- results_gt['img'] = img_gt
- results_gt['gt_bboxes'] = np.array([[0.5, 0.5, 2.5, 1.5]],
- dtype=np.float32)
- gt_masks = np.array([[0, 1, 1, 0], [0, 1, 1, 0], [0, 0, 0, 0]],
- dtype=np.uint8)[None, :, :]
- results_gt['gt_masks'] = BitmapMasks(gt_masks, 3, 4)
- results_gt['gt_ignore_flags'] = np.array(np.array([1], dtype=bool))
- results_gt['gt_seg_map'] = np.array(
- [[255, 13, 13, 13], [255, 255, 13, 255],
- [255, 255, 255,
- 255]]).astype(self.results_mask['gt_seg_map'].dtype)
- check_result_same(results_gt, results_rotated, self.check_keys)
- # test clockwise rotation with angle 90, PolygonMasks
- results_rotated = transform(copy.deepcopy(self.results_poly))
- gt_masks = [[np.array([0, 1, 0, 1, 0, 2], dtype=np.float)]]
- results_gt['gt_masks'] = PolygonMasks(gt_masks, 3, 4)
- check_result_same(results_gt, results_rotated, self.check_keys)
- # test counter-clockwise rotation with angle 90
- transform = Rotate(
- prob=1.0,
- level=10,
- max_mag=90.0,
- # set reversal_prob to 0 for counter-clockwise rotation
- reversal_prob=0.0,
- )
- results_rotated = transform(copy.deepcopy(self.results_mask))
- # The image, masks, and semantic segmentation map
- # will be bilinearly interpolated.
- img_gt = np.array([[66, 6, 10, 70], [65, 5, 9, 69],
- [65, 4, 8, 69]]).astype(np.uint8)
- img_gt = np.stack([img_gt, img_gt, img_gt], axis=-1)
- results_gt = copy.deepcopy(self.results_mask)
- results_gt['img'] = img_gt
- results_gt['gt_bboxes'] = np.array([[0.5, 0.5, 2.5, 1.5]],
- dtype=np.float32)
- gt_masks = np.array([[0, 0, 0, 0], [0, 1, 1, 0], [0, 1, 1, 0]],
- dtype=np.uint8)[None, :, :]
- results_gt['gt_masks'] = BitmapMasks(gt_masks, 3, 4)
- results_gt['gt_ignore_flags'] = np.array(np.array([1], dtype=bool))
- results_gt['gt_seg_map'] = np.array(
- [[255, 255, 255, 255], [255, 13, 255, 255],
- [13, 13, 13, 255]]).astype(self.results_mask['gt_seg_map'].dtype)
- check_result_same(results_gt, results_rotated, self.check_keys)
- # test counter-clockwise rotation with angle 90, PolygonMasks
- results_rotated = transform(copy.deepcopy(self.results_poly))
- gt_masks = [[np.array([2, 0, 0, 0, 1, 0], dtype=np.float)]]
- results_gt['gt_masks'] = PolygonMasks(gt_masks, 3, 4)
- check_result_same(results_gt, results_rotated, self.check_keys)
- def test_rotate_use_box_type(self):
- # test case when no rotate aug (level=0)
- transform = Rotate(
- prob=1.,
- level=0,
- img_border_value=self.img_border_value,
- seg_ignore_label=self.seg_ignore_label,
- )
- results_wo_rotate = transform(copy.deepcopy(self.results_mask_boxtype))
- check_result_same(self.results_mask_boxtype, results_wo_rotate,
- self.check_keys)
- # test clockwise rotation with angle 90
- transform = Rotate(
- prob=1.,
- level=10,
- max_mag=90.0,
- # set reversal_prob to 1 for clockwise rotation
- reversal_prob=1.,
- )
- results_rotated = transform(copy.deepcopy(self.results_mask_boxtype))
- # The image, masks, and semantic segmentation map
- # will be bilinearly interpolated.
- img_gt = np.array([[69, 8, 4, 65], [69, 9, 5, 65],
- [70, 10, 6, 66]]).astype(np.uint8)
- img_gt = np.stack([img_gt, img_gt, img_gt], axis=-1)
- results_gt = copy.deepcopy(self.results_mask_boxtype)
- results_gt['img'] = img_gt
- results_gt['gt_bboxes'] = HorizontalBoxes(
- np.array([[0.5, 0.5, 2.5, 1.5]], dtype=np.float32))
- gt_masks = np.array([[0, 1, 1, 0], [0, 1, 1, 0], [0, 0, 0, 0]],
- dtype=np.uint8)[None, :, :]
- results_gt['gt_masks'] = BitmapMasks(gt_masks, 3, 4)
- results_gt['gt_ignore_flags'] = np.array(np.array([1], dtype=bool))
- results_gt['gt_seg_map'] = np.array(
- [[255, 13, 13, 13], [255, 255, 13, 255],
- [255, 255, 255,
- 255]]).astype(self.results_mask['gt_seg_map'].dtype)
- check_result_same(results_gt, results_rotated, self.check_keys)
- # test counter-clockwise rotation with angle 90
- transform = Rotate(
- prob=1.0,
- level=10,
- max_mag=90.0,
- # set reversal_prob to 0 for counter-clockwise rotation
- reversal_prob=0.0,
- )
- results_rotated = transform(copy.deepcopy(self.results_mask_boxtype))
- # The image, masks, and semantic segmentation map
- # will be bilinearly interpolated.
- img_gt = np.array([[66, 6, 10, 70], [65, 5, 9, 69],
- [65, 4, 8, 69]]).astype(np.uint8)
- img_gt = np.stack([img_gt, img_gt, img_gt], axis=-1)
- results_gt = copy.deepcopy(self.results_mask_boxtype)
- results_gt['img'] = img_gt
- results_gt['gt_bboxes'] = HorizontalBoxes(
- np.array([[0.5, 0.5, 2.5, 1.5]], dtype=np.float32))
- gt_masks = np.array([[0, 0, 0, 0], [0, 1, 1, 0], [0, 1, 1, 0]],
- dtype=np.uint8)[None, :, :]
- results_gt['gt_masks'] = BitmapMasks(gt_masks, 3, 4)
- results_gt['gt_ignore_flags'] = np.array(np.array([1], dtype=bool))
- results_gt['gt_seg_map'] = np.array(
- [[255, 255, 255, 255], [255, 13, 255, 255],
- [13, 13, 13, 255]]).astype(self.results_mask['gt_seg_map'].dtype)
- check_result_same(results_gt, results_rotated, self.check_keys)
- def test_repr(self):
- transform = Rotate(prob=0.5, level=5)
- self.assertEqual(
- repr(transform), ('Rotate(prob=0.5, '
- 'level=5, '
- 'min_mag=0.0, '
- 'max_mag=30.0, '
- 'reversal_prob=0.5, '
- 'img_border_value=(128.0, 128.0, 128.0), '
- 'mask_border_value=0, '
- 'seg_ignore_label=255, '
- 'interpolation=bilinear)'))
- class TestTranslateX(unittest.TestCase):
- def setUp(self):
- """Setup the model and optimizer which are used in every test method.
- TestCase calls functions in this order: setUp() -> testMethod() ->
- tearDown() -> cleanUp()
- """
- self.check_keys = ('img', 'gt_bboxes', 'gt_bboxes_labels', 'gt_masks',
- 'gt_ignore_flags', 'gt_seg_map')
- self.results_mask = construct_toy_data(poly2mask=True)
- self.results_poly = construct_toy_data(poly2mask=False)
- self.results_mask_boxtype = construct_toy_data(
- poly2mask=True, use_box_type=True)
- self.img_border_value = (104, 116, 124)
- self.seg_ignore_label = 255
- def test_translatex(self):
- # test assertion for invalid value of min_mag
- with self.assertRaises(AssertionError):
- transform = TranslateX(prob=0.5, level=2, min_mag=-1.)
- # test assertion for invalid value of max_mag
- with self.assertRaises(AssertionError):
- transform = TranslateX(prob=0.5, level=2, max_mag=1.1)
- # test case when level=0 (without translate aug)
- transform = TranslateX(
- prob=1.0,
- level=0,
- img_border_value=self.img_border_value,
- seg_ignore_label=self.seg_ignore_label)
- results_wo_translatex = transform(copy.deepcopy(self.results_mask))
- check_result_same(self.results_mask, results_wo_translatex,
- self.check_keys)
- # test translate horizontally, magnitude=-1
- transform = TranslateX(
- prob=1.0,
- level=10,
- max_mag=0.3,
- reversal_prob=0.0,
- img_border_value=self.img_border_value,
- seg_ignore_label=self.seg_ignore_label)
- results_translated = transform(copy.deepcopy(self.results_mask))
- img_gt = np.array([[2, 3, 4, 0], [6, 7, 8, 0], [10, 11, 12,
- 0]]).astype(np.uint8)
- img_gt = np.stack([img_gt, img_gt, img_gt], axis=-1)
- img_gt[:, 3, :] = np.array(self.img_border_value)
- results_gt = copy.deepcopy(self.results_mask)
- results_gt['img'] = img_gt
- results_gt['gt_bboxes'] = np.array([[0, 0, 1, 2]], dtype=np.float32)
- gt_masks = np.array([[1, 0, 0, 0], [1, 1, 0, 0], [1, 0, 0, 0]],
- dtype=np.uint8)[None, :, :]
- results_gt['gt_masks'] = BitmapMasks(gt_masks, 3, 4)
- results_gt['gt_ignore_flags'] = np.array(np.array([1], dtype=bool))
- results_gt['gt_seg_map'] = np.array(
- [[13, 255, 255, 255], [13, 13, 255, 255],
- [13, 255, 255,
- 255]]).astype(self.results_mask['gt_seg_map'].dtype)
- check_result_same(results_gt, results_translated, self.check_keys)
- # test PolygonMasks with translate horizontally.
- results_translated = transform(copy.deepcopy(self.results_poly))
- gt_masks = [[np.array([0, 2, 0, 0, 1, 1], dtype=np.float32)]]
- results_gt['gt_masks'] = PolygonMasks(gt_masks, 3, 4)
- check_result_same(results_gt, results_translated, self.check_keys)
- def test_translatex_use_box_type(self):
- # test case when level=0 (without translate aug)
- transform = TranslateX(
- prob=1.0,
- level=0,
- img_border_value=self.img_border_value,
- seg_ignore_label=self.seg_ignore_label)
- results_wo_translatex = transform(
- copy.deepcopy(self.results_mask_boxtype))
- check_result_same(self.results_mask_boxtype, results_wo_translatex,
- self.check_keys)
- # test translate horizontally, magnitude=-1
- transform = TranslateX(
- prob=1.0,
- level=10,
- max_mag=0.3,
- reversal_prob=0.0,
- img_border_value=self.img_border_value,
- seg_ignore_label=self.seg_ignore_label)
- results_translated = transform(
- copy.deepcopy(self.results_mask_boxtype))
- img_gt = np.array([[2, 3, 4, 0], [6, 7, 8, 0], [10, 11, 12,
- 0]]).astype(np.uint8)
- img_gt = np.stack([img_gt, img_gt, img_gt], axis=-1)
- img_gt[:, 3, :] = np.array(self.img_border_value)
- results_gt = copy.deepcopy(self.results_mask)
- results_gt['img'] = img_gt
- results_gt['gt_bboxes'] = HorizontalBoxes(
- np.array([[0, 0, 1, 2]], dtype=np.float32))
- gt_masks = np.array([[1, 0, 0, 0], [1, 1, 0, 0], [1, 0, 0, 0]],
- dtype=np.uint8)[None, :, :]
- results_gt['gt_masks'] = BitmapMasks(gt_masks, 3, 4)
- results_gt['gt_ignore_flags'] = np.array(np.array([1], dtype=bool))
- results_gt['gt_seg_map'] = np.array(
- [[13, 255, 255, 255], [13, 13, 255, 255],
- [13, 255, 255,
- 255]]).astype(self.results_mask['gt_seg_map'].dtype)
- check_result_same(results_gt, results_translated, self.check_keys)
- def test_repr(self):
- transform = TranslateX(prob=0.5, level=5)
- self.assertEqual(
- repr(transform), ('TranslateX(prob=0.5, '
- 'level=5, '
- 'min_mag=0.0, '
- 'max_mag=0.1, '
- 'reversal_prob=0.5, '
- 'img_border_value=(128.0, 128.0, 128.0), '
- 'mask_border_value=0, '
- 'seg_ignore_label=255, '
- 'interpolation=bilinear)'))
- class TestTranslateY(unittest.TestCase):
- def setUp(self):
- """Setup the model and optimizer which are used in every test method.
- TestCase calls functions in this order: setUp() -> testMethod() ->
- tearDown() -> cleanUp()
- """
- self.check_keys = ('img', 'gt_bboxes', 'gt_bboxes_labels', 'gt_masks',
- 'gt_ignore_flags', 'gt_seg_map')
- self.results_mask = construct_toy_data(poly2mask=True)
- self.results_poly = construct_toy_data(poly2mask=False)
- self.results_mask_boxtype = construct_toy_data(
- poly2mask=True, use_box_type=True)
- self.img_border_value = (104, 116, 124)
- self.seg_ignore_label = 255
- def test_translatey(self):
- # test assertion for invalid value of min_mag
- with self.assertRaises(AssertionError):
- transform = TranslateY(prob=0.5, level=2, min_mag=-1.0)
- # test assertion for invalid value of max_mag
- with self.assertRaises(AssertionError):
- transform = TranslateY(prob=0.5, level=2, max_mag=1.1)
- # test case when level=0 (without translate aug)
- transform = TranslateY(
- prob=1.0,
- level=0,
- img_border_value=self.img_border_value,
- seg_ignore_label=self.seg_ignore_label)
- results_wo_translatey = transform(copy.deepcopy(self.results_mask))
- check_result_same(self.results_mask, results_wo_translatey,
- self.check_keys)
- # test translate vertically, magnitude=-1
- transform = TranslateY(
- prob=1.0,
- level=10,
- max_mag=0.4,
- reversal_prob=0.0,
- seg_ignore_label=self.seg_ignore_label)
- results_translated = transform(copy.deepcopy(self.results_mask))
- img_gt = np.array([[5, 6, 7, 8], [9, 10, 11, 12],
- [128, 128, 128, 128]]).astype(np.uint8)
- img_gt = np.stack([img_gt, img_gt, img_gt], axis=-1)
- results_gt = copy.deepcopy(self.results_mask)
- results_gt['img'] = img_gt
- results_gt['gt_bboxes'] = np.array([[1, 0, 2, 1]], dtype=np.float32)
- gt_masks = np.array([[0, 1, 1, 0], [0, 1, 0, 0], [0, 0, 0, 0]],
- dtype=np.uint8)[None, :, :]
- results_gt['gt_masks'] = BitmapMasks(gt_masks, 3, 4)
- results_gt['gt_ignore_flags'] = np.array(np.array([1], dtype=bool))
- results_gt['gt_seg_map'] = np.array(
- [[255, 13, 13, 255], [255, 13, 255, 255],
- [255, 255, 255,
- 255]]).astype(self.results_mask['gt_seg_map'].dtype)
- check_result_same(results_gt, results_translated, self.check_keys)
- # test PolygonMasks with translate vertically.
- results_translated = transform(copy.deepcopy(self.results_poly))
- gt_masks = [[np.array([1, 1, 1, 0, 2, 0], dtype=np.float32)]]
- results_gt['gt_masks'] = PolygonMasks(gt_masks, 3, 4)
- check_result_same(results_gt, results_translated, self.check_keys)
- def test_translatey_use_box_type(self):
- # test case when level=0 (without translate aug)
- transform = TranslateY(
- prob=1.0,
- level=0,
- img_border_value=self.img_border_value,
- seg_ignore_label=self.seg_ignore_label)
- results_wo_translatey = transform(
- copy.deepcopy(self.results_mask_boxtype))
- check_result_same(self.results_mask_boxtype, results_wo_translatey,
- self.check_keys)
- # test translate vertically, magnitude=-1
- transform = TranslateY(
- prob=1.0,
- level=10,
- max_mag=0.4,
- reversal_prob=0.0,
- seg_ignore_label=self.seg_ignore_label)
- results_translated = transform(
- copy.deepcopy(self.results_mask_boxtype))
- img_gt = np.array([[5, 6, 7, 8], [9, 10, 11, 12],
- [128, 128, 128, 128]]).astype(np.uint8)
- img_gt = np.stack([img_gt, img_gt, img_gt], axis=-1)
- results_gt = copy.deepcopy(self.results_mask_boxtype)
- results_gt['img'] = img_gt
- results_gt['gt_bboxes'] = HorizontalBoxes(
- np.array([[1, 0, 2, 1]], dtype=np.float32))
- gt_masks = np.array([[0, 1, 1, 0], [0, 1, 0, 0], [0, 0, 0, 0]],
- dtype=np.uint8)[None, :, :]
- results_gt['gt_masks'] = BitmapMasks(gt_masks, 3, 4)
- results_gt['gt_ignore_flags'] = np.array(np.array([1], dtype=bool))
- results_gt['gt_seg_map'] = np.array(
- [[255, 13, 13, 255], [255, 13, 255, 255],
- [255, 255, 255,
- 255]]).astype(self.results_mask['gt_seg_map'].dtype)
- check_result_same(results_gt, results_translated, self.check_keys)
- def test_repr(self):
- transform = TranslateX(prob=0.5, level=5)
- self.assertEqual(
- repr(transform), ('TranslateX(prob=0.5, '
- 'level=5, '
- 'min_mag=0.0, '
- 'max_mag=0.1, '
- 'reversal_prob=0.5, '
- 'img_border_value=(128.0, 128.0, 128.0), '
- 'mask_border_value=0, '
- 'seg_ignore_label=255, '
- 'interpolation=bilinear)'))
|