Saturday, May 16, 2026
HomeLanguagesRandomVerticalFlip() Method in Python PyTorch

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 tensor image as an input. 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 a randomly flipped image.

Syntax: torchvision.transforms.RandomVerticalFlip(p)

Parameter:

  • p: p is the probability of the image being flipped.
  • img: input image.  

Returns: This method returns a randomly flipped image. This method returns vertically flipped image if P=1, returns original image if P=0 and if p in between the range of 0 to 1 then P is the probability to return the vertically flipped image.

The below image is used for demonstration:

 

Example 1:

The following program is to understand the RandomVerticalFlip() 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.RandomVerticalFlip(p=1)
  
# apply above defined transform to
# input image
img = transform(img)
  
# display result
img.show()


Output:

RandomVerticalFlip() Method in Python PyTorch

 

Example 2:

The following program is to understand the RandomVerticalFlip() 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('a.png')
  
# define a transform
transform = T.RandomVerticalFlip(p=0.5)
  
# apply above defined transform to 
# input image
img = transform(img)
  
# display result
img.show()


Output:

RandomVerticalFlip() Method in Python PyTorch

 

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32514 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6892 POSTS0 COMMENTS
Nicole Veronica
12012 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12107 POSTS0 COMMENTS
Shaida Kate Naidoo
7016 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6975 POSTS0 COMMENTS
Umr Jansen
6963 POSTS0 COMMENTS