TensorFlow 1.2.1 and InceptionV3 for image classification

I am trying to create an example using Keras built in the latest version of TensorFlow from Google. This example should be able to classify the classic elephant image. 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 am getting an error when I try to evaluate a model using 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 the excellent coremltools-keras-inception example and will expand more when clarified.

+3


source to share


2 answers


The reason this error was thrown is because the model is always expecting a series example , not a single one . This is at odds with the general understanding of models as mathematical functions of their inputs. Reasons why the model is waiting for the batch:

  • Models are computational tools designed to work faster with batches to speed up learning.
  • There are algorithms that take into account the batch nature of the input (for example, Batch Normalization or GAN .


So four dimensions come from the first dimension, which is the sample / batch size , and then the next 3 dimensions are thumbnails.

+3


source


I actually found the answer. Although the documentation states that if the top layer is on, the shape of the input vector is still set to batch image processing. So we have to add this before the prediction code line:

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

      



Then the tensor is in the correct shape and everything works correctly. I'm still not sure why the documentation states that the input vector should be (3x299x299) or (299x299x3) when it explicitly wants 4 dimensions.

Be careful!

+2


source







All Articles