Adding Gaussion Noise in CIFAR10 dataset

If I want to add some Gaussion noise in the CIFAR10 dataset which is loaded by torchvision, how should I do it? Or, if I have defined a dataset by, how can I add more data samples there? Is any function like append( )? Thanks.

There’s a few ways you can do this.

  1. If you don’t care about seeing all 50k cifar10 samples in one complete pass of the data loader you could pass in a transform that randomly returns noise instead of the image. i.e.
import random

class RandomNoise(object):
    def __init__(self, probability):
         self.probabilit = probability
    def __call__(self, img):
        if random.random() <= self.probability:
            return img.clone().normal_(0,1)
        return img
cifar10_dataset = torchvision.datasets.CIFAR10(root="...", download=True, transform=input_transform)
  1. You could sub class CIFAR10 and override the __len__ function to return maybe 100000, then the __getitem__ function would look something like:
def __getitem__(self, index):
    if index < 50000:
        return super(MySubClass, self).__getitem__(index)
        return torch.Tensor(size).normal_(0,1), torch.Tensor(1).fill_(class_label)

Hi, I use torchvision.transform to do it, it has a lambda function which you can customized a funciton to add noise to the data. But the CIFAR10 image is small just 32 * 32 * 10, after add sp or gaussion noise on them, the final result seems like not well .