Image Recognition Using Keras

Image Recognition Using Keras : Build from Pre-Trained Models

Image Recognition is trending technology in cameras and other equipment that require image processing. You can use OpenCV for it but there are also other Packages that do manipulation on it in a few lines of code. One of them is Keras. Keras is a popular deep learning framework. Not you can only build your machine learning model using Keras, but you can also use a pre-trained model that is built by the other developers. There are many Image Recognition built-in Model in the Keras and We will use them. In this entire intuition, you will learn how to do image recognition using Keras.

Pre-Built Image Recognition Model

Before going into the coding parts, you should know about the various models that are already built. Some of them are :

1. ImageNet – It contains millions of pictures that are labeled. Thus you can use it to train your own model to recognize them .

2. ILSVRC – It stands for Large Scale Visual Recognition Challenge. Every year people around the world took part in the competition for object detection. It has object detection for 200 labeled categories.

There are other Image Recognition Models in Keras Module. Currently, it is of Four Types.

VGG – It is deep learning model with 16 or 19 layers and It takes a lot of memory to run.

ResNet50 -It has 50 Layers inside the deep neural networks. Thus makes it more accurate and consume less memory than the VGG.

Xception – You can directly reuse it in your programs to recognize objects and images.You can tune it according to your need rather than training your model from scratch.

Steps to Model an Image Recognization

Step 1: Import necessary libraries

For this project, I have imported numpy and Keras packages only. Numpy will be used for creating a new dimension and Keras for preprocessing and importing the resnet50 pre-trained model.

import numpy as np
from keras.preprocessing import image
from keras.applications import resnet50

From keras.preprocessing  I am importing an image for loading the sample image and converting the image to numpy array.

Step 2:  Load and Convert the Image

The next step is to load your sample image and then converting the image to numpy array. The load_img() and img_to_array() method will load and convert the image to the numpy array.  Inside the load_img() method you have to pass the path of the image and also remember that the size of the image for feeding to the neural network is 224×224 .  Therefore I had pass the target_size = (224,224) to this method. Use the following code

# load the image
img = image.load_img("panda.jpg", target_size=(224, 224))
# convert the image to the numpy
x = image.img_to_array(img)

Step 3: Add New Dimension

The resnet50 requires a list or array of the images to be feed into the network. But in this example, I am just passing only one image. Therefore I am creating a new dimension thus turning the single image to the array of the multiple images. If you are feeding the array of the images then you can ignore this step and move the next step.

# add fourth dimension as Keras wants a list of images
x = np.expand_dims(x, axis=0)

Step 4: Normalize the Image

In this step you have to preprocess the image before feeding it for prediction. The resenet50 has built in normalization function preprocess_input() that normalize the image.

# normalize the image
x = resnet50.preprocess_input(x)

Step 5: Make Predictions and Find its Classes

In the last step, you have to feed the input image (Normalized) to the model.predict() method for getting the predictions. And to find the classes you have to pass the prediction to resnet50.decode_predictions(predict, top=5) method.

# prediction
predict = model.predict(x)
# classes of the prediction
predict_class = resnet50.decode_predictions(predict, top=5)

Here I am finding the top 5 classes of the prediction. Lets print the accuracy and top 5 classes the resnet50 are predicting. Use the following code.

# print the results
for imagenet_id, name, likelihood in predict_class[0]:
print(f'name:{name},accuracy: {likelihood}')

Input

panda

Output

giant_panda_output

You can see the 5 predictions made by the Keras and Corresponding accuracy. Giant Panda is the prediction for this input.

Full Code

import numpy as np
from keras.preprocessing import image
from keras.applications import resnet50

# import the model
model = resnet50.ResNet50()

# load the image
img = image.load_img("giant_panda.jpg", target_size=(224, 224))
# convert the image to the numpy
x = image.img_to_array(img)

# add forth dimension as Keras wants list of images
x = np.expand_dims(x, axis=0)

# normalize the image
x = resnet50.preprocess_input(x)

# prediction
predict = model.predict(x)

# classes of the prediction

predict_class = resnet50.decode_predictions(predict, top=5)

# print the results
for imagenet_id, name, likelihood in predict_class[0]:
    print(f'name:{name},accuracy: {likelihood}')

 

Conclusion

Keras has many image recognization pre-trained models. Rather than creating your own model, you should use it for the image recognization. Resnet50 also allows you to add your own model with the existing model. Thus make it best among various models.

Hope you have understood the above steps. If you have any query about it then you can contact us to get more information about it. If you want to learn more about Keras here is the official Keras Link.

Data Science Learner Team

 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner