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

Add gaussian noise transform #6192 #6233

Closed

Conversation

parth-shastri
Copy link

@parth-shastri parth-shastri commented Jul 3, 2022

Fixes #6192

Adds GaussianNoise transform functionality to the torchvision.transform.

-Basic GaussianNoise augmentation technique

@facebook-github-bot
Copy link

Hi @parth-shastri!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at [email protected]. Thanks!

@parth-shastri
Copy link
Author

This PR addresses the issue #6192.

@facebook-github-bot
Copy link

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

Copy link
Contributor

@oke-aditya oke-aditya left a comment

Choose a reason for hiding this comment

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

Hi @parth-shastri thanks a lot for the PR.

While I haven't gone through thorough details and verified the implementation fully. I had few code style and comments.

Also,

torchvision/_C.pyd

file is unnecessary. Is it because you are using windows and this file is generated while compiling torchvision from source?

I am sure @vfdev-5 is champion in this domain and will have a deep look 😄

torchvision/transforms/transforms.py Outdated Show resolved Hide resolved
Returns:
PIL Image or Tensor: Image added with gaussian noise.
"""
if not isinstance(image, torch.Tensor):
Copy link
Contributor

@oke-aditya oke-aditya Jul 3, 2022

Choose a reason for hiding this comment

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

I'm bit hesitant about this implementation. Why not create F.gaussian_noise that would work well on PIL Images and tensors which we can then use here?

See https://github.com/pytorch/vision/blob/main/torchvision/transforms/functional.py#L1338

Copy link
Collaborator

Choose a reason for hiding this comment

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

I agree, let's create a functional op F.gaussian_noise and at first iteration we can have tensor implementation. If input is PIL, we can convert it to tensor, apply the op and get back PIL image type for the output.

Copy link
Collaborator

Choose a reason for hiding this comment

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

@parth-shastri please create a functional op F.gaussian_noise

Copy link
Collaborator

Choose a reason for hiding this comment

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

@parth-shastri please address this comment

Copy link
Author

Choose a reason for hiding this comment

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

Yes working on that

test/test_transforms.py Outdated Show resolved Hide resolved
torchvision/transforms/transforms.py Outdated Show resolved Hide resolved
torchvision/transforms/transforms.py Outdated Show resolved Hide resolved
torchvision/transforms/transforms.py Show resolved Hide resolved
@datumbox
Copy link
Contributor

datumbox commented Jul 4, 2022

@parth-shastri Thanks for the contribution.

As noted at #6192 (comment), we are currently reviewing the API of Transforms so ideally we don't want to introduce new ones while working on it. Perhaps we could add this directly to the new API as a means of offering incentives to the users to migrate. @vfdev-5 wdyt?

@vfdev-5
Copy link
Collaborator

vfdev-5 commented Jul 4, 2022

@parth-shastri Thanks for the contribution.

As noted at #6192 (comment), we are currently reviewing the API of Transforms so ideally we don't want to introduce new ones while working on it. Perhaps we could add this directly to the new API as a means of offering incentives to the users to migrate. @vfdev-5 wdyt?

I think it is OK, this transformation is applied on images and meanwhile we are coding/reviewing new API, we can accept small transforms like this one.

@vfdev-5
Copy link
Collaborator

vfdev-5 commented Jul 7, 2022

@parth-shastri can you please fix failing job "ci/circleci: lint_python_and_config"

Comment on lines 127 to 129
noisy = T.GaussianNoise(mean=0.0, sigma=(0.1, 5))
noisy_imgs = [noisy(orig_img) for _ in range(4)]
plot(noisy_imgs)
Copy link
Collaborator

Choose a reason for hiding this comment

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

This gives the following result: https://output.circle-artifacts.com/output/job/34f9c3d8-231e-4748-81b9-cda1724a9f16/artifacts/0/docs/auto_examples/plot_transforms.html#gaussiannoise which does not make sense. Let's reduce the number of transformed images and propose acceptable range of sigmas

Copy link
Author

Choose a reason for hiding this comment

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

I think we should not keep an accepted range of sigmas, just let the user decide how much distortion does he want ?

Copy link
Author

Choose a reason for hiding this comment

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

Fixed the documentation images in the documentation.

gallery/plot_transforms.py Outdated Show resolved Hide resolved
test/test_transforms.py Outdated Show resolved Hide resolved
torchvision/transforms/transforms.py Outdated Show resolved Hide resolved
Returns:
PIL Image or Tensor: Image added with gaussian noise.
"""
if not isinstance(image, torch.Tensor):
Copy link
Collaborator

