In [1]:
import numpy as np
from keras.preprocessing import image
from keras.applications import resnet50
In [2]:
from keras import backend as K
print(K.backend())
In [3]:
# load pre-trained model
model = resnet50.ResNet50()
In [4]:
# load and resixe image to match model nodes
img = image.load_img("Exercise Files/05/bay.jpg", target_size=(224, 224))
In [5]:
# convert image to np array
x = image.img_to_array(img)
In [6]:
# the model expects multiple images, a list
x = np.expand_dims(x, axis=0)
In [7]:
# scale input image to range used in trained NN
x = resnet50.preprocess_input(x)
In [8]:
# run image through NN to make a predict
predictions = model.predict(x)
In [11]:
# look up predicted class names
# returns top 5
# add ", top=n" for less/more results in `decode_predictions` arguments
predicted_classes = resnet50.decode_predictions(predictions)
In [12]:
print("This is an image of:")
for imagenet_id, name, likelihood in predicted_classes[0]:
print(" - {}: {:2f} likelihood".format(name, likelihood))
In [ ]: