0

I am trying to use the Keras 2 incepctionV3 based trained model to predict an image for testing purpose. My original model work well, then I try to create a model with specified input_shape (299,299,3)

base_model = InceptionV3(weights='imagenet', include_top=False, input_shape=(299,299,3)) 

The training process looks fine but when I try to use it to predict the image it causes this error.

ValueError: Error when checking : expected input_1 to have shape (None, 299, 299, 3) but got array with shape (1, 229, 229, 3)

    import sys
    import argparse
    import numpy as np
    from PIL import Image
    from io import BytesIO

    from keras.preprocessing import image
    from keras.models import load_model
    from keras.applications.inception_v3 import preprocess_input


    target_size = (229, 229) #fixed size for InceptionV3 architecture


    def predict(model, img, target_size):
      """Run model prediction on image
      Args:
        model: keras model
        img: PIL format image
        target_size: (w,h) tuple
      Returns:
        list of predicted labels and their probabilities
      """
      if img.size != target_size:
        img = img.resize(target_size)

      x = image.img_to_array(img)
      print(x.shape)
      print("model input",model.inputs)
      print("model output",model.outputs)
      x = np.expand_dims(x, axis=0)
      #x = x[None,:,:,:]
      print(x.shape)
      x = preprocess_input(x)

      print(x.shape)
      preds = model.predict(x)
      print('Predicted:',preds)
      return preds[0]

Here is the print out

(229, 229, 3)  
('model input', [<tf.Tensor 'input_1:0' shape=(?, 299, 299, 3) dtype=float32>])  
('model output', [<tf.Tensor 'dense_2/Softmax:0' shape=(?, 5) dtype=float32>]) 
(1, 229, 229, 3) 
(1, 229, 229, 3)

(1,299,299,3) mean 1 image in 299 X 299 with 3 channel. What is the expected input of my trained model (None,299,299,3) meaning in this case? How can I create a (None,299,299,3) from (299,299,3)?

Kuroro
  • 1,841
  • 1
  • 19
  • 32

2 Answers2

2

here the problem is image size, set the require size to 299, 299

target_size = (299, 299) #fixed size for InceptionV3 architecture
Ishant Mrinal
  • 4,898
  • 3
  • 29
  • 47
1

You should use

preds = model.predict(x, batch_size=1)

batch_size=32 by default. And you have only one image to predict on. (None,299,299,3) meaning in this case that model.predict expects array with shape (n,299,299,3) with n > batch_size to process it batch by batch and return (n, outputs_dim) dimensional array of predictions.

mih.net
  • 61
  • 5