Choose a reason for hiding this comment

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

@parth-shastri please address this comment

@vfdev-5 vfdev-5 self-assigned this Jul 25, 2022
@parth-shastri
Copy link
Author

Are we stuck on this? @parth-shastri do you need help?
Hey, thanks I am not able to decide on a valid sigma to display
and also is there a way that we can visualize the results in the documentation before the commit?
apart from the deciding of sigma is there anything else that I'm supposed to do?

@oke-aditya
Copy link
Contributor

You can visualise the results by building the docs.
Also I think you need to refactor the code to create a functional API.

You can just try doing a grid search (or just try a few values and post the images here)

cc @vfdev-5 any suggestions for Sigma ?

@parth-shastri
Copy link
Author

Hi, tried out some sigmas and found that the range [0.1, 0.5] works.
gaussian_astronaut
gaussian_astronaut

Copy link
Collaborator

@vfdev-5 vfdev-5 left a comment

Choose a reason for hiding this comment

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

Thanks for the update @parth-shastri
I left few other comment to improve the PR

torchvision/transforms/functional_tensor.py Outdated Show resolved Hide resolved
],
)
# add the gaussian noise with the given mean and sigma.
normalize_img = img / 255.0
Copy link
Collaborator

Choose a reason for hiding this comment

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

We can't make this assumption that img range is [0, 255].
By default, we assume: [0, 1] for float images and [0, 255] for uint8 RGB images.
Let's not rescale image range and let user pick appropriate mean and sigma according to their data range.

@@ -748,8 +748,6 @@ def gaussian_blur(img: Tensor, kernel_size: List[int], sigma: List[float]) -> Te
kernel.dtype,
],
)

# padding = (left, right, top, bottom)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's keep this comment

Comment on lines 1429 to 1433
if sigma is None:
raise ValueError("The value of sigma cannot be None.")

if sigma is not None and not isinstance(sigma, (int, float, list, tuple)):
raise TypeError(f"sigma should be either float or sequence of floats. Got {type(sigma)}")
Copy link
Collaborator

Choose a reason for hiding this comment

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

Something is unclear here about whether sigma is Optional or not. Docstring and typehint says optional, but you raise an error if sigma is None. Let's remove optional and assume that sigma is not None

Copy link
Collaborator

Choose a reason for hiding this comment

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

@parth-shastri check above comment and the code. Why do you check sigma is None if sigma is not intended to be Optional.

@parth-shastri
Copy link
Author

Hi @vfdev-5 Anything more that you want me to do
Did not get the build error resolved can you help?

@parth-shastri
Copy link
Author

@vfdev-5 is there anything more to do?

@parth-shastri
Copy link
Author

@oke-aditya @vfdev-5 can you please highlight any changes I need to make in order to merge this pull request?

@javirk
Copy link

javirk commented Nov 28, 2023

Hi. I think it would be valuable to have Gaussian Noise as a transformation. What is the state of this at the moment? Should I take over this PR in order to finally merge it?

@vfdev-5
Copy link
Collaborator

vfdev-5 commented Nov 28, 2023

The problem with this PR right now is that it should add code compatible to the new v2 API.
I think it would be easier to start from a new PR and add new transform and all functional part. @javirk if you are interested in working on that, feel free to provide another PR. You may cherry-pick some commits on the gallery to acknowledge the work of @parth-shastri.

@parth-shastri
Copy link
Author

The problem with this PR right now is that it should add code compatible to the new v2 API. I think it would be easier to start from a new PR and add new transform and all functional part. @javirk if you are interested in working on that, feel free to provide another PR. You may cherry-pick some commits on the gallery to acknowledge the work of @parth-shastri.

Hi I can take this up, do you want me to create another pull request compatible with the new v2 API or shall I continue here?

@Richienb Richienb mentioned this pull request Apr 17, 2024
@NicolasHug
Copy link
Member

Thanks a lot for your PR @parth-shastri . We just merged #8381 which adds gaussian noise, but thanks again for the original implementation!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add Gaussian noise transformation
7 participants