Image Classification Using Transfer Learning.
Image Classification using pre-trained Inception v3 Convolution Neural Network Model.
Introduction
Today we are going to create a python program which will classify image using pretrained Inception-v3 model.
What is pretrained model?
A pre-trained model is a model created by some one else to solve a similar problem. Instead of building a model from scratch to solve a similar problem, you use the model trained on other problem as a starting point.
So instead of creating a image classification model from scratch we can simply use some pretrained model for our project. There are few different pretrained model available in Keras , link.
What is inception-v3?
Inception-v3 is a pre-trained convolutional neural network model that is 48 layers deep.
It is a widely-used image recognition model that has been shown to attain greater than 78.1% accuracy on the ImageNet dataset. The model is the culmination of many ideas developed by multiple researchers over the years.
This pre-trained network can classify images into 1000 object categories, such as keyboard, mouse, pencil, and many animals. In input size of 299 by 299.
Architecture of inception-v3
More information about inception-v3 model can be found here link.
Now lets start with the code.
Import the required modules
We are going to use TensorFlow v2.x
import tensorflow as tf
import numpy as np
import pandas as pd
import keras
import requests
Download and import inception-v3
We are going to download and import inception-v3 from keras.applications.
inception = tf.keras.applications.InceptionV3()
Download human-readable text for prediction
We download human readable text for Image-Net
and separate them.
response = requests.get("https://git.io/JJkYN")
label = response.text.split('\n')
Image classification function
The user will input the image and the function will reshape the image in 299 by 299 and three channel because the input size of inception model is 299 by 299 and pass the image to the pretrained inception-v3, which will give the prediction, and the function will return first 1000 predicted values.
def classify(image):
image = image.reshape((-1, 299, 299, 3))
image=tf.keras.applications.inception_v3.preprocess_input(image)
prediction = inception.predict(image).flatten()
return {label[i]: float(prediction[i]) for i in range(1000)}
Prediction function
In prediction function, the user will pass the image and with the help of opencv-python, function will read and resize the image and pass the image to image classifier function. The function will return 1000 values in which a lot of them is not required and in order to get the only prediction with the highest value we will use Counter from collections modules, which will sort the directory and give the prediction with the highest value.
By default only the prediction with highest value is given, but the user can get as many top prediction they want simply by passing the value of
n
from collections import Counter
import cv2 def prediction(image, n=1):
image = cv2.imread(image)
image = cv2.resize(image, (299, 299), 3)
label = classify(image)
k = Counter(label)
high = k.most_common(n)
for i in high:
print ("{} : {}".format(i[0], i[1]))
Prediction
We are going to give the model a unknow image and check the top predicted value.
image = 'unknown.jpg'prediction(image)
Let check the actual image.
import matplotlib.pyplot as pltimg = cv2.imread(image1)
plt.imshow(img)
As we can see the model classification was spot on.
We can experiment with model with different type of object and animal image and predict the label.
Thanks for reading the article.
~ Happy Learning