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

Fixed MultiplicativeNoise for case with image shape [h, w, 1] #793

Merged
merged 4 commits into from
May 15, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion albumentations/augmentations/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -2706,7 +2706,7 @@ def get_params_dependent_on_targets(self, params):
shape = [c]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is not related to the bugfix itself. It points to the possible bug in implementation, when per_channel = True. In this case, the condition check will set shape to shape = [c], which I read as "apply same noise vector of [c] channels to all pixels". Probably, it should be shape = [h,w,1] to apply image-level noise to all channels instead.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@BloodAxe
I a little bit confused what you mean.
Do you mean that in your mind condition per_channel means that we need to generate noise with shape [h, w]?
In my mind condition per_channel=True means that we will use c equal to image c shape when we produce noise tensor, else we use c=1.
If elementwise=True we set [h, w] of noise vector equal to image [h, w]. If elementwise=False noise vector h = w = 1


multiplier = np.random.uniform(self.multiplier[0], self.multiplier[1], shape)
if F.is_grayscale_image(img):
if F.is_grayscale_image(img) and img.ndim == 2:
multiplier = np.squeeze(multiplier)

return {"multiplier": multiplier}
Expand Down
8 changes: 7 additions & 1 deletion tests/test_transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,13 @@ def test_resize_keypoints():


@pytest.mark.parametrize(
"image", [np.random.randint(0, 256, [256, 320], np.uint8), np.random.random([256, 320]).astype(np.float32)]
"image",
[
np.random.randint(0, 256, [256, 320], np.uint8),
np.random.random([256, 320]).astype(np.float32),
np.random.randint(0, 256, [256, 320, 1], np.uint8),
np.random.random([256, 320, 1]).astype(np.float32),
],
)
def test_multiplicative_noise_grayscale(image):
m = 0.5
Expand Down