Python PyTorch – RandomHorizontalFlip() Function Last Updated : 10 Jun, 2022 Comments Improve Suggest changes Like Article Like Report 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 tensor image as input. The tensor image is a PyTorch tensor with shape [C, H, W], where C represents the number of channels and H, W represents the height and width respectively. This method returns a horizontally flipped image and an original image if the probability P is 1 or 0 respectively, if P is in the range between 0 to 1 then P is the probability to return the horizontally flipped image. Syntax: torchvision.transforms.RandomHorizontalFlip(p)(img) Parameter: p: p is the probability of the image being flipped at a random angle.img: input image to be flipped. Returns: This method returns a randomly flipped image at a random angle. The below image is used for demonstration: Example 1: In this example, we flip an image using RandomHorizontalFlip() Method when the probability is 1. Python3 # import required libraries import torch import torchvision.transforms as T from PIL import Image # read input image from computer img = Image.open('a.png') # define a transform transform = T.RandomHorizontalFlip(p=1) # apply above defined transform to # input image img = transform(img) # display result img.show() Output: Example 2: In this example, we flip an image using RandomHorizontalFlip() Method when the probability is in the range of 0 to 1. Python3 # import required libraries import torch import torchvision.transforms as T from PIL import Image # read input image from computer img = Image.open('img.png') # define a transform transform = T.RandomHorizontalFlip(p=0.5) # apply above defined transform to # input image img = transform(img) # display result img.show() Output: Comment More infoAdvertise with us Next Article RandomResizedCrop() Method in Python PyTorch 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 RandomResizedCrop() Method in Python PyTorch 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 2 min read Python Random - random() Function The random() function in Python is used to generate a random floating-point number between 0 and 1. It's part of the random module and helps add randomness to your programs, like in games or simulations.To generate a random number, we need to import the "random module" in our program using the comma 1 min read Python Random - random() Function The random() function in Python is used to generate a random floating-point number between 0 and 1. It's part of the random module and helps add randomness to your programs, like in games or simulations.To generate a random number, we need to import the "random module" in our program using the comma 1 min read randint() Function in Python randint() is an inbuilt function of the random module in Python3. The random module gives access to various useful functions one of them being able to generate random numbers, which is randint(). In this article, we will learn about randint in Python.Python randint() Method SyntaxSyntax: randint(sta 6 min read random.sample() function - Python sample() is an built-in function of random module in Python that returns a particular length list of items chosen from the sequence i.e. list, tuple, string or set. Used for random sampling without replacement. Example:Pythonfrom random import sample a = [1, 2, 3, 4, 5] print(sample(a,3))Output[2, 5 2 min read Like