vision icon indicating copy to clipboard operation
vision copied to clipboard

Add Gaussian noise transformation

Open mjack3 opened this issue 3 years ago • 8 comments

🚀 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

mjack3 avatar Jun 22 '22 21:06 mjack3

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

oke-aditya avatar Jun 23 '22 08:06 oke-aditya

@oke-aditya maybe the correct link is https://albumentations.ai/docs/api_reference/augmentations/transforms/#albumentations.augmentations.transforms.GaussNoise

Apply gaussian noise to the input image

vfdev-5 avatar Jun 23 '22 09:06 vfdev-5

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

oke-aditya avatar Jun 23 '22 09:06 oke-aditya

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

mjack3 avatar Jun 23 '22 09:06 mjack3

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?

datumbox avatar Jun 23 '22 09:06 datumbox

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

oke-aditya avatar Jun 23 '22 09:06 oke-aditya

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.

vfdev-5 avatar Jun 23 '22 09:06 vfdev-5

That will the job for now

mjack3 avatar Jun 23 '22 09:06 mjack3