跟随TensorFlow教程时在模型预测阶段遇到问题

我在学习TensorFlow的一个教程,在模型预测阶段遇到了问题。

代码的最后部分是这样的:

import cv2import tensorflow as tfCATEGORIES = ["bishopB", "bishopW", "empty", "kingB", "kingW",            "knightB", "knightW", "pawnB", "pawnW",            "queenB", "queenW", "rookB", "rookW"]def prepare(file):    IMG_SIZE = 50    img_array = cv2.imread(file, cv2.IMREAD_GRAYSCALE)    new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE))    return new_array.reshape(-1, IMG_SIZE, IMG_SIZE, 1)model = tf.keras.models.load_model("CNN.model")image = "test.jpg" #your image pathprediction = model.predict([image])prediction = list(prediction[0])print(CATEGORIES[prediction.index(max(prediction))])

这应该可以让我根据文件输入得到预测结果。

然而,当我运行时,出现了以下错误:

    prediction = model.predict([image])  File "/Users/stuff/Library/Python/2.7/lib/python/site-packages/tensorflow/python/keras/engine/training.py", line 1060, in predict    x, check_steps=True, steps_name='steps', steps=steps)  File "/Users/stuff/Library/Python/2.7/lib/python/site-packages/tensorflow/python/keras/engine/training.py", line 2651, in _standardize_user_data    exception_prefix='input')  File "/Users/stuff/Library/Python/2.7/lib/python/site-packages/tensorflow/python/keras/engine/training_utils.py", line 334, in standardize_input_data    standardize_single_array(x, shape) for (x, shape) in zip(data, shapes)  File "/Users/stuff/Library/Python/2.7/lib/python/site-packages/tensorflow/python/keras/engine/training_utils.py", line 265, in standardize_single_array    if (x.shape is not None and len(x.shape) == 1 andAttributeError: 'str' object has no attribute 'shape'

有谁能帮我理解我在这里做错了什么吗?我认为它甚至没有开始处理我的测试图像。


回答:

你的 image 应该使用 prepare_file(img_path) 而不是直接使用字符串。

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注