Image Augmentations with Albumentations

Learn how to Implement Image Augmentation for Your Deep Learning Models

Derrick Mwiti
Heartbeat
Published in
5 min readOct 7, 2020

--

Photo by Andrew Ridley on Unsplash

One of the biggest problems in the deep learning field is obtaining enough training data. As we know, deep learning models perform better with more training data. Very little data could lead to poor performance as well as overfitting. This problem is addressed via image augmentation: This is a technique used to generate more training samples from existing data. In this article, we’ll see how this can be done using the open-source Albumentation package.

Image Augmentation

With image augmentation, various transformations are applied to the original data in order to generate new data. This can be flipping or shearing the image. Other ways of doing this include blurring or cropping the image. This technique is applied to classification, segmentation, pose estimation, and object detection tasks. It helps to prevent overfitting as well as to improve a model’s performance.

Albumentations

The Albumentations package provides a variety of techniques for performing image augmentations. I have seen it being widely used in Kaggle competitions. It is also used in industry, deep learning research, and open-source projects. The tool is loved for its performance and speed. It uses NumPy and Open CV for data processing. At the moment, Albumentations supports 60 image augmentations. The tool also allows developers to easily add new augmentations and use them in their machine learning pipeline. Here’s a snapshot of its performance compared to other techniques:

source

Using Albumentations

Using Albumentations involves the following steps:

  • importing Albumentations
  • importing a library that can read images
  • creating an augmentation pipeline
  • reading in images
  • passing images to the augmentation pipeline
  • obtaining the augmented images

Next, we can define the transformations we’d like to use. The Compose function receives a list of the transformations we’d like to apply to the image. In this case, these are a random crop, a horizontal flip, and a random brightness contrast. The transformations are applied sequentially. p is the probability of the image being transformed.

The next step is to read in an image using cv2. The image should be passed in as a NumPy array, but we’ll do this later. Since Albumentations uses RGB, we have to convert the image to that format.

Here’s the image visualized using Matplotlib.

Image Source

Before we can apply the transformation, we have to convert the image to a NumPy array.

Now, let’s apply the transformation.

We can also visualize the transformed image using Matplotlib.

Since these transformations are random, the image you’ll see will definitely be different from this one.

Color Augmentations

Let’s now try out different augmentations starting with color augmentations.

We can, for example, change the lighting using the brightness contrast.

Let’s visualize the transformed image. Compare this to the original one and you’ll notice some change in lighting.

Let’s see how we can randomly change the saturation, hue, and value of the image.

When we transform and visualize the image, we get the image below. Again, this might be different for you.

Another transformation we can do is to randomly rearrange the channels of the RGB image. Here’s the result I obtained when doing this:

Let’s look at how this rearrangement can be accomplished. First, define the transformation pipeline.

With that in place, transform and visualize the image.

The process of performing different visualizations is the same. Let me show you one more.

We can mimic snowing in our images using the RandomSnow transformation.

Let’s visualize it.

Conclusion

As we have seen in this article, performing image augmentation with Albumentations is quite simple. Albumentations can also be used with TensorFlow and PyTorch.

You can check out the full list of the available augmentations here. Try them out and you can share your augmented images in the comments!

Editor’s Note: Heartbeat is a contributor-driven online publication and community dedicated to providing premier educational resources for data science, machine learning, and deep learning practitioners. We’re committed to supporting and inspiring developers and engineers from all walks of life.

Editorially independent, Heartbeat is sponsored and published by Comet, an MLOps platform that enables data scientists & ML teams to track, compare, explain, & optimize their experiments. We pay our contributors, and we don’t sell ads.

If you’d like to contribute, head on over to our call for contributors. You can also sign up to receive our weekly newsletters (Deep Learning Weekly and the Comet Newsletter), join us on Slack, and follow Comet on Twitter and LinkedIn for resources, events, and much more that will help you build better ML models, faster.

--

--