Skip to main content

How to use Keras for Computer Vision- Part 1


Another post starts with you beautiful people! Today we are going to enter into the Computer Vision (CV) field with this first post. With this and further coming posts, we will learn how to use Deep Learning for CV. We will cover following three key concepts with Python code in this post-
1. What is CV?
2. How to load and manipulate image using Keras?
3. How to scale your image using Keras?
Let's starts our learning-

1. What is CV? 
Computer Vision or CV is a sub-field of Machine Learning and Artificial Intelligence, focused on the problem of helping computers to see. The goal of CV is to understand the content of digital images. CV has a wide variety of tasks-

  • Object Classification
  • Object Identification
  • Object Verification
  • Object Detection
  • Object Segmentation
  • Object Recognition
2. How to load and manipulate image using Keras?
A. Loading an image using Keras api is very easy. Keras provides load_img() function for loading an image. Before moving further make sure you have install keras. If not then run following command in Anaconda Prompt: conda install keras. The below Python code snippet demonstrates how to use this function-

In above output shell we can see my uploaded image format, it's mode and size. The image will not open in our notebook. It will be displayed using default application. In my case it was displayed by Photos app-
B. Manipulating an image is an integral part in deep learning and it starts with converting the image into numpy array format. Keras api provided img_to_array() and array_to_img() functions for this manipulation. Let's see how can we use both these two functions-
In above output cell, the uploaded image is converted into numpy array in float data type. The pixel values are converted into the format of (Height, Width, Channels). Finally the image is converted back to original PIL format.

C. Saving an image using Keras api is also easy. It provides save_img() function for this task. Following example will demonstrate how to load an image in grayscale format, converts it to numpy array and then save it to a new file name-


In above output, you can see our loaded image is converted into grayscale format and in numpy array. We have also saved and uploaded the new grayscale image.

3. How to scale your image using Keras?
Deep Learning models accept input images in scaled form. So we must scale our input images before training and evaluation of our deep learning model. Keras provides ImageDataGenerator class and API for just-in-time image scaling. To understand how to use this class and API, we will work on the same MNIST digit dataset which we have seen earliar in this post. For a quick revision the MNIST dataset, is an image classification problem comprised of 70,000 images of handwritten digits. The goal of the problem is to classify a given image of a handwritten digit as an integer from 0 to 9. So it is a multiclass image classification problem.

A. Load the dataset using keras
From above output we can see that there are 60000 images in training dataset and 10000 images in testing dataset. All images are 28 x 28 pixels. Pixel values are in between 0 to 255 and mean/std deviation of datasets are similar.

B. Scale the pixels using ImageDataGenerator
ImageDataGenerator class has three main scaling methods: Pixel Normalization, Pixel Centering, Pixel Standardization. Pixel Normalization technique scales pixel value to the range 0-1. Pixel Centering technique scales pixel values to have a zero mean. Pixel Standardization technique scales pixel values to have a zero mean and unit variance. Let's learn how to use each technique one by one-

1. Pixel Normalization: This can be achieved by setting the rescale argument to a ratio by which each pixel can be multiplied to achieve the desired range. In our case it would be 1.0/255.0.
In above output cell we can see that minimum and maximum pixels of training and testing datasets- 0 and 255. Since MNIST dataset has only black and white images, it has only one channel. If you are applying this to color images then minimum and maximum values will be calculated across all channels. In this example the ImageDataGenerator does not need to be fit on the training dataset as there is nothing that needs to be calculated, we have provided the scale factor directly.

2. Pixel Centering: In this technique we calculate the mean pixel value across the entire training dataset, then subtract it from each image.
In above output cell you are seeing the mean pixel values of training and test datasets. The ImageDataGenerator is fit on the training dataset and we have confirmed that this mean pixel value matches with our calculated mean pixel value in next line. You can see mean pixel value for the scaled dataset is close to zero.

3. Pixel Standardization: Standardization of images is achieved by subtracting the mean pixel value and dividing the result by the standard deviation of the pixel values. The mean and standard deviation statistics can be calculated on the training dataset.
In above output cell, you can see in the first line- the mean and standard deviation of pixel values in both datasets. The data generator is then configured for feature-wise standardization and the statistics are calculated on the training dataset, which is matching what we would expect when the statistics are calculated manually. The test is then repeated on the entire training dataset and we can confirm that the mean is very small value close to 0.0 and the standard deviation is a value very close to 1.0.

That is enough learning for today. After reading this post I recommend you to run above code examples in your notebook and understand each line. After this you can repeat the same techniques with any color image dataset. Working on a color dataset will teach you how to modify data scaling techniques. If you follow what I suggest then believe me you will become computer vision Ninja at the end of coming posts. In the next post we will learn the standard way to lay out our image data and image augmentation for modeling. Till then Go chase your dreams, have an awesome day, make every second count and see you later in my next post.

Comments

Post a Comment

Popular posts from this blog

How to deploy your ML model as Fast API?

Another post starts with you beautiful people! Thank you all for showing so much interests in my last posts about object detection and recognition using YOLOv4. I was very happy to see many aspiring data scientists have learnt from my past three posts about using YOLOv4. Today I am going to share you all a new skill to learn. Most of you have seen my post about  deploying and consuming ML models as Flask API   where we have learnt to deploy and consume a keras model with Flask API  . In this post you are going to learn a new framework-  FastAPI to deploy your model as Rest API. After completing this post you will have a new industry standard skill. What is FastAPI? FastAPI is a modern, fast (high-performance), web framework for building APIs with Python 3.6+ based on standard Python type hints. It is easy to learn, fast to code and ready for production . Yes, you heard it right! Flask is not meant to be used in production but with FastAPI you can use you...

Learn the fastest way to build data apps

Another post starts with you beautiful people! I hope you have enjoyed and learned something new from my previous three posts about machine learning model deployment. In one post we have learned  How to deploy a model as FastAPI?  I n the second post, we have learned  How to deploy a deep learning model as RestAPI ? and in the third post, we have also learned  How to scale your deep learning model API?   If you are following my blog posts, you have seen how easily you have transit yourselves from aspiring to a mature data scientist. In this new post, I am going to share a new framework-  Streamlit which will help you to easily create a beautiful app with Python only. I will show here how had I used the Streamlit framework to create an app for my YOLOv3 custom model. What is Streamlit? Streamlit’s open-source app framework is the easiest way for data scientists and machine learning engineers to create beautiful, performant apps in only a few hours!...

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...