- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- 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 – RandomVerticalFlip()

We apply **RandomVerticalFlip()** transform to flip an image vertically at a random angle with a given probability. It's one of the transforms provided by the **torchvision.transforms** module. This module contains many important transformations that can be used to perform different types of manipulations on the image data.

**RandomVerticalFlip()** accepts both PIL and tensor images. A tensor image is a torch Tensor with shape [C, H, W], where C is the number of channels, H is the image height, and W is the image width.

## Syntax

torchvision.transforms.RandomVerticalFlip(p)(img)

If

**p**= 1, it returns the vertically flipped image.If

**p**= 0, It returns the original image.If

**p**is in the range (0,1), then the probability to return the vertically flipped image is p.

It returns a vertically flipped image at a random angle with a given probability p.

## Steps

One could follow the steps given below to vertically flip an image at a random angle with a given probability −

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

Read the input image. The input image is a PIL image or a tensor image.

img = Image.open('mountain.jpg')

Define a transform to vertically flip the image randomly with a given probability p. Here p = 0.25 means, the chance of any input image to be vertically flipped is 25%.

transform = T.RandomVerticalFlip(p = 0.25)

Apply the above defined transform on the input image to vertically flip the image.

vflipped_img = transform(img)

Show the output image.

vflipped_img.show()

## Input Image

This image is used as the input file in all the following examples.

## Example 1

In this program, we set p=1, so the output image will be vertically flipped image.

# import the required libraries import torch import torchvision.transforms as T from PIL import Image # read the input image img = Image.open('mountain.jpg') # define a transform with probability = 1 # to vertically flip an image transform = T.RandomVerticalFlip(p=1) # apply the transform on input image img = transform(img) # display the flipped image img.show()

## Output

It will produce the following output −

Notice that the output image is a vertically flipped image, as we set the probability p=1.

## Example 2

In this example, we set the probability p=0.25, so the chance of any image to be vertically flipped is 25%.

import torch import torchvision.transforms as T from PIL import Image import matplotlib.pyplot as plt # read the input image img = Image.open('mountain.jpg') # define a transform with probability = 0.25 transform = T.RandomVerticalFlip(p=0.25) # save four output images applying the above transform imgs = [transform(img) for _ in range(4)] # display these four output images fig = plt.figure(figsize=(7,4)) 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()

## Output

It will produce the following output −

Notice that out of the four output images, at least one image is vertically flipped. You may get different number of vertically flipped images.

- Related Questions & Answers
- 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 – RandomResizedCrop()