Augmentation on datasets is very common technique used in machine learning where we need to add more details or information in our datasets. Data Augmentation used to increase the amount and diversity of datasets because sometimes we don’t want to collect new data, rather we apply transformation to the already collected datasets. 
Peepgrams image processing



Why we Need data Augmentation? This is the first thing as a machine learning student comes into your mind when you hear that term actually using this simple and easy technique we can increase and make our already collected datasets diverse because it is not feasible to collect thousands of images again so here data augmentation is better option to use after this datasets will be increase in size and it also introduce variability in the datasets. Operations in data augmentation are commonly used in Digital image processing the most commonly used terms are: Rotation,  Shearing,  Zooming,  Cropping, Flipping,  Controlling Brightness level, Vertical shift, Horizontal shift. For implementation you need to have sound knowledge of python programming language and machine learning understanding of Keras is must for doing this. Keras is a high-level machine learning framework and it is opensource neural network library which is written in python programming language build on top of TensorFlow. for data augmentation we will use ImageDataGenerator class import from Keras then we will give different arguments which we wish to perform on our dataset such as described earlier Rotation_range, brightness_range, shear_range, zoom_rnage etc.



Implementation of the Data Augmentation in Python is really an easy thing you just need to follow simple steps as i explain below with Code:

first you need to import important functions using Keras library using simple from and import keyword
# Importing necessary functions
from keras.preprocessing.image import ImageDataGenerator, 
array_to_img, img_to_array, load_img

after importing ImageDataGenerator we need to specify argument list for for
passing into the Constructor parameter list is the operations which you wish
to perform with your datasets images

datagen = ImageDataGenerator(
        rotation_range = 40,
        shear_range = 0.2,
        zoom_range = 0.2,
        horizontal_flip = True,
        brightness_range = (0.5, 1.5))  
  
after defining parameter list you need to define input image or a folder for taking multiple image  for simple on image you need to write this

img = load_img('image path'

it will load only one image from the specific path if you want to take multiple images at once you just need a loop for instant you can use this code snippet in place of above line

take_all_images = os.listdir('path of the images ')
 img = load_img('/path/'+i)
Note: loop should be use before image loading code 'i' is actually that loops variable
Converting image into array for procession:
x = img_to_array(img)

and reshaping the image:

x = x.reshape((1, ) + x.shape) 

this is the final step where we actually performed augmentation using the parameter
list and generate 10 augmentated picture
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 > 10:
        break


Results of Data Augmentation:
Original Image:


Original Image 

Images Created from input image:






Code implementation (Single Image Augmentation):
#importing all important libs
import cv2
import os
import numpy as np
# Importing necessary functions 
from keras.preprocessing.image import ImageDataGenerator,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.51.5)) 
  
# Loading a sample image 
img = load_img('Path of image'
# 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 ='Path where to save output'
            save_prefix ='image', save_format ='jpeg'): 
  i += 1
  if i > 10
    break
Code implementation (Multiple Images Augmentation):
(take path of folder and augment all images at once)
#importing all important libs
import cv2
import os
import numpy as np
#reading all images in list_all_images
list_all_images= os.listdir('/content/imagesd')
# Importing necessary functions 
from keras.preprocessing.image import ImageDataGenerator,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.51.5)) 
#i will iterate all images present in the given path i contains all images one by one

for i in list_all_images: 
  # Loading a sample image 
  img = load_img('/content/imagesd/'+i) 
  print(i) #to see what i hold at a time
  # 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 ='/content/'
            save_prefix ='image', save_format ='jpeg'): 
  i += 1
  if i > 10
    break

Conclusion: Using this simple augmentation technique we have every possible orientation of our dataset's images which will enhance our detection or pattern recognition accuracy for instant if we detection human face we always need every possible angle of faces so we must use this to generate different desire angles instead of taking more images.

if you feel this post is useful and provides you what you looking for please feel free to provide feedback so i can do more hard work and make thing more easy to understand.





1 Comments

Post a Comment

Previous Post Next Post