RandomResizedCrop() Method in Python PyTorch Last Updated : 03 Jun, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report In this article, we are going to discuss RandomResizedCrop() method in Pytorch using Python. RandomResizedCrop() method RandomResizedCrop() method of torchvision.transforms module is used to crop a random area of the image and resized this image to the given size. This method accepts both PIL Image and Tensor Image. The tensor image is a PyTorch tensor with [C, H, W] shape, where C represents a number of channels and H, W represents height and width respectively. This method returns a randomly cropped image. Syntax: torchvision.transforms.RandomResizedCrop(size, scale, ratio) Parameters: size: Desired crop size of the image.scale: This parameter is used to define the upper and lower bounds for the random area.ratio: This parameter is used to define upper and lower bounds for the random aspect ratio. Return: This method will returns the randomly cropped image of given input size. The below image is used for demonstration: Example 1: In this example, we are transforming the image with a height of 300 and a width of 600. Python3 # import required libraries import torch import torchvision.transforms as transforms from PIL import Image # Read image image = Image.open('pic.png') # create an transform for crop the image # 300px height and 600px wide transform = transforms.RandomResizedCrop(size=(300, 600)) # use above created transform to crop # the image image_crop = transform(image) # display result image_crop.show() Output: Example 2: In this example, we crop an image at a random location with the expected scale of 0.2 to 0.8. Python3 # import required libraries import torch import torchvision.transforms as transforms from PIL import Image # Read image image = Image.open('a.png') # create an transform for crop the image transform = transforms.RandomResizedCrop(size=(300, 600), scale=(0.2, 0.8)) # use above created transform to crop # the image image_crop = transform(image) # display result image_crop.show() Output: Example 3: In this example, we crop an image at a random location with the expected ratio of 0.5 to 1.08. Python3 # import required libraries import torch import torchvision.transforms as transforms from PIL import Image # Read image image = Image.open('a.png') # create an transform for crop the image transform = transforms.RandomResizedCrop( size=(300, 600), scale=(0.2, 0.8), ratio=(0.5, 1.08)) # use above created transform to crop # the image image_crop = transform(image) # display result image_crop.show() Output: Comment More infoAdvertise with us Next Article Python PyTorch â RandomHorizontalFlip() Function M mukulsomukesh Follow Improve Article Tags : Python Python-PyTorch Practice Tags : python Similar Reads RandomVerticalFlip() Method in Python PyTorch In this article, we are going to discuss RandomVerticalFlip() Method in PyTorch using Python. RandomVerticalFlip() Method RandomVerticalFlip() method of torchvision.transforms module is used to vertically flip the image at a random angle with a given probability.  This accepts a PIL image and a ten 2 min read Python - Pytorch randn() method PyTorch torch.randn() returns a tensor defined by the variable argument size (sequence of integers defining the shape of the output tensor), containing random numbers from standard normal distribution. Syntax: torch.randn(*size, out=None, dtype=None, layout=torch.strided, device=None, requires_grad= 3 min read Python PyTorch â RandomHorizontalFlip() Function In this article, we will discuss the RandomHorizontalFlip() Method in PyTorch Python. RandomHorizontalFlip() method RandomHorizontalFlip() method of torchvision.transforms module is used to horizontally flip the given image at a random angle with a given probability. This method accepts a PIL and te 2 min read Python - random.seed( ) method random.seed() method in Python is used to initialize the random number generator, ensuring the same random numbers on every run. By default, Python generates different numbers each time, but using .seed() allows result reproducibility.It's most commonly used in:Machine Learning- to ensure model cons 4 min read Python - random.seed( ) method random.seed() method in Python is used to initialize the random number generator, ensuring the same random numbers on every run. By default, Python generates different numbers each time, but using .seed() allows result reproducibility.It's most commonly used in:Machine Learning- to ensure model cons 4 min read random.triangular() method in Python triangular() is an inbuilt method of the random module. It is used to return a random floating point number within a range with a bias towards one extreme. Syntax : random.triangular(low, high, mode) Parameters : low : the lower limit of the random number high : the upper limit of the random number 2 min read Like