Tom T
Tom T

Reputation: 51

TensorFlow 1.2.1 and InceptionV3 to classify an image

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.

Upvotes: 3

Views: 494

Answers (2)

Tom T
Tom T

Reputation: 51

Actually I found the answer. Even though the documentation states that if the top layer is included the shape of the input vector is still set to take a batch of images. Thus we need to add this before the code line for the prediction:

elephant = numpy.expand_dims(elephant, axis=0)

Then the tensor is in the right shape and everything works correctly. I am still uncertain why the documentation states that the input vector should be (3x299x299) or (299x299x3) when it clearly wants 4 dimensions.

Be careful!

Upvotes: 2

Marcin Możejko
Marcin Możejko

Reputation: 40506

The reason why this error occured is that model always expects the batch of examples - not a single example. This diverge from a common understanding of models as mathematical functions of their inputs. The reasons why model expects batches are:

  1. Models are computationaly designed to work faster on batches in order to speed up training.
  2. There are algorithms which takes into account the batch nature of input (e.g. Batch Normalization or GAN training tricks).

So four dimensions comes from a first dimension which is a sample / batch dimension and then - the next 3 dimensions are image dims.

Upvotes: 3

Related Questions