How to perform random affine transformation of an image in PyTorch
Last Updated :
13 May, 2022
In this article, we will cover how to perform the random affine transformation of an image in PyTorch.
RandomAffine() method
RandomAffine() method accepts PIL Image and Tensor Image. The tensor image is a PyTorch tensor with [C, H, W] shape, where C represents the number of channels and H, W represents the height and width respectively. This method returns the affine transformed image of the input image. The below syntax is used to perform the affine transformation of an image in PyTorch.
Syntax: torchvision.transforms.RandomAffine(degree)
Parameters:
- degree: This is our desired range of degree. It’s a sequence like (min, max).
Return: This method returns the affine transformed image of the input image.
The below image is used for demonstration:
Example 1:
The following example is to understand how to perform the random affine transformation of an image in PyTorch whereas, the desired range of degree is (50,60).
Python3
import torch
from PIL import Image
import torchvision.transforms as transforms
img = Image. open ( 'a.jpg' )
transform = transforms.RandomAffine(( 50 , 60 ))
img = transform(img)
img.show()
|
Output:
Example 2:
The following example is to know how to perform the random affine transformation of an image in PyTorch whereas, the desired range of degrees is (30).
Python3
import torch
from PIL import Image
import torchvision.transforms as transforms
img = Image. open ( 'a.jpg' )
transform = transforms.RandomAffine(( 30 ))
img = transform(img)
img.show()
|
Output:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...