Image Recognition using Pre-trained Xception Model in 5 steps
Hello Readers,
In this article, we will take you to predict images using Convolutional Neural Network(specifically using Xception Model) pre-trained on the ImageNet database with python and Keras deep learning library.
What is ImageNet?
ImageNet is a project intended to label and categorize Images manually. in the field of Deep Learning and Convolutional Neural Networks, we will refer ImageNet as “ImageNet Large Scale Visual Recognition Challenge” in short ILSVRC.
The main objective of this ImageNet project is to train a model, which can classify an Input Image into 1000 separate object categories.
Categories include species of dogs, cats, fish, Vehicle types include vehicle parts like wheels, steering, etc to know more Categories refer
The models are trained on approximately 1.2 million Images and additional 50000 images for validation and 100,000 images for testing.
for Image Recognition, we can use pre-trained models available in the Keras core library. the models like VCG16, VCG19, Resnet50, Inception V3, Xception models
In this article, we have chosen the Pre-trained Xception Model for Image Classification
Xception Model
Xception Model is proposed by Francois Chollet. Xception is an extension of the inception Architecture which replaces the standard Inception modules with depthwise Separable Convolutions.
Comparison of ImageNet: Xception Model with other Models
Now let us write a python script for Image Recognition using Xception Model
Step1: Installing required dependencies
for Image Recognition, we rely on libraries Numpy, Matplotlib(for visualization), tf-explain(to import pre-trained models), Tensorflow with Keras as backend
open command prompt (cmd) > run below command
pip install tf-explain
Step2: Importing packages
#import librariesimport numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
from tf_explain.core.activations import ExtractActivations
from tensorflow.keras.applications.xception import decode_predictions
%matplotlib inline
Step3: Loading Pretrained Model
#load pre trained Xception modelmodel=tf.keras.applications.xception.Xception(weights='imagenet',include_top=True)#Summary of Xception Model
print(model.summary)
Step4: Loading and pre-processing Sample Input Image
#loading and preprocessing cat imageIMAGE_PATH='./lion.jpg'
img=tf.keras.preprocessing.image.load_img(IMAGE_PATH,target_size=(299,299))
img=tf.keras.preprocessing.image.img_to_array(img)#view the image
plt.imshow(img/255.)
Step5: Prediction of objects in the Image
Now let us make the top 5 predictions on the input image using the Xception Model which we loaded successfully in our IDE.
import requests#fetching labels from Imagenet
response=requests.get('https://storage.googleapis.com/download.tensorflow.org/data/imagenet_class_index.json')
imgnet_map=response.json()imgnet_map={v[1]:k for k, v in imgnet_map.items()}#make model predictionsimg=tf.keras.applications.xception.preprocess_input(img)
predictions=model.predict(np.array([img]))
decode_predictions(predictions,top=5)
Result
from the below snippet we found that Xception model has predicted that Image consists of Lion with the probability of 0.87(87%)and other animals with a probability of 0.001(less than 1%).
References
- Xception: Deep Learning with Depthwise Separable Convolutions(https://arxiv.org/abs/1610.02357)
- ImageNet (http://www.image-net.org/)