0

I created a classification model with both saved_model format and .h5 format. I am trying to load the model so I can deploy it with

new_model = tf.keras.models.load_model('my_model.h5')

Then I predict

print(new_model.predict('/content/images/image.jpg'))

Then it returns

> IndexError                                Traceback (most recent call last)
<ipython-input-26-749bd8c0774b> in <module>()
      1 new_model = tf.keras.models.load_model('my_model.h5')
----> 2 print(new_model.predict('/content/images/image.jpg'))

>5 frames
>/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/tensor_shape.py in __getitem__(self, key)
    887       else:
    888         if self._v2_behavior:
--> 889           return self._dims[key].value
    890         else:
    891           return self._dims[key]

>IndexError: list index out of range

I've tried other similar solutions but they don't work. Do I need to retrain the model? What do I do so I can predict on one image in a clean environment?

Samay Lakhani
  • 85
  • 1
  • 1
  • 7
  • Method [predict](https://www.tensorflow.org/api_docs/python/tf/keras/Model#predict) does not work with image files. You should open file and make suitable pre-processing, then call a `predict` method. – Alex K. Jan 07 '21 at 15:58
  • How would I preprocess? What would the goal of it be? Can you point me to a source that shows how to do so? – Samay Lakhani Jan 07 '21 at 16:04
  • You have made some pre-processing for images during training, like resize, padding, etc. You should [read data](https://stackoverflow.com/questions/7762948/how-to-convert-an-rgb-image-to-numpy-array) from your image, perform resize/reshape/padding/ all you have done during training, then feed data to `predict` method. Without details about your training process I can not provide detailed answer. – Alex K. Jan 07 '21 at 16:33

1 Answers1

0

for model.predict to produce proper predictions it is necessary that the input be of the same nature as the inputs that the model was trained on. For example in training you read in an image from the training set. Then typically you will rescale the pixel values, usually in the range from 0 to +1 or in some cases -1 to +1. Then you typically resize the images so all training images are of the same size. Now when you want to input an image to be predicted you should follow the same process. Read in the image, rescale it and resize it as you did for the training images.

Gerry P
  • 7,662
  • 3
  • 10
  • 20