In [1]:
import numpy as np
from keras.preprocessing import image
from keras.applications import resnet50
C:\Users\rstancut\AppData\Local\Continuum\anaconda2\envs\keras\lib\site-packages\h5py\__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
  from ._conv import register_converters as _register_converters
Using TensorFlow backend.
In [2]:
from keras import backend as K
print(K.backend())
tensorflow
In [3]:
# load pre-trained model
model = resnet50.ResNet50()
WARNING:tensorflow:From C:\Users\rstancut\AppData\Local\Continuum\anaconda2\envs\keras\lib\site-packages\keras\backend\tensorflow_backend.py:1255: calling reduce_prod (from tensorflow.python.ops.math_ops) with keep_dims is deprecated and will be removed in a future version.
Instructions for updating:
keep_dims is deprecated, use keepdims instead
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)
Downloading data from https://s3.amazonaws.com/deep-learning-models/image-models/imagenet_class_index.json
40960/35363 [==================================] - 0s 4us/step
In [12]:
print("This is an image of:")

for imagenet_id, name, likelihood in predicted_classes[0]:
    print(" - {}: {:2f} likelihood".format(name, likelihood))
This is an image of:
 - lakeside: 0.372070 likelihood
 - seashore: 0.301635 likelihood
 - dock: 0.143978 likelihood
 - breakwater: 0.056647 likelihood
 - promontory: 0.042953 likelihood