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
# 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
# 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