Enhancing Deep Learning Models with Albumentations: A Guide to Image Augmentation
Last Updated :
19 Jul, 2024
Albumentattion is a powerful Python library derived from and is for image augmentation. This library offers a wide range of image transformations that can be applied to image datasets before using them for training models. Image augmentation is a crucial preprocessing step as it increases dataset diversity, ultimately reducing overfitting by creating transformed versions of the original images. This library provides numerous augmentation techniques such as geometric transformations, color manipulations, and advanced techniques like adding weather effects or noise. As this library is easy to use, and easily integrates with other deep learning libraries like PyTorch and TensorFlow hence it is widely used in deep learning projects.
In this article, we will first understand all the benefits of using the Albumentations library in our projects. Then, we will go through the installation process of this library. Following that, we will look at various transformations this library provides, such as such as adding rain, snow, sun flares, shadows, and fog.
Benefits of using Albumentations
- The Albumentations library offers various computer vision tasks, including semantic segmentation, object detection, and primarily image augmentation.
- It is widely used across domains such as deep learning, computer vision, and open-source projects.
- Albumentations provides straightforward code for working with images, masks, bounding boxes, and key points.
- It demonstrates superior performance compared to other image augmentation libraries.
- Albumentation offers over 60 different image augmentation techniques.
- We can easily integrate many augmentation techniques via pipelines.
Installation
Via PyPI
To install the library on your system, use the following command in your terminal:
pip install -U albumentations
To install this library in a notebook like Jupyter Notebook, Kaggle, or Google Colab, add '!' before the above command.
Via GitHub
There are two ways to install this library from GitHub: the first is to install it directly using the git link, and the second is to install it using pip.
pip install -U git+https://github.com/albumentations-team/albumentations
python3 setup.py install
Via conda-forge
If you are using Anaconda, you can install this library using the following command:
conda install -c conda-forge albumentations
In this section, we will perform weather transformations on a desert photo. To do these transformations, we will first import the necessary libraries and import an image. We will use the PIL library to handle image operations, requests to fetch the image from a URL, BytesIO to handle byte streams, numpy for array manipulations, albumentations for augmentations, and matplotlib for visualizing the photo.
We will use `requests.get(url)` to fetch the image from the specified URL. After making sure that the request was successful using `response.raise_for_status()`, we will open the image using `Image.open(BytesIO(response.content))`. Finally, we will visualize the image using the `visualize` function, which will display the image without axis using matplotlib.
Python
from PIL import Image
import requests
from io import BytesIO
import numpy as np
import albumentations as A
import matplotlib.pyplot as plt
def visualize(image):
plt.imshow(image)
plt.axis('off')
plt.show()
url = "https://static.toiimg.com/thumb/msid-102149252,width-1070,height-580,resizemode-75/102149252,pt-32,y_pad-40/102149252.jpg"
response = requests.get(url)
response.raise_for_status()
image = Image.open(BytesIO(response.content))
visualize(image)
image_np = np.array(image)
Output:
Random Rain
In this transformation, we will add the rain effect to the desert image. This is usually useful for training deep learning models to recognize things under rainy conditions. Here’s how to do this transformation:
We will call the RandomRain() function from the Albumentations library to add a rain effect to the desert image. We will pass parameters like brightness_coefficient to reduce the image brightness (since it is usually darker when it rains), drop_width to define the width of the raindrops and blur_value to blur the raindrops to make them appear more natural. Then, we will pass the desert image into this transformation. Finally, we will call the visualize() function to see the resultant image.
Python
transform = A.RandomRain(brightness_coefficient=0.9, drop_width=1, blur_value=5, p=1)
transformed = transform(image=image_np)
visualize(transformed['image'])
Output:
Random Snow
In this transformation, we will add a snow effect to a desert image. This is particularly useful for training models to recognize objects in images that contain snow. Now, let’s see how to add this effect to our image.
We will call the RandomSnow() function from the Albumentations library to add the snow transformation. We will pass parameters like brightness_coeff, which will be set to 2.5 to increase the brightness and simulate the reflective nature of snow. Then, we will pass snow_point_lower and snow_point_upper parameters, which will define the range of snow coverage on the image. Finally, we will pass the desert image to the transform variable and call the visualize function to visualize the image.
Python
transform = A.RandomSnow(brightness_coeff=2.5, snow_point_lower=0.3, snow_point_upper=0.5, p=1)
transformed = transform(image=image_np)
visualize(transformed['image'])
Output:
Random Sun Flare
The Random Sun Flare transformation is an advanced augmentation technique that simulates sun flares on an image. This is particularly useful for training models to recognize objects and scenes under bright, sunny conditions. Now let’s see how to add this transformation to the desert image.
To do this we will call the `RandomSunFlare()` function from the Albumentations library. We will pass parameters like `flare_roi` to define the region of interest where the sun flare will appear in the image, and `angle_lower` to specify the lower bound of the angle for the sun flare. Finally, we will then apply this transformation to our image using the `transform` function and visualize the result with the `visualize()` function.
Python
transform = A.RandomSunFlare(flare_roi=(0, 0, 1, 0.5), angle_lower=0.5, p=1)
transformed = transform(image=image_np)
visualize(transformed['image'])
Output:
Random Shadow
The Random Shadow transformation is another useful augmentation technique that simulates shadows on an image. This is particularly beneficial for training models to recognize objects and scenes under varying lighting conditions. Now let’s see how to build this transformation.
To apply the Random Shadow transformation, we will create a transformation object using the `A.RandomShadow` function from the Albumentations library. We will pass parameters such as `num_shadows_lower=1` and `num_shadows_upper=1` to define the number of shadows to add to the image. The `shadow_dimension` parameter will be set to 5 to specify the size of the shadow, and the `shadow_roi` parameter will be set to (0, 0.5, 1, 1) to define the region of interest where the shadow will appear. Finally, we will apply this transformation to our image using the `transform` function and visualize the result with the `visualize` function.
Python
transform = A.RandomShadow(num_shadows_lower=1, num_shadows_upper=1, shadow_dimension=5, shadow_roi=(0, 0.5, 1, 1), p=1)
transformed = transform(image=image_np)
visualize(transformed['image'])
Output:
Random Fog
Let’s now add the fog effect to the desert image. This type of augmentation is important to train deep learning models which are being trained to identify objects from foggy images. Now let’s see how to build this transformation.
To do this, we will call the RandomFog() function from the Albumentations library. In this function, we will pass a few parameters like fog_coef_lower and fog_coef_upper, which will define the range of fog density in the image. Another important parameter we will pass is alpha_coef, which will define the transparency of the fog. Finally, we will apply this transformation by passing the desert image to the transform variable. Then we will call the visualize() function to visualize the image.
Python
transform = A.RandomFog(fog_coef_lower=0.7, fog_coef_upper=0.8, alpha_coef=0.1, p=1)
transformed = transform(image=image_np)
visualize(transformed['image'])
Output:
Conclusion
In conclusion, Albumentations is one of the best Python libraries for image augmentation, offering weather transformations and many more options like morphological and rotational transformations. One of the best features of Albumentations is the ability to control the transformations by passing various parameters. Additionally, Albumentations has dedicated documentation and an active community, making it easier to get help if you encounter any issues. So, next time you are building a deep learning project and need image augmentation, try the Albumentations library.
Similar Reads
Perceptual Autoencoder: Enhancing Image Reconstruction with Deep Learning
In recent years, autoencoders have emerged as powerful tools in unsupervised learning, especially in image compression and reconstruction. The Perceptual Autoencoder is a specialized type of autoencoder that takes image reconstruction to the next level by optimizing for pixel-wise accuracy and perce
15 min read
What is Data Augmentation? How Does Data Augmentation Work for Images?
Data augmentation is a technique used to increase diversity of a dataset without actually collecting new data. It works by applying various transformations to the existing data to create new, modified versions of data that helps the model generalize better. In this article, we will learn more about
4 min read
Audio Augmentation Using nlpaug: A Comprehensive Guide
Audio augmentation is crucial in preparing training data for machine learning models and signal processing. In this step, we create modified versions of the original audio data to ensure the dataset is diverse. This is especially useful in tasks like speech recognition and music classification. By a
6 min read
Black and white image colorization with OpenCV and Deep Learning
In this article, we'll create a program to convert a black & white image i.e grayscale image to a colour image. We're going to use the Caffe colourization model for this program. And you should be familiar with basic OpenCV functions and uses like reading an image or how to load a pre-trained mo
3 min read
Easy-NLP-Augmentation Library: Simplifying Text Augmentation in Python
Text augmentation is a critical step in data preparation for building robust language models. By creating variations of existing text data, we can enhance the diversity and size of the training dataset, ultimately improving the performance of language models. This process involves applying transform
8 min read
Avengers Endgame and Deep learning | Image Caption Generation using the Avengers EndGames Characters
Behold, Marvel Fans. Avengers are out there to save the Multiverse, so are we, ready to do whatever it takes to support them. In this article, we will use Deep Learning and computer vision for the caption generation of Avengers Endgame characters. We will start will the basics, explaining concepts a
14 min read
Face completion with a Multi-output Estimators in Scikit Learn
Face completion is a fascinating application of machine learning where the goal is to predict missing parts of an image, typically the face, using the existing data. Scikit-learn provides multi-output estimators which are useful for this kind of task. This post is a step-by-step tutorial on how to p
6 min read
ALIGN: A Large-scale ImaGe and Noisy-text Model
ALIGN (A Large-scale Image and Noisy-text) is a multimodal model designed to process images and their associated textual descriptions. It aligns and combines images with their textual representations (Alt Text) in a shared embedding space, facilitating cross-modal understanding by associating images
5 min read
Holistically-Nested Edge Detection with OpenCV and Deep Learning
Holistically-nested edge detection (HED) is a deep learning model that uses fully convolutional neural networks and deeply-supervised nets to do image-to-image prediction. HED develops rich hierarchical representations automatically (directed by deep supervision on side replies) that are critical fo
3 min read
Evaluating Object Detection Models: Methods and Metrics
Object detection combines the tasks of image classification and object localization tasks to determine objects' presence and draw bounding boxes around them. In this article, we are going to explore the metrics used to evaluate the object detection models. Importance of Evaluating Object Detection M
7 min read