Image Data Augmentation in Python

INTRODUCTION: 

To be very precise and clear, if you have 100 images in a dataset. You want to train your model but 100 is a very less number for a model to train and provide accurate results. 

So, it would be great if you don't have to manually collect more images or download them more from the internet to increase the size of your dataset. 

What if you can convert those 100 images into 500  or more images and increase your dataset size multiple times. This process is known as Image data augmentation.

If you have worked on a real-time dataset or created your own dataset, you might have faced a problem of different and multiple images. Either we get tired of collecting images from different angles or we feel lazy as always.

So, in these short lines of code, you can create a dataset of a huge amount of images. The source code is provided at the bottom. I have taken an image of my helmet for reference. You can take any image which you want.



Have you started practicing data science? 

Yes -> Congratulations, you are on your way to becoming a data scientist soon. 

No -> Alright, it is the right time to start practicing it. 

I have some projects for you to start your journey in data science and machine learning.

DATA SCIENCE PROJECTS

Also, if you don't have any/much knowledge about algorithms used in data science and machine learning, then you must understand the most basic algorithms used in data science.

UNDERSTANDING MACHINE LEARNING ALGORITHMS

Now let's get started with the code without wasting much any of your time.

We will use Tensorflow(more specifically Keras), a very powerful library in Python to make our code very easy.

In the below part of code, libraries are simply imported.


After importing the libraries, we will immediately instantiate(call) the required libraries and methods and try to get our work finished in no time.


This is the most important line of the project. In the above line of code, the ImageDataGenerator method of the preprocessing library of Keras is being called. It takes a number of parameters depending upon the requirement of the user. 
Some examples of parameters are:
  • width_shift_range
  • height_shift_range
  • shear_range
  • zoom_range
  • rotation_range
  • horizontal_flip, vertical_flip

These parameters are very helpful in augmenting the images.



Here, rotation_range=40  tells Python that the image should be rotated by 40 degrees.

Let's look at some different examples first.
In the below image, you can see that we can shift the width and height range of the image.



We can change the brightness of the image so that there would be some change in pixel values which will ultimately change the matrix of the image. Brightness plays a very important role, especially in deep learning. The neural networks identify objects based on the pixel values. If you haven't build any neural network yet, try this: 


In the above image, it is clearly visible that there is a change in the brightness of the image. 
Have you heard about shear transformations? Shear transformation changes/slants the shape of the image by changing its coordinate values. An example of shear transformation is given below.


Similarly, we can zoom the image also according to our requirements.


Now, if you have followed it till here you might be wondering 
that would it be possible to use two or more augmentations at the same time. It is possible to do it.
For example, flip the image and rotate it at the same time.


So, you can see now that a single image of a helmet can be converted into 10 different images. You can use this technique to augment your images and increase your dataset size multiple times.


If you want to be more updated about my posts and projects, visit and subscribe to my Telegram channel
Kindly select an option from this page -> Next?

Thanks for reading! 

Post a Comment

1 Comments

Thank You!