pytorch

How to Apply Random Affine Transformation on an Image in PyTorch?

In PyTorch, the “torchvision.transforms” module provides a “RandomAffine()” method that applies affine transformation on the specific image. It takes several parameters that control the range and type of transformation. The most important parameter is degrees which specifies the range of rotation angles. Other parameters include translate, scale, shear, interpolation, fill, and center. These parameters control the translation, scaling, shearing, interpolation mode, fill value, and center of rotation of the affine transformation, respectively. This method returns a new affine transformed image.

This article will demonstrate the method to apply/perform the random affine transformation on the desired image in PyTorch.

How to Apply Random Affine Transformation on an Image in PyTorch?

To apply/perform the random affine transformation on the desired image in PyTorch, look at the below-provided steps:

Step 1: Upload an Image to Google Colab

First, open Google Colab and click on the below-highlighted icons. Then, choose the specific image from the computer and upload it:


Upon doing so, the image will be uploaded to Google Colab:


Here, we have uploaded the following image and we will apply the random affine transformation to it:


Step 2: Import Required Library

Next, import the required libraries. For instance, we have imported the following libraries:

import torch
import torchvision.transforms as transforms
from PIL import Image

 
Here:

    • import torch” imports the PyTorch library.
    • import torchvision.transforms as transforms” imports the transforms module from torchvision that is used to preprocess image data before feeding it into a neural network.
    • from PIL import Image” is used to open and save different image file formats:

 

Step 3: Read the Input Image

After that, read the input image from the computer. Here, we are reading the “flowers_img.jpg” and storing it in the “input_img” variable:

input_img = Image.open('flowers_img.jpg')

 

Step 4: Define a Transform

Then, define a transform to apply the random affine transformation on the provided input image. Users need to define the desired range of degrees in the “RandomAffine()” method. Here, we have defined (30, 60) degrees which means that the image will be randomly rotated by an angle between 30 and 60 degrees:

transform = transforms.RandomAffine((30, 60))

 

Step 5: Apply the Transform on Input Image

Now, apply the above-created transform on the desired input image:

trans_img = transform(input_img )

 

Step 6: Display Affine Transformed Image

Finally, view the affine transformed image by displaying it:

trans_img

 

The above output shows that the affine transformation has been successfully applied to the input image.

Moreover, users can also specify “translate”, “scale”, “sher”, and “interpolation”, etc. in the “RandomAffine()” method as a parameter. For instance, we have specified “translate” and “scale” parameters as seen below:

transform = transforms.RandomAffine(degrees=(30, 60), translate=(0.1, 0.3), scale=(0.5, 0.8))

 

 

Comparison

The comparison between the original image and affine transformed images can be seen below:


Note: You can access our Google Colab Notebook at this link.

We have efficiently explained the method of applying random affine transformation on the specific image in PyTorch.

Conclusion

To apply the random affine transformation on a particular image in PyTorch, first, upload the desired image to Google Colab. Then, import the necessary libraries and read the input image. Next, define a transform using the “RandomAffine()” method and apply it to the input image. Finally, view the affine transformed image by displaying it. This article has demonstrated the method to apply the random affine transformation on the specific image in PyTorch.

About the author

Laiba Younas

I have done bachelors in Computer Science. Being passionate about learning new technologies, I am interested in exploring different programming languages and sharing my experience with the world.