# How to use Generic transforms for the following preprocessing step

**URL:** <https://discuss.pytorch.org/t/how-to-use-generic-transforms-for-the-following-preprocessing-step/50658>\
**Category:** vision\
**Created:** [July 15, 2019, 3:07pm UTC](https://discuss.pytorch.org/t/how-to-use-generic-transforms-for-the-following-preprocessing-step/50658 "2019-07-15T15:07:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rishav\_Sapahia](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/r/51bf81/32.png) [@Rishav\_Sapahia](https://discuss.pytorch.org/u/Rishav_Sapahia)\
**Post date:** [July 15, 2019, 3:07pm UTC](https://discuss.pytorch.org/t/how-to-use-generic-transforms-for-the-following-preprocessing-step/50658/1 "2019-07-15T15:07:39Z")

</div>

I want to apply the following transformation to the image dataset.

1. N(w, h) = I(w, h) − G(w, h), (1) where N is the normalized image, I is the original image, and G is the Gaussian blurred image with kernel size 65\*65 and 0 mean and standard deviation 10.

The code for gaussian blur is-

```auto
def gaussian_blur(img):
    image = cv2.GaussianBlur(image,(65,65),10)
    new_image = img - image
return image

```

I am really not sure how to convert it into lambda function as to use in generic transform.Any other advice on how to apply the above preprocessing step is also welcomed.

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [July 15, 2019, 7:47pm UTC](https://discuss.pytorch.org/t/how-to-use-generic-transforms-for-the-following-preprocessing-step/50658/2 "2019-07-15T19:47:45Z")

</div>

This code should work:

```python
def gaussian_blur(img):
    image = np.array(img)
    image_blur = cv2.GaussianBlur(image,(65,65),10)
    new_image = image_blur
    return new_image

x = torch.randn(3, 224, 224)
img = TF.to_pil_image(x)
transform = transforms.Lambda(gaussian_blur)
img = transform(img)

```

---

<div class="post-metadata">

**Author:** ![Rishav\_Sapahia](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/r/51bf81/32.png) [@Rishav\_Sapahia](https://discuss.pytorch.org/u/Rishav_Sapahia)\
**Post date:** [July 19, 2019, 3:48pm UTC](https://discuss.pytorch.org/t/how-to-use-generic-transforms-for-the-following-preprocessing-step/50658/3 "2019-07-19T15:48:18Z")

</div>

Thanks, it worked. I am observing a peculiar behavior, don’t know whether a gap in my knowledge or not.

```
data_transforms = transforms.Compose([transforms.RandomCrop(512,512),
                             transforms.ToTensor(),
                             transforms.Lambda(gaussian_blur),
                             transforms.Normalize(mean=train_mean, std=train_std),
                             transforms.RandomRotation([+90,+180]),
                             transforms.RandomRotation([+180,+270]),
                             transforms.RandomHorizontalFlip(),
                           ])

```

When I am showing the shape of the images, it is coming out to be not 512,512. Does Random Crop doesn’t take the required size and then crop it or I am doing something wrong.

```
for images, labels in final_train_loader:  
    print('Image batch dimensions:', images.shape)

Image batch dimensions: torch.Size([3, 3, 584, 565])
Image label dimensions: torch.Size([3])
```

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [July 19, 2019, 3:53pm UTC](https://discuss.pytorch.org/t/how-to-use-generic-transforms-for-the-following-preprocessing-step/50658/4 "2019-07-19T15:53:45Z")

</div>

Make sure this code is really called in your `Dataset`, as it should throw an error.  
Image transformations like `RandomRotation` and `RandomHorizontalFlip` are only defined for `PIL.Image`s.

Thus you would have to use `ToTensor()` and `Normalize()` as the last transformations.  
I’m not sure if your `Lambda(gaussian_blur)` transform works on tensors or `PIL.Image`s.

---

<div class="post-metadata">

**Author:** ![Rishav\_Sapahia](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/r/51bf81/32.png) [@Rishav\_Sapahia](https://discuss.pytorch.org/u/Rishav_Sapahia)\
**Post date:** [July 22, 2019, 12:37pm UTC](https://discuss.pytorch.org/t/how-to-use-generic-transforms-for-the-following-preprocessing-step/50658/7 "2019-07-22T12:37:24Z")

</div>

Yes, you were right. I was calling some other dataset and there was also a typo in my implementation.  
`Lambda(gaussian_blur)` works on PIL image but we have to add a conversion of numpy array to PIL image using `im = Image.fromarray(new_image)` in the gaussian\_blur function.

---

<div class="post-metadata">

**Author:** ![Ripley](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/r/74df32/32.png) [@Ripley](https://discuss.pytorch.org/u/Ripley)\
**Post date:** [October 16, 2020, 3:01am UTC](https://discuss.pytorch.org/t/how-to-use-generic-transforms-for-the-following-preprocessing-step/50658/8 "2020-10-16T03:01:48Z")

</div>

Thank you! for this code!
