• For any query, contact us at
  • +91-9872993883
  • +91-8283824812
  • info@ris-ai.com

Image Augmentation

Image Augmentation is the process of increasing the amount and diversity of data.

These are the following topics under Image Augmentation

  1. Need for Image Augmentation
  2. Operations in Image Augmentation
  3. Image Augmentation in Keras
  4. Image Augmentation using Augmentor

1. Need for Image Augmentation

Image 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 Image Augmentation comes to the rescue. It helps us to increase the size of the dataset and introduce variability in the dataset.

2. Operations in Image Augmentation

The most commonly used operations are-

  1. Rotation
  2. Shearing
  3. Zooming
  4. Cropping
  5. Flipping
  6. Changing the brightness level

3. Image Augmentation in Keras

Keras is a high-level machine learning framework build on top of TensorFlow.We can perform Image Augmentation by using the ImageDataGenerator class. It takes in various arguments like – rotation_range, brightness_range, shear_range, zoom_range etc.

In [2]:
import tqdm as tqdm
from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img
   
# Initialising the ImageDataGenerator class.
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('/home/webtunix/Downloads/Aug/gavin-allanwood-hcxqLJjI99E-unsplash.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 tqdm.tqdm(datagen.flow(x, batch_size = 1,save_to_dir ='/home/webtunix/Downloads/Aug/',
                          save_prefix ='rabbit', save_format ='jpg')):
    i += 1
    if i > 5:
        break
5it [00:51, 10.34s/it]                       

4.Image Augmentation using Augmentor

In [ ]:
!pip3 install Augmentor
In [26]:
import Augmentor
# Passing the path of the image directory
p = Augmentor.Pipeline("/home/webtunix/Downloads/Aug/")
  
# Defining augmentation parameters and generating 5 samples
p.flip_left_right(0.2)
p.black_and_white(0.5)
p.rotate(0.5, 25, 25)
p.skew(0.4, 0.5)
p.zoom(probability = 0.2, min_factor = 1.1, max_factor = 1.5)
p.sample(6)
Executing Pipeline:   0%|          | 0/6 [00:00<?, ? Samples/s]
Initialised with 7 image(s) found.
Output directory set to /home/webtunix/Downloads/Aug/output.
Processing <PIL.Image.Image image mode=RGB size=6016x4016 at 0x7F9D66BB8D68>: 100%|██████████| 6/6 [00:10<00:00,  1.77s/ Samples]                  

rotation

Image Augmentation Rotation

skew

Image Augmentation Skew

Zoom

Image Augmentation Zoom

grey image

Image Augmentation Grayscale
In [ ]: