Using Pretrained models for prediction
Using Pretrained models for prediction
So today, I will tell you how to use pre-trained models.
For your existing images, you must follow specific steps.
step one
load the model
step two,
load image to be predicted. Step three, resize it. We resize
since models were trained, using a specific size of pictures, and our target
images must be resized before prediction.
Step Four
convert to NumPy array. We are doing this because images can
be all in different formats. This can raise specific issues. So converting it
to a form that Python can understand is a good idea for all images. Now we can
predict after prediction, and we have to use the code predictions and find out the
top few predictions. Now show to the user
Code:
from tensorflow.keras.applications.resnet50 import ResNet50
from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications.resnet50 import preprocess_input, decode_predictions
import numpy as np
model = ResNet50(weights='imagenet')
img_path = 'elephant.jpg'
img = image.load_img(img_path, target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
preds = model.predict(x)
# decode the results into a list of tuples (class, description, probability)
# (one such list for each sample in the batch)
print('Predicted:', decode_predictions(preds, top=3)[0])
# Predicted: [(u'n02504013', u'Indian_elephant', 0.82658225), (u'n01871265', u'tusker', 0.1122357), (u'n02504458', u'African_elephant', 0.061040461)]
Comments
Post a Comment