So since now, we have loaded the Image and did some work on it so that we can “augment” the image. Now we are moving to the phase where we are going to actually augment the image.
Okay, sounds good right?
Suppose you don’t feel right! Then you should have missed what we did in my previous story. I am sure that you missed some of the important steps that you should do before augmenting the image.
If you already know what to do before “augmenting the image” and you clearly understand it then no problem. If not I can help you
DON’T WORRY I GOT YOU:
HERE IS THE LINK TO MY STORY: — IMAGE AUGMENTATION PART 2.
It is just a 5-minute story. So don’t take too much time and once you are done. You are ready to get onboard on this story for “AUGMENTING THE IMAGE”.
WHAT YOU ARE GOING TO LEARN:
- CODE TO AUGMENT THE IMAGES.
- BASIC LIBRARIES AND IT’S PARAMETERS.
So this is the image we will do AUGMENTATION. So a quick note that the augmentation is specific to your application or your ML model. Here I will do augmentation in such a way that the ML model can recognise it as TIM even if the photo is upside down.
LET’S GET STARTED
We will begin with the code
from tensorflow.keras.preprocessing.image import ImageDataGenerator
datagen=ImageDataGenerator(shear_range=0.2,Horizontal_flip=True,Vertical_flip=True,
Zoom_range=0.5,rescale=1./255,width_shift_range=0.2,
height_shift_range=0.2,Rotation_range=50)
i=0
plt.figsize(figsize=(10,10))
for batch in datagen.flow(x,batch=1):
plt.subplot(3,3,i+1)
plt.imshow(batch[0])
plt.axis=('off')
i +=1
if(i >=16):
breakplt.show()
Don’t get worried by the size of the code.
It is our first step in Augmenting the image in which we will import the “class” called ImageDataGenerator. This augments the image and It doesn’t take any memory to store the image because it augments the image on the fly.
STAY UPDATED:
You may be wondering that the Tensorflow and Keras are separate libraries but here I am mentioning the Keras as a submodule of the Tensorflow library. This is because, in Sep 2019, Tensorflow 2.0 was released and was integrated with Keras.
Let’s move on to the next step:
Here we created the object for our class ImageDataGenerator also we are passing on some of the attributes for this class. In a nutshell, what we are basically doing is telling the class what are the ways we want our image to be augmented.
NOTE:
There are also many “attributes” other than the one we are using here. These “attributes” change according to the task we do.
While researching for this story I found an informative story of Greeshma Pillai in her story called Boosting Model Performance. Other attributes for the “ImageDataGenerator” class are explained.
LET’S START WITH THE SOME ATTRIBUTE AND TACKLE THEM ONE BY ONE:
shear_range=0.2
width_shift_range=0.2
heigth_shift_range=0.2
zoom_range=0.5
rescale=1./255
Rotation_range=50
vertical_flip=True
horizontal_flip=True
- SHEAR_RANGE = 0.2
Here I used the rectangle in here to illustrate because observing the effect of shear_range in real images is quite difficult and in the above image we can see that the rectangle’s bottom is fixed and the top portion is “Sheared” making it a parallelogram. This is the same happens in our image which makes it tilted in some way.
2.WIDTH_SHIFT_RANGE and HEIGHT_SHIFT_RANGE
In the above image, I explained what happens in a clear and efficient way. When we specify height_shift_range=0.2 and width_shift_range=0.2. What happens is we are telling the machine to shift the pixel in the width and height respectively.
The input that we gave in our case (0.2) is telling the machine to use 20% of the width and height of the whole image pixel size and do the shifting in width and height of the image.
“IF YOU HAVE NOTICED THE IMAGE CLEARLY. YOU CAN SEE THAT THE WIDTH SHIFT RANGE SHIFTS THE IMAGE BY “HEIGHT” AND THE HEIGHT SHIFT RANGE SHIFTS THE IMAGE BY “WIDTH”. BUT DON’T WORRY, THIS IS HOW IT WORKS. THERE ARE CERTAIN REASONS BEHIND IT AND WE DON’T NEED THEM NOW”.
4. ROTATION RANGE = 50
We can see that the original image and the rotated image are slightly tilted in a certain way and this rotation of the image will be only in the range of our input value. The larger the input larger the degree of (rotation or siltation) done in our image. Here I showed only two but many rotated images can be produced.
5. ZOOM_RANGE = 0.5
In here In the image above we can see that the “dog image” is zoomed in and zoomed out. In the code, we gave the input as “0.5″ which means the Zooming in and the Zooming out can be done between the range of 10% to 50%.
6.VERTICAL_FLIP AND HORIZONTAL_FLIP
Here the images are flipped and THERE IS ONE THING THAT LOOKS REALLY VERY AWESOME. SEE THE WAY THE DOG LOOKS WHEN WE DO THE HORIZONTAL FLIPPING (POWER IN HIS LOOK).
When we set the VERTICAL_FLIP = True, It allows the machine to do vertical flipping in the image and when we set the HORIZONTAL_FLIP=True, It allows the machine to do horizontal flipping on the image.
7.RESCALE = .1/255
DON’T HURRY I WILL JUST FINISH IN A MINUTE.
Long story short: — each image has pixels within it and it has values inside it when we are doing the image augmentation. The values should be Scaled down. This should be done so that the machine can work efficiently.
NOTE: I EXPLAINED IT IN THE LAST. BUT THIS IS THE STEP THAT IS COMPULSORY TO DO AND THIS SHOULD BE DONE FIRST. THEN ONLY IMAGE AUGMENTATION CAN TAKE PLACE.
Okay, we covered a lot and I know it is quite heavy. But I did my best in explaining this in simple and understandable way.
I want to finish off with this and the rest of the code will be discussed in the next story.
HERE IS THE LINK TO THE GOOGLE COLLAB:- IMAGE AUGMENTATION.
WHAT YOU LEARNED:
- BASIC PARAMETERS FOR IMAGE AUGMENTATION.
I learned a lot while writing this story and I believe you also learned while reading the story.
You know that your feedback is very essential for me.
YOUR FEEDBACK IS HEARTILY WELCOMED.
THANK YOU FOR SPENDING YOUR TIME WITH ME……