【问题标题】:Keras predict_classes method returns "list index out of range" errorKeras predict_classes 方法返回“列表索引超出范围”错误
【发布时间】:2019-12-03 21:54:31
【问题描述】:

我是 CNN 和机器学习的新手,一直在尝试学习 TensorFlow 的图像分类教程。

现在,您可以在 here 找到 Google Colab。我关注了 TensorFlow 的 this official tutorial。我稍微改变了它,所以它把模型保存为h5而不是tf格式,这样我就可以使用Keras的model.predict_classes

现在,我已经对模型进行了训练,并从保存的模型中重新加载了模型。但是每当我试图预测我正在这样做的图像时,我都会反复收到list index out of range 错误:

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 是我从训练模型的flowers_photos 数据集中拍摄的图像之一。

回溯是:

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

我进行了广泛搜索,但找不到任何解决方案。如果有人能指出我的启动和运行方向,那将会很有帮助。

【问题讨论】:

    标签: python tensorflow machine-learning keras conv-neural-network


    【解决方案1】:

    Keras 中的所有预测函数都需要一批输入。因此,由于您是对单个图像进行预测,因此您需要在图像张量的开头添加一个轴来表示批处理轴:

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

    【讨论】:

    • 谢谢!愚蠢的我。我在任何文档中都没有遇到过这个!所以如果我有多个图像,我不需要做expand_dims?
    • @AmitJoki 没错,在这种情况下您不需要使用expand_dims,因为它们的形状为(NUM_IMAGES, IMAGE_SIZE...),因此批处理轴已经存在。
    • 谢谢。这个信息很难找到。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-17
    • 1970-01-01
    • 1970-01-01
    • 2017-10-30
    • 1970-01-01
    相关资源
    最近更新 更多