Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] flip_ratio in RandomAffine pipeline is inverted #799

Merged
merged 5 commits into from
Mar 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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