Data Augmentation is a regularization technique that increases the size of the training dataset by adding “artificial” data to the dataset. Data Augmentation serves the purpose of making the model more robust by providing more data and hence reducing overfitting. This artificial data is generated using the original data such that it is indistinguishable from the original data. In computer vision, for example, data augmentation can be performed by flipping the images about horizontal or vertical axes, or by scaling or rotating the image, or by any combination of these. This method is known as image transformation. Albumentations is a library that does the job of image transformation for augmenting data. In this blog we will get started with how to use albumentations and why should you use it.
An example of data augmentation using image transformation is shown in the figure below(Figure 1). The leftmost image is the original image. The middle image is generated from the original image by performing a horizontal flip(flipping over the vertical axis). The rightmost image is generated from the original image by randomly changing the brightness and contrast of the original image.
Why use Albumentations?
Almost every deep learning library implements algorithms for performing image transformations. The obvious question, therefore, that comes to mind is- Why learn and use albumentations for performing image transformations? The answer to that question is simple- It’s faster, simpler and contains more image transformation operations available.
The table below compares how albumentations compares to other libraries for data augmentation when measured by the time required per transformation. As it can be clearly seen that it is quite fast as compared to other data augmentation libraries.
The measure used is time in seconds per image transformation(lower is better). Source.
Getting Started with Albumentations
Enough talks about how good albumentations is, it’s time to get started with image transformation using albumentations.
A Simple Image Transformer
We will start by creating a simple image transformer. This transformer will apply a particular transformation to all the images. The code below creates an image transformer that will horizontally flip all the images.
We just import the albumentations library and create an image transformer by creating an object of the HorizontalFlip
class. We pass the probability of the transformation using the argument p
. In this particular example, the probability is set to 1, hence, the transformer will horizontally flip all the images that pass through it. The default value of probability parameter p
is 1.
Applying Multiple Transformations
The above example was quite simple as only one transformation was being applied. In practical use cases though, multiple image transformations are to be applied to the image. This is where albumentations’ Compose
comes into play. It amalgamates multiple transformers into a single transformer. We will now be building a transformer that applies multiple transformations to images.
Here we have created a transformer that does multiple transformations. We have done this using the Compose
API. A list of transformers is passed to Compose
and it will return a transformer that will apply all the required transformations. In this example, we will be applying only the horizontal and vertical transformations using HorizontalFlip
and VerticalFlip
transformers respectively. As can be seen in this example, the probability of each transformation being applied is 0.5
. In this example, only transformers are passed to Compose
, however, you can pass any number of transformers to Compose
.
Example of Image Transformation
Having talked about how to use albumentations, it’s time to perform some actual image transformation. We will use the transformer we created in the earlier example to perform transformations on an image.
The above code will display an image that has a 50% of being both horizontally and vertically flipped.
Conclusion
In this blog, we learnt about what is albumentations, what it does and why using it can be better than using other image transformation libraries. We learnt how to use it by creating transformers and how to use Compose
API. We also learnt how to transform images using transformers.
References
- The official Albumentations website.
- Albumentations paper.
- Albumentations Github repository.