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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Gaussian noise transformation #6192

Open
mjack3 opened this issue Jun 22, 2022 · 10 comments 路 May be fixed by #6233 or #8381
Open

Add Gaussian noise transformation #6192

mjack3 opened this issue Jun 22, 2022 · 10 comments 路 May be fixed by #6233 or #8381

Comments

@mjack3
Copy link

mjack3 commented Jun 22, 2022

馃殌 The feature

Add gaussian noise transformation in the functionalities of torchvision.transforms.

Motivation, pitch

Using Normalizing Flows, is good to add some light noise in the inputs. Right now I am using albumentation for this but, would be great to use it in the torchvision library

Alternatives

Albumentation has a gaussian noise implementation

Additional context

No response

cc @vfdev-5 @datumbox

@oke-aditya
Copy link
Contributor

Hi @mjack3 thanks for the request.

We do have GaussianBlur https://github.com/pytorch/vision/blob/main/torchvision/transforms/transforms.py#L1765

May I know what's the difference between gaussian blur and gaussian Noise? In case they are same feel free to use T.GaussianBlur

@vfdev-5
Copy link
Collaborator

vfdev-5 commented Jun 23, 2022

@oke-aditya
Copy link
Contributor

What do you think @vfdev-5 ? Is this useful to add to torchvision.transforms ?

@mjack3
Copy link
Author

mjack3 commented Jun 23, 2022

Gaussian noise and Gaussian blur are different as I am showing below. As I said, Gaussian noise is used in several unsupervised learning methods

image

@datumbox
Copy link
Contributor

I think Salt and Pepper and Gaussian Noise are valid transforms to offer. I would probably be in favour to create them on the new API which is in progress. Wdyt?

@oke-aditya
Copy link
Contributor

Seems great, salt pepper noise and gaussian Noise and probably textBook transforms. Seems nice addition to new API.

@vfdev-5
Copy link
Collaborator

vfdev-5 commented Jun 23, 2022

A minimal workaround today could be the following (keeping in mind all limitations it could have):

from PIL import Image

import torch
import torchvision


img = Image.open("test-image.jpg")

from torchvision.transforms import Compose, PILToTensor, ToPILImage


# Tensor GN pipeline

def gauss_noise_tensor(img):
    assert isinstance(img, torch.Tensor)
    dtype = img.dtype
    if not img.is_floating_point():
        img = img.to(torch.float32)
    
    sigma = 25.0
    
    out = img + sigma * torch.randn_like(img)
    
    if out.dtype != dtype:
        out = out.to(dtype)
        
    return out


t1 = Compose([
    PILToTensor(),
    gauss_noise_tensor,
    # For visualization purposes
    ToPILImage()
])


o1 = t1(img)

import matplotlib.pyplot as plt
%matplotlib inline


plt.figure(figsize=(14, 7))
plt.subplot(121)
plt.title("Pillow")
plt.imshow(img)
plt.subplot(122)
plt.title("Tensor GN")
plt.imshow(o1)

image

I agree that we could implement it and its variant in the transforms.

@mjack3
Copy link
Author

mjack3 commented Jun 23, 2022

That will the job for now

@parth-shastri parth-shastri linked a pull request Jul 3, 2022 that will close this issue
@Richienb Richienb linked a pull request Apr 17, 2024 that will close this issue
@richardrl
Copy link

This seems easy to do with: https://pytorch.org/docs/stable/generated/torch.normal.html ?

@mjack3
Copy link
Author

mjack3 commented May 23, 2024

It is as easy as developing a horizontal flip. However, having it already implemented would be helpful.

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

Successfully merging a pull request may close this issue.

5 participants