Keras predict_classes method returns “list index out of range” error

∥☆過路亽.° 提交于 2021-01-29 17:11:06

问题


I am new to CNN and machine learning in general and have been trying to follow Image Classification tutorial of TensorFlow.

Now, the Google Colab can be found here. I've followed the this official tutorial of TensorFlow. And I've changed it slightly so it saves the model as h5 instead of tf format so I can use the Keras' model.predict_classes.

Now, I have the model trained and the model reloaded from the saved model alright. But I'm repeatedly getting list index out of range error whenever I am trying to predict the image which I am doing so:

def predict():
  image = tf.io.read_file('target.jpeg')
  image = tf.image.decode_jpeg(image, channels=3)
  image = tf.image.resize(image, [224, 224])
  print(model.predict_classes(image)[0])

target.jpeg is one of the images I took from the flowers_photos dataset on which the model is trained.

The Traceback is:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 7, in predict
  File "/home/amitjoki/.local/lib/python3.6/site-packages/tensorflow/python/keras/engine/sequential.py", line 319, in predict_classes
    proba = self.predict(x, batch_size=batch_size, verbose=verbose)
  File "/home/amitjoki/.local/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 821, in predict
    use_multiprocessing=use_multiprocessing)
  File "/home/amitjoki/.local/lib/python3.6/site-packages/tensorflow/python/keras/engine/training_arrays.py", line 712, in predict
    callbacks=callbacks)
  File "/home/amitjoki/.local/lib/python3.6/site-packages/tensorflow/python/keras/engine/training_arrays.py", line 187, in model_iteration
    f = _make_execution_function(model, mode)
  File "/home/amitjoki/.local/lib/python3.6/site-packages/tensorflow/python/keras/engine/training_arrays.py", line 555, in _make_execution_function
    return model._make_execution_function(mode)
  File "/home/amitjoki/.local/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 2037, in _make_execution_function
    self._make_predict_function()
  File "/home/amitjoki/.local/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 2027, in _make_predict_function
    **kwargs)
  File "/home/amitjoki/.local/lib/python3.6/site-packages/tensorflow/python/keras/backend.py", line 3544, in function
    return EagerExecutionFunction(inputs, outputs, updates=updates, name=name)
  File "/home/amitjoki/.local/lib/python3.6/site-packages/tensorflow/python/keras/backend.py", line 3468, in __init__
    self.outputs[0] = array_ops.identity(self.outputs[0])
IndexError: list index out of range

I searched extensively but couldn't get any solution. It would be helpful if anyone can point me in the direction of getting this up and running.


回答1:


All the predict functions in Keras expect batch of inputs. Therefore, since you are doing prediction on one single image, you need to add an axis at the beginning of image tensor to represent the batch axis:

image = tf.expand_dims(image, axis=0)   # the shape would be (1, 224, 224, 3)
print(model.predict_classes(image)[0])


来源:https://stackoverflow.com/questions/57205032/keras-predict-classes-method-returns-list-index-out-of-range-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!