Skip to main content

How to use Keras for Computer Vision- Part 2


Another post starts with you beautiful people!
I hope you have enjoyed my last post and now you have become familiar with image loading, manipulation and saving in deep learning. If you have not read my last post then I recommend you to read the first part of this series. In this second part of my post we will learn about one of the most important technique used for a small image training dataset- Image Data Augmentation!

1. What is Image Data Augmentation?
Image Data Augmentation is a technique to expand the size of a training dataset. This technique can create modified versions of images which helps when we have a small dataset. Image shifting, flipping and zooming are some examples of this technique. Keras provides ImageDataGenerator class for this purpose.

2. How to use this technique?
We will learn different uses of this technique one by one by applying on an image-

A. How to shift an image?
Shifting an image means we are moving all pixels in one direction while keeping the image dimensions the same. ImageDataGenerator class has width_shift_range and height_shift_range arguements for shifting an image. Let's see how can we horizontal shift an image-
In above code cell I have loaded an image of ICC Cricket World Cup 2019 Trophy. I will horizontal shift this image using width_shift_range arguement. This arguement takes min and max range. In our example I am taking -150 and 150 as min and max pixels. After creating the iterator we are  calling it nine times for augmentation. Once we run above cell we get following output-
In above output cell you can see our loaded image is augmented 9 times. The plot is showing randomly selected positive and negative horizontal shifts. You can try the same code but with height_shift_range arguement for vertical shifting. Remember in this case you need to pass the percentage of image to shift (for example height_shift_range=0.4) instead of [min,max] values.

B. How to flip an image?
Flipping an image means reversing the rows or column of pixels. The ImageDataGenerator class has horizontal_flip and vertical_flip arguements for flipping. Let's see how can we augment an image with horizontal flip-
The above code is quite same as we saw earliar except one change. Here we are passing horizontal_flip arguement which takes a boolean value- either True or False. After running the above cell we will get following plot-
Compare this plot with first one. What changes are you seeing?

C. How to rotate an image?
Image rotation is also part of augmentation technique.We can randomly rotate the image clockwise by just giving a number between 0 to 360 in rotation_range arguement. Let's see how can we do it-
Once we run above cell we see our loaded image is rotated as below-
D. How to augment an image by brightness?
We can augment an image by it's brightness using the brightness_range arguement. This arguement takes min and max range indicating percentage of darkness or brightness amount. Let's see how can we use this type of augmentation-
In above code cell I have given brightness_range arguement with 0.2 and 1.0 value. Here value less than 1.0 means darkening the image while larger than 1.0 means brighten the image whereas 1.0 means no affect on brightness. running above cell gives us following plot-
E. How to apply zoom augmentation?
Zoom augmentation randomly zooms an image. We can use zoom_range arguement which takes percentage of zoom as value. The zoom values less than 1.0 will zoom the image in and values larger than 1.0 will zoom the image out. Let's see how can we do that-
Running above code gives you following result having zoom in effect-

That's it for today. You can save the augmented images using the save_img() function as we did in our first post. In this way you can larger your small image training datatset. We have learnt 5 types of image augmentation. Apply each technique on your own image and experiment it with different different values of arguements. For more details of image data augmentation I recommend you to read following two awesome blogs-
1. https://www.pyimagesearch.com/2019/07/08/keras-imagedatagenerator-and-data-augmentation/
2. https://machinelearningmastery.com/how-to-configure-image-data-augmentation-when-training-deep-learning-neural-networks/
In next post of this series we will learn about the different colour channel ordering. Till then Go chase your dreams, have an awesome day, make every second count and see you later in my next post.

Comments

  1. Data Science Courses in Gurgaon at APTRON have been designed with the objective of developing in the candidates, the capacity to master the professional techniques towards acquiring the best and desirable value for the companies.

    For More Info: Data Science Course in Gurgaon

    ReplyDelete
  2. Its very informative blog and useful article thank you for sharing with us , keep posting learn Data Analysis Courses in Pune

    ReplyDelete
  3. great post about data science using python i am currently doing my training from data analytics course in pune . it is the best it training institute in pune , thanks

    ReplyDelete

Post a Comment

Popular posts from this blog

How to use opencv-python with Darknet's YOLOv4?

Another post starts with you beautiful people 😊 Thank you all for messaging me your doubts about Darknet's YOLOv4. I am very happy to see in a very short amount of time my lovely aspiring data scientists have learned a state of the art object detection and recognition technique. If you are new to my blog and to computer vision then please check my following blog posts one by one- Setup Darknet's YOLOv4 Train custom dataset with YOLOv4 Create production-ready API of YOLOv4 model Create a web app for your YOLOv4 model Since now we have learned to use YOLOv4 built on Darknet's framework. In this post, I am going to share with you how can you use your trained YOLOv4 model with another awesome computer vision and machine learning software library-  OpenCV  and of course with Python 🐍. Yes, the Python wrapper of OpenCV library has just released it's latest version with support of YOLOv4 which you can install in your system using below command- pip install opencv-pyt...

How to convert your YOLOv4 weights to TensorFlow 2.2.0?

Another post starts with you beautiful people! Thank you all for your overwhelming response in my last two posts about the YOLOv4. It is quite clear that my beloved aspiring data scientists are very much curious to learn state of the art computer vision technique but they were not able to achieve that due to the lack of proper guidance. Now they have learnt exact steps to use a state of the art object detection and recognition technique from my last two posts. If you are new to my blog and want to use YOLOv4 in your project then please follow below two links- How to install and compile Darknet code with GPU? How to train your custom data with YOLOv4? In my  last post we have trained our custom dataset to identify eight types of Indian classical dance forms. After the model training we have got the YOLOv4 specific weights file as 'yolo-obj_final.weights'. This YOLOv4 specific weight file cannot be used directly to either with OpenCV or with TensorFlow currently becau...

How can I make a simple ChatBot?

Another post starts with you beautiful people! It has been a long time of posting a new post. But my friends in this period I was not sitting  where I got a chance to work with chatbot and classification related machine learning problem. So in this post I am going to share all about chatbot- from where I have learned? What I have learned? And how can you build your first bot? Quite interesting right! Chatbot is a program that can conduct an intelligent conversation based on user's input. Since chatbot is a new thing to me also, I first searched- is there any Python library available to start with this? And like always Python has helped me this time also. There is a Python library available with name as  ChatterBot   which is nothing but a machine learning conversational dialog engine. And yes that is all I want to start my learning because I always prefer inbuilt Python library to start my learning journey and once I learn this then only I move ahead for another...