How to apply a Rotate 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

Rotate

function in the Albumentations library to apply a

Rotate

augmentation to images in your dataset.

To generate augmented images, we will:

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

Rotate

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:

Rotate

. 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

pipeline = A.Compose([
    A.Rotate(limit=(-90, 90))
])

image = cv2.imread("example.png")

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

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

In this code, we:

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

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.