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.

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
take_all_images = os.listdir('path of the images ')
load_img('/path/'+i)
img =
Note: loop should be use before image loading code 'i' is actually that loops variable
Converting image into array for procession:
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.5, 1.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
(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.5, 1.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
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.
thanks a lot
ReplyDeletePost a Comment