From ce3d6618ce2c040de0eab475637e4e7b5220d53a Mon Sep 17 00:00:00 2001 From: wdmwhh <38210459+wdmwhh@users.noreply.github.com> Date: Thu, 24 Mar 2022 15:18:02 +0800 Subject: [PATCH] [Fix] `flip_ratio` in `RandomAffine` pipeline is inverted (#799) * fix flip_ratio bug original: `flip_ratio=1.` means always no flip and `flip_ratio=0.` means always flip after: `flip_ratio=1.` means always flip and `flip_ratio=0.` means always no flip * add some notes to modification * modify center, add unittests * clean test codes * clean test codes Co-authored-by: wangruohui <12756472+wangruohui@users.noreply.github.com> --- mmedit/datasets/pipelines/augmentation.py | 8 +++++-- .../test_pipelines/test_augmentation.py | 21 +++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/mmedit/datasets/pipelines/augmentation.py b/mmedit/datasets/pipelines/augmentation.py index 0e876a7454..be6a1dafcc 100644 --- a/mmedit/datasets/pipelines/augmentation.py +++ b/mmedit/datasets/pipelines/augmentation.py @@ -447,7 +447,11 @@ def _get_params(degrees, translate, scale_ranges, shears, flip_ratio, else: shear = 0.0 - flip = (np.random.rand(2) < flip_ratio).astype(np.int32) * 2 - 1 + # Because `flip` is used as a multiplier in line 479 and 480, + # so -1 stands for flip and 1 stands for no flip. Thus `flip` + # should be an 'inverse' flag as the result of the comparison. + # See https://github.com/open-mmlab/mmediting/pull/799 for more detail + flip = (np.random.rand(2) > flip_ratio).astype(np.int32) * 2 - 1 return angle, translations, scale, shear, flip @@ -521,7 +525,7 @@ def __call__(self, results): params = self._get_params(self.degrees, self.translate, self.scale, self.shear, self.flip_ratio, (h, w)) - center = (w * 0.5 + 0.5, h * 0.5 + 0.5) + center = (w * 0.5 - 0.5, h * 0.5 - 0.5) M = self._get_inverse_affine_matrix(center, *params) M = np.array(M).reshape((2, 3)) diff --git a/tests/test_data/test_pipelines/test_augmentation.py b/tests/test_data/test_pipelines/test_augmentation.py index b10a240449..a98f190cf3 100644 --- a/tests/test_data/test_pipelines/test_augmentation.py +++ b/tests/test_data/test_pipelines/test_augmentation.py @@ -265,7 +265,28 @@ def test_random_affine(self): target_keys = ['fg', 'alpha'] + # Test identical transformation + alpha = np.random.rand(4, 4).astype(np.float32) + fg = np.random.rand(4, 4).astype(np.float32) + results = dict(alpha=alpha, fg=fg) + random_affine = RandomAffine(['fg', 'alpha'], + degrees=0, flip_ratio=0.0) + random_affine_results = random_affine(results) + assert np.allclose(alpha, random_affine_results['alpha']) + assert np.allclose(fg, random_affine_results['fg']) + + # Test flip in both direction + alpha = np.random.rand(4, 4).astype(np.float32) + fg = np.random.rand(4, 4).astype(np.float32) + results = dict(alpha=alpha, fg=fg) + random_affine = RandomAffine(['fg', 'alpha'], + degrees=0, flip_ratio=1.0) + random_affine_results = random_affine(results) + assert np.allclose(alpha[::-1, ::-1], random_affine_results['alpha']) + assert np.allclose(fg[::-1, ::-1], random_affine_results['fg']) + # test random affine with different valid setting combinations + # only shape are tested alpha = np.random.rand(240, 320).astype(np.float32) fg = np.random.rand(240, 320).astype(np.float32) results = dict(alpha=alpha, fg=fg)