How to apply a Vertical Flip augmentation with Albumentations

Overview

Albumentations is an open source computer vision package with which you can generate augmentated images. These images can be added to a training dataset. When the appropriate augmentations are chosen, augmented images can improve the performance of your model.

In this guide, we are going to show you how to use the

VerticalFlip

function in the Albumentations library to apply a

Vertical Flip

augmentation to images in your dataset.

To generate augmented images, we will:

1. Install Albumentations
2. Construct an image augmentation pipeline that uses the

Vertical Flip

augmentation
3. Generate augmented images using the pipeline

Without further ado, let's get started!

Install Albumentations

First, install the Albumentations pip package:

pip install albumentations


Once you have installed albumentations, you are ready to start generating augmented images.

Generate Augmented Images

Next, we are going to generate augmented images. We will define an augmentations pipeline with a single augmentation:

VerticalFlip

. We will then use this pipeline to generate augmented images. While we will only use one augmentation in this guide, you can stack augmentations if you want.



Create a new Python file and add the following code:


import albumentations as A
import cv2

image = cv2.imread("yard.jpg")

pipeline = A.Compose([
    A.VerticalFlip(p=1),
])

augmented_image = pipeline(image=image)["image"]

cv2.imwrite("augmented_image.jpg", augmented_image) 

In this code, we:

  1. Import the required dependencies.
  2. Define a Compose pipeline with one augmentation: VerticalFlip.
  3. Load an image using the OpenCV Python package
  4. Pass the image through our VerticalFlip transformation function.
  5. Save the augmented image.

You can customize the chance the augmentation is applied to an image by changing the p= argument from 1 to a value between 0 and 1.

You can update the code above to use a loop to augment multiple images.

Let's run our code on this image:

Here is an example of an image augmented with our code:

Our augmentation pipeline is now set up!

Next Steps

Now that you have augmented your images, your next step is to train a computer vision model.

Roboflow has several resources you can use to train vision models. Check out our training guides for more information:

- Train a YOLOv8 object detection model
- Train a YOLOv10 object detection model
- Train a PaliGemma object detection model
- Train a Florence-2 object detection model

If you are interested in learning more about training models for enterprise use cases, contact the Roboflow sales team.