Image Recognition using Pre-trained VGG16 model in Keras

Home » News » Image Recognition using Pre-trained VGG16 model in Keras
Lets use a pre-trained VGG16 model to predict an image from ImageNet database. We will load an image, convert that image to numpy array, preprocess that array and let the pre-trained VGG16 model predict the image.


VGG16 is a CNN model. To know more about CNN, you can visit my this post. We are not fine-tuning the VGG16 model here. We are using it as it is. To fine-tune the existing VGG16 model, you can visit my this post.


You can download my Jupyter notebook containing following code from here.

Step 1: Import required libraries

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



Step 2: Load pre-trained weights from VGG16 model for ImageNet dataset

model = vgg16.VGG16(weights='imagenet')


Step 3: Load image to predict


img = image.load_img(‘cat.jpg', target_size=(224, 224))
img













Please note that we need to reshape the image to 224X224 as it is a requirement for VGG16 model. You can download this image from ImageNet official website.

Step 4: Convert the image into numpy array

arr = image.img_to_array(img)
arr.shape



(224, 224, 3)


Step 5: Expand the array dimension


arr = np.expand_dims(arr, axis=0)
arr.shape



(1, 224, 224, 3)


Step 6: Preprocess the array


arr = vgg16.preprocess_input(arr)
arr



Step 7: Predict from the model

predictions = model.predict(arr)

predictions

We get an array as an output which is hard to understand. So, lets simplify it and see top 5 predictions made by the VGG16 model.

vgg16.decode_predictions(predictions, top=5)
[[(‘n02123045', ‘tabby', 0.7138179),
  (‘n02123159', ‘tiger_cat', 0.21695374),
  (‘n02124075', ‘Egyptian_cat', 0.043560617),
  (‘n04040759', ‘radiator', 0.0053847637),
  (‘n04553703', ‘washbasin', 0.0024860944)]]
So, as per VGG16 model prediction, the given image may be a tabby (71%) or a tiger cat (21%). You can try the same with different images from ImageNet database and check your results.

Leave a Reply

Your email address will not be published. Required fields are marked *

New Providers
Quotex

1000+ Trading Instruments
The best trading broker in the world.

Stockity

1000+ Trading Instruments
The best trading broker in the world.

New Games
Lies of P

$59.99 Standard Edition
28% Save Discounts
See Top 10 Provider Games

COCOON

$24.99 Standard Edition
28% Save Discounts
See Top 10 Provider Games

New Offers
Commission up to $1850 for active user of affiliate program By Exness

Top Points © Copyright 2023 | By Topoin.com Media LLC.
Topoin.info is a site for reviewing the best and most trusted products, bonus, offers, business service providers and companies of all time.

Discover more from Topoin

Subscribe now to keep reading and get access to the full archive.

Continue reading