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.

import numpy as np

from keras.applications import vgg16

from keras.preprocessing import image

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

img = image

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.

arr = image.

arr.shape

(224, 224, 3)

arr = np.

arr.shape

(1, 224, 224, 3)

arr

predictions = model.

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)]]

('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.