Python | Data Augmentation
Last Updated :
03 Aug, 2023
Data augmentation is the process of increasing the amount and diversity of data. We do not collect new data, rather we transform the already present data. I will be talking specifically about image data augmentation in this article. So we will look at various ways to transform and augment the image data. This article covers the following articles -
- Need for data augmentation
- Operations in data augmentation
- Data augmentation in Keras
- Data augmentation using Augmentor
1. Need for data augmentation Data augmentation is an integral process in deep learning, as in deep learning we need large amounts of data and in some cases it is not feasible to collect thousands or millions of images, so data augmentation comes to the rescue. It helps us to increase the size of the dataset and introduce variability in the dataset. 2. Operations in data augmentation The most commonly used operations are-
- Rotation
- Shearing
- Zooming
- Cropping
- Flipping
- Changing the brightness level
Now we will look at all these operations in detail. I will also provide the code for data augmentation later in the article. The original image that I will use for illustration
Rotation Rotation operation as the name suggests, just rotates the image by a certain specified degree. In the example below, I specified the rotation degree as 40.
Shearing Shearing is also used to transform the orientation of the image. The result of shearing operation looks like this -
Zooming Zooming operation allows us to either zoom in or zoom out. The result looks like this -
Cropping Cropping allows us to crop the image or select a particular area from an image.
Flipping Flipping allows us to flip the orientation of the image. We can use horizontal or vertical flip. You should use this feature carefully as there will be scenarios where this operation might not make much sense e.g. suppose you are designing a facial recognition system, then it is highly unlikely that a person will stand upside down in front of a camera, so you can avoid using the vertical flip operation.
Changing the brightness level This feature helps us to combat illumination changes.You can encounter a scenario where most of your dataset comprises of images having a similar brightness level e.g. collecting the images of employees entering the office, by augmenting the images we make sure that our model is robust and is able to detect the person even in different surroundings.
3. Data augmentation in Keras Keras is a high-level machine learning framework build on top of TensorFlow. I won't go into the details of the working of Keras, rather I just want to introduce the concept of data augmentation in Keras. We can perform data augmentation by using the ImageDataGenerator class. It takes in various arguments like - rotation_range, brightness_range, shear_range, zoom_range etc. Code : Python code implementing Data augmentation
Python3
# Importing necessary functions
from keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.utils import array_to_img, img_to_array, load_img
# Initialising the ImageDataGenerator class.
# We will pass in the augmentation parameters in the constructor.
datagen = ImageDataGenerator(
rotation_range = 40,
shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip = True,
brightness_range = (0.5, 1.5))
# Loading a sample image
img = load_img('image.jpg')
# Converting the input sample image to an array
x = img_to_array(img)
# Reshaping the input image
x = x.reshape((1, ) + x.shape)
# Generating and saving 5 augmented samples
# using the above defined parameters.
i = 0
for batch in datagen.flow(x, batch_size = 1,
save_to_dir ='preview',
save_prefix ='image', save_format ='jpeg'):
i += 1
if i > 5:
break
The above code snippet allows you to generate 5 augmented images having different zoom, rotation, brightness etc. Augmented Images
4. Data augmentation using Augmentor
Python3
# Importing necessary library
import Augmentor
# Passing the path of the image directory
p = Augmentor.Pipeline('image_folder')
# Defining augmentation parameters and generating 5 samples
p.flip_left_right(0.5)
p.black_and_white(0.1)
p.rotate(0.3, 10, 10)
p.skew(0.4, 0.5)
p.zoom(probability = 0.2, min_factor = 1.1, max_factor = 1.5)
p.sample(5)
The above code snippet allows you to generate 5 augmented images based on the features defined above. Augmented Images
You can checkout the Augmentor github repository for more information. References - https://keras.io/preprocessing/image/ https://github.com/mdbloice/Augmentor
Similar Reads
NLP Augmentation with nlpaug Python Library Data augmentation is a crucial step in building robust AI models, especially during the data preparation phase. This process involves adding synthetic data to the existing datasets to enhance the quality and diversity of the training data. For textual models, such as generative chatbots and translat
6 min read
Audio Data Preparation and Augmentation in Tensorflow Audio is nothing but any type of sound in a digital format. It can be speech, music, environmental sounds, etc. Audio data is commonly used in various fields such as speech recognition, music analysis, audio classification, and more. In recent years, audio data has become an increasingly important d
8 min read
Pgmagick enhance() method - Python The enhance() function is an inbuilt function in the Pgmagick library which is used to apply a digital filter to enhance a noisy image. The function returns the true value on success. Syntax: enhance() Parameters: This function does not accept any parameters. Return Value: This function returns the
1 min read
Python for Machine Learning Welcome to "Python for Machine Learning," a comprehensive guide to mastering one of the most powerful tools in the data science toolkit. Python is widely recognized for its simplicity, versatility, and extensive ecosystem of libraries, making it the go-to programming language for machine learning. I
6 min read
Python Image Processing Libraries Python offers powerful libraries such as OpenCV, Pillow, scikit-image, and SimpleITK for image processing. They offer diverse functionalities including filtering, segmentation, and feature extraction, serving as foundational tools for a range of computer vision tasks.Python Image Processing Librarie
11 min read
Implementing an Autoencoder in PyTorch Autoencoders are neural networks designed for unsupervised tasks like dimensionality reduction, anomaly detection and feature extraction. They work by compressing data into a smaller form through an encoder and then reconstructing it back using a decoder. The goal is to minimize the difference betwe
4 min read