# Adding Gaussion Noise in CIFAR10 dataset

**URL:** <https://discuss.pytorch.org/t/adding-gaussion-noise-in-cifar10-dataset/961>\
**Category:** Uncategorized\
**Created:** [March 9, 2017, 2:15am UTC](https://discuss.pytorch.org/t/adding-gaussion-noise-in-cifar10-dataset/961 "2017-03-09T02:15:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shiyu](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/s/eb9ed0/32.png) [@Shiyu](https://discuss.pytorch.org/u/Shiyu)\
**Post date:** [March 9, 2017, 2:15am UTC](https://discuss.pytorch.org/t/adding-gaussion-noise-in-cifar10-dataset/961/1 "2017-03-09T02:15:31Z")

</div>

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 torch.utils.data.TensorDataset, how can I add more data samples there? Is any function like append( )? Thanks.

---

<div class="post-metadata">

**Author:** ![alykhantejani](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/alykhantejani/32/3561_2.png) [@alykhantejani](https://discuss.pytorch.org/u/alykhantejani)\
**Post date:** [March 9, 2017, 10:24am UTC](https://discuss.pytorch.org/t/adding-gaussion-noise-in-cifar10-dataset/961/2 "2017-03-09T10:24:39Z")

</div>

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.

```python
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:

```python
def __getitem__ (self, index):
    if index < 50000:
        return super(MySubClass, self). __getitem__ (index)
    else:
        return torch.Tensor(size).normal_(0,1), torch.Tensor(1).fill_(class_label)

```

---

<div class="post-metadata">

**Author:** ![maxiaoyuzdz](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/maxiaoyuzdz/32/1071_2.png) [@maxiaoyuzdz](https://discuss.pytorch.org/u/maxiaoyuzdz)\
**Post date:** [July 24, 2017, 5:05am UTC](https://discuss.pytorch.org/t/adding-gaussion-noise-in-cifar10-dataset/961/3 "2017-07-24T05:05:46Z")

</div>

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 .
