PyTorch is a widely used machine-learning library that offers several modules and tools for working with numerous images. Gaussian blur is the image processing procedure that reduces the noise and sharpness of edges in the image. In PyTorch, the “torchvision.transforms” module has the “GaussianBlur()” function/method that is used to blur or smooth any desired image.
This write-up will demonstrate the method to use “GaussianBlur()” in PyTorch.
How to Use “GaussianBlur()” in PyTorch?
To use the “GaussianBlur()” method in PyTorch, follow the below-listed steps:
- Upload/Add the desired image to Google Colab
- Install necessary libraries
- Read the uploaded input image
- Create transform to blur image
- Apply transform to the uploaded input image
- Display blurred image
Step 1: Upload/Add Desired Image to Google Colab
First, upload the desired image to Google Colab by selecting the following highlighted icons:
This has added/uploaded the image on Google Colab:
Below is the uploaded image and we will apply the “GaussianBlur()” transformation to it:
Step 2: Install Necessary Libraries
Then, import the following libraries necessary for working with images:
import torch
import torchvision.transforms as T
from PIL import Image
In the above snippet:
- “import torch” imports the PyTorch package.
- “import torchvision.transforms as T” imports the transforms module as “T” from “torchvision” for preprocessing image data
- “from PIL import Image” imports “Image” from “PIL” for opening and saving several image file formats:
Step 3: Read Input Image
Next, read the uploaded input image i.e. “flowers_img.jpg” from the PC using the “Image.open()” function:
input_img = Image.open('flowers_img.jpg')
Step 4: Create Transform to Blur Image
Now, use the “GaussianBlur” transformation to define a transform to blur the input image. Users must define the “kernal_size” and “sigma” for the new blurred image. Here, we have specified the following values:
transform = T.GaussianBlur(kernel_size=(9, 17), sigma=(3, 16))
Here:
- “kernel_size” parameter defines the size of the Gaussian blur kernel.
- “sigma” parameter specifies the standard deviation/sigma of the Gaussian distribution:
Step 5: Apply Transform to Input Image
After that, apply the above-defined transform on the specified input image to blur it:
blur_img = transform(input_img)
Step 6: Display Blurred Image
Finally, print the blurred image:
blur_img
The below image shows the blurred image which indicates that the “GaussianBlur” has been applied successfully:
Comparison
The below table shows the comparison between the original image and the blurred images:
We have explained the method of using the “GaussianBlur()” transformation in PyTorch.
Note: Click on the provided link to access our Google Colab Notebook.
Conclusion
To use the “GaussianBlur()” transformation in PyTorch, first, add the desired image to Google Colab. Then, install the required libraries and read the input image. After that, utilize the “GaussianBlur()” transformation to define and apply the transform on the desired input image. Finally, display the blurred image. This article will illustrate the method of using “GaussianBlur()” in PyTorch.