Project - Image Classification with Pre-trained Keras models

6 / 6

Predicting the Class using Pre-Trained model

Now that we have loaded the image and the pre-trained network, we shall preprocess the image, feed it to the neural network to predict the class of the image.

Note::

  • image.img_to_array : Converts a PIL Image instance to a Numpy array.

  • tensorflow.keras.applications.resnet50.preprocess_input : The images are converted from RGB to BGR, then each color channel is zero-centered with respect to the ImageNet dataset, without scaling.

  • tensorflow.keras.applications.resnet50.decode_predictions : Decodes the prediction into a list of tuples (class, description, probability)

INSTRUCTIONS
  • Converts the PIL image img to np array x using image.img_to_array function.

    x = << your code comes here >>(img)
    
  • The model expects to be fed with an array of images. So write as follows:

    x= np.array([x])
    
  • Preprocess the x using preprocess_input.

    x =  << your code comes here >>(x)
    
  • Predict the class probabilities of the input image x using model.predict.

    preds = << your code comes here>>(x)
    
  • Decode the predictions and print the top 3 prediction probabilities as follow:

    print('Predicted:', decode_predictions(preds, top=3)[0])
    

    We could see that the image has been correctly predicted as a lion with as high as 0.9998623 probability. This is a spectacular work!

Get Hint See Answer


Note - Having trouble with the assessment engine? Follow the steps listed here

Loading comments...