Data Science

Image Data Augmentation. How and Why?

If we have limited amount of Data, We can diversify it using data augmentation. It is like instead of collecting new data elements we just transform which is already there to increase the sample size along with diversity. We will consider unstructured data i.e. Image data for augmentation process.

We will Discuss certain things such as…

  1. Need of Image Data Augmentation
  2. Commonly used Image Data Augmentation Techniques and Operations
  3. Using Keras and Augmentor for Augmenting process

Need of Image Data Augmentation

Suppose we are working on a Deep Learning project which usually needs a large set of data to create a proper prediction model. But it is not convenient for us to collect such a huge set then data augmentation can let us multiply our existing data set with a lot of possibilities of variability and diversity.

Machine Intelligence is the last Invention that humanity will need to make.

Nick Bostrom

Commonly used Image Data Augmentation Techniques and Operations

It is about creating variability or diversity on a single Image. Then what are the possibilities ?

These are following possibilities for which we can look for..

  1. Zoomimg
  2. Shearing
  3. Flipping
  4. Rotating
  5. Brightness variations

How can we apply these ? Lets Discuss…

Lets Consider an Image…

Zooming

It operates over an image to create an image either zoomed in or zoomed out.

Shearing

Shearing lets us create an sheared image.

Flipping

Through flipping we can flip an image either vertical or horizontal.

Rotating

Rotation basically used to rotate an image upto a defined degree.

Brightness Variations

We can enhance or decrease the brightness of an image using this operation.

Image Data Augmentation using Keras

Code for Keras based Augmentation

# Import keras library and required functions 
from keras.preprocessing.image import ImageDataGenerator,array_to_img, img_to_array, load_img 

# Initialise ImageDataGenerator class. 
# Pass the augmentation parameters in the constructor. 
datagen = ImageDataGenerator(zoom_range = 0.2,shear_range = 0.3,
                             horizontal_flip = True,
                             rotation_range = 50,
                             brightness_range = (0.5, 1.5))
 
# Get the sample image 
img = load_img('image.jpg') 
# Generate image features as array 
x = img_to_array(img) 
# Reshape the input image 
x = x.reshape((1, ) + x.shape) 

# Create a loop to generate and save five augumented images 
#using defined parameters
i = 0
for batch in datagen.flow(x, batch_size = 1,
                          save_to_dir ='E:/preview',
                          save_prefix ='image', save_format ='jpeg'): 
    i += 1
    if i > 5: 
        break

Outputs

Image Data Augmentation using Augmentor

Code for augmentor based Augmentation

import sys
sys.path.append('C:/Users/vipul/Anaconda3/envs/tensorenv/Lib/site-packages')

# Import Augmentor Library
#For installation : pip install Augumentor
import Augmentor 
# Locate image Directory 
p = Augmentor.Pipeline("E:\preview") 

# Generate Five samples based out of Augumentation Parameters
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) 

Outputs

Leave a Reply

Your email address will not be published. Required fields are marked *