Keras Forecast_classes方法返回“列表索引超出范围",并且返回“列表索引超出范围".错误 [英] Keras predict_classes method returns "list index out of range" error

查看:98
本文介绍了Keras Forecast_classes方法返回“列表索引超出范围",并且返回“列表索引超出范围".错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

现在,可以在此处找到Google Colab.我遵循了TensorFlow的本官方教程 .并且我对其进行了少许更改,因此将模型另存为h5而不是tf格式,因此我可以使用Keras的model.predict_classes.

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.

现在,我已经对模型进行了训练,可以从保存的模型中重新加载模型.但是,每当我尝试预测正在执行的图像时,我都会反复出现list index out of range错误:

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

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

回溯是:

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.

推荐答案

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

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])

这篇关于Keras Forecast_classes方法返回“列表索引超出范围",并且返回“列表索引超出范围".错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