Skip to content

Commit

Permalink
[Fix] flip_ratio in RandomAffine pipeline is inverted (#799)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
wdmwhh and wangruohui authored Mar 24, 2022
1 parent 4ff208e commit 051a9f7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
8 changes: 6 additions & 2 deletions mmedit/datasets/pipelines/augmentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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))

Expand Down
21 changes: 21 additions & 0 deletions tests/test_data/test_pipelines/test_augmentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 051a9f7

Please sign in to comment.