DAY 46-100 DAYS MLCODE: Classification using Inception V3

My Tech World

DAY 46-100 DAYS MLCODE: Classification using Inception V3

December 27, 2018 100-Days-Of-ML-Code blog 0

In the previous blog, we created a simple CNN model and tested using MNIST data, in this blog we’ll perform Classification using Inception V3 pre-trained model.

We’ll download the Inception V3 pre-trained model from here. And use the name of the class from this file.

You can find the detail about Inception V3 here. Architecture of the Inception V3 looks like below:

Inception V3 Architecture
Inception V3 Architecture of Single Layer

Our aim is to download the Inception V3 pre-trained model and use the model to identify the class of the image.

Download the two images from the internet using below code:

import requests
url_list = []

url_list.append(
‘https://kids.nationalgeographic.com/content/dam/kids/photos/games/screen-shots/More%20Games/A-G/babyanimal_open.ngsversion.1429194155981.jpg’)

url_list.append(
‘https://www.humanesociety.org/sites/default/files/styles/400×400/public/2018/04/magazine-hero.jpg?h=9c5a9055&itok=fD5FktH3’)
count = 0
for url in url_list:
print(url)
r = ‘ ‘
count = count + 1
print(count)
r = requests.get(url, allow_redirects=True)
filename = ‘Image’+str(count)+’.jpg’
print(filename)
open(filename, ‘wb’).write(r.content)

Now resize the image to size of 299 * 299 so that we can use it for testing purpose.

from PIL import Image

from resizeimage import resizeimage

with open(‘Image1.jpg’, ‘r+b’) as f:
with Image.open(f) as image:
cover = resizeimage.resize_cover(image, [299, 299])
cover.save(‘Image1c.jpg’, image.format)
with open(‘Image2.jpg’, ‘r+b’) as f:
with Image.open(f) as image:
cover = resizeimage.resize_cover(image, [299, 299])
cover.save(‘Image2c.jpg’, image.format)

Review the downloaded images before starting the download of the Inception V3 model

import matplotlib.image as mpimg
import matplotlib.pyplot as plt
from matplotlib.pyplot import figure as fig
image1 = mpimg.imread(‘Image1c.jpg’)[:, :, :3]
image2 = mpimg.imread(‘Image2c.jpg’)[:, :, :3]
# display images
fig, ax = plt.subplots(1,2)
fig.canvas.set_window_title(‘Test Images’)
ax[0].imshow(image1)
ax[0].set_axis_off()
ax[0].title.set_text(‘Image 1’)
ax[1].imshow(image2)
ax[1].set_axis_off()
ax[1].title.set_text(‘Image 2’)

Downloaded Images for testing
Downloaded Images for testing

Convert the value of Image to -1 to 1 range instead of 0 and 1. This is requirement for Inception model.

image1 = 2 * (image1 / 255 ) – 1
image2 = 2 * (image2 / 255 ) – 1

Create the Inception model by calling Inception_V3_arg_scope function

from tensorflow.contrib.slim.nets import inception
import tensorflow.contrib.slim as slim
tf.reset_default_graph()
X = tf.placeholder(tf.float32, shape=[None, 299, 299, 3], name=”X”)
with slim.arg_scope(inception.inception_v3_arg_scope()):
logits, end_points = inception.inception_v3(
X, num_classes=1001, is_training=False)
predictions = end_points[“Predictions”]
saver = tf.train.Saver()

Now open a session and use the Saver to restore the pre-trained model checkpoint downloaded above

with tf.Session() as sess:
saver.restore(sess, INCEPTION_V3_CHECKPOINT_PATH)

Now check the class of the first image by passing the value of Image1 as X_test.

X_test = image2.reshape(-1, 299, 299, 3)
with tf.Session() as sess:
saver.restore(sess, INCEPTION_V3_CHECKPOINT_PATH)
predictions_val = predictions.eval(feed_dict={X: X_test})

Read the class name of the detected probability.

class_index = np.argmax(predictions_val[0])
class_names[class_index]

Output: ‘chimpanzee, chimp, Pan troglodytes’

So model has predicted the correct value . Now lets try to predict the image 1.

X_test = image1.reshape(-1, 299, 299, 3)
with tf.Session() as sess:
saver.restore(sess, INCEPTION_V3_CHECKPOINT_PATH)
predictions_val = predictions.eval(feed_dict={X: X_test})

Check the class name of the predicted class index.

class_index = np.argmax(predictions_val[0])
class_names[class_index]

Output : ‘giant panda, panda, panda bear, coon bear, Ailuropoda melanoleuca’

In conclusion, this model has successfully detected the both images. You can find the entire code here.