TensorFlow 1.2.1 and InceptionV3 to classify an image

Viewed 435

I'm trying to create an example using the Keras built in the latest version of TensorFlow from Google. This example should be able to classify a classic image of an elephant. The code looks like this:

# Import a few libraries for use later
from PIL import Image as IMG

from tensorflow.contrib.keras.python.keras.preprocessing import image
from tensorflow.contrib.keras.python.keras.applications.inception_v3 import InceptionV3
from tensorflow.contrib.keras.python.keras.applications.inception_v3 import preprocess_input, decode_predictions


# Get a copy of the Inception model
print('Loading Inception V3...\n')
model = InceptionV3(weights='imagenet', include_top=True)
print ('Inception V3 loaded\n')

# Read the elephant JPG
elephant_img = IMG.open('elephant.jpg')

# Convert the elephant to an array
elephant = image.img_to_array(elephant_img)
elephant = preprocess_input(elephant)

elephant_preds = model.predict(elephant)

print ('Predictions: ', decode_predictions(elephant_preds))

Unfortunately I'm getting an error when trying to evaluate the model with model.predict:

ValueError: Error when checking : expected input_1 to have 4 dimensions, but got array with shape (299, 299, 3)

This code is taken from and based on the excellent example coremltools-keras-inception and will be expanded more when it is figured out.

2 Answers
Related