- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
PyTorch – torchvision.transforms – RandomResizedCrop()
RandomResizedCrop() transform crops a random area of the original input image. This crop size is randomly selected and finally the cropped image is resized to the given size. RandomResizedCrop() transform is one of the transforms provided by the torchvision.transforms module. This module contains many important transforms that can be used to perform different types of manipulations on the image data.
RandomResizedCrop() accepts both PIL and tensor images. A tensor image is a PyTorch tensor with shape [..., H, W], where ... means a number of dimensions, H is the image height, and W is the image width. If the image is neither a PIL image nor a tensor image, then we first convert it to a tensor image and then apply the transform.
where size is the desired crop size. size is a sequence like (h, w), where h and w are the height and width of the cropped image. If size is an int, the cropped image is a square image.
It returns the cropped image resized with a given size.
We could use the following steps to crop a random portion of an input image and resize it to given size −
Import the required libraries. In all the following examples, the required Python libraries are torch, Pillow, and torchvision. Make sure you have already installed them.
import torch import torchvision import torchvision.transforms as T from PIL import Image import matplotlib.pyplot as plt
Read the input image. The input image is a PIL image or a torch tensor of shape [..., H, W].
img = Image.open('baseball.png')
Define a transform to crop a random portion on the input image and then resize to given size. Here given size is (150,250) for rectangular crop and 250 for square crop. Change the crop size according your need.
# transform for rectangular crop transform = T.RandomResizedCrop((150,250)) # transform for square crop transform = T.RandomResizedCrop(250)
Apply the above-defined transform on the input image to crop a random portion on the input image and then resize it to given size.
cropped_img = transform(img)
Show the cropped image and then the resized image
This image is used as the input in all the following examples.
In this program, the random portion of the input image is cropped and then resized with the size (150, 250).
# import required libraries import torch import torchvision.transforms as T from PIL import Image import matplotlib.pyplot as plt # read the input image img = Image.open('baseball.png') # define a transform to crop a random portion of an image # and resize it to given size transform = T.RandomResizedCrop(size=(350,600)) # apply above defined transform to the input image img = transform(img) # display the cropped image img.show()
It will produce the following output −
import torch import torchvision.transforms as T from PIL import Image import matplotlib.pyplot as plt img = Image.open('baseball.png') transform = T.RandomResizedCrop(size = (200,150), scale=(0.08, 1.0), ratio=(0.75, 1.3333333333333333)) imgs = [transform(img) for _ in range(4)] fig = plt.figure(figsize=(7,3)) rows, cols = 2,2 for j in range(0, len(imgs)): fig.add_subplot(rows, cols, j+1) plt.imshow(imgs[j]) plt.xticks() plt.yticks() plt.show()
It will produce the following output −
- 2D transforms in CSS3
- 3D transforms in CSS3
- Methods of 3D transforms with CSS3
- Rotate div with Matrix transforms using CSS
- String Transforms Into Another String in Python
- Define skew transforms along with x axis using CSS
- Define skew transforms along with y axis using CSS
- PyTorch – torch.linalg.cond()
- Linear Regression using PyTorch?
- PyTorch – torch.log2() Method
- PyTorch – FiveCrop Transformation
- PyTorch – torch.linalg.solve() Method
- PyTorch – torchvision.transforms – RandomGrayscale()
- PyTorch – torchvision.transforms – RandomHorizontalFlip()
- PyTorch – torchvision.transforms – RandomVerticalFlip()