python - 遵循 TensorFlow 教程并遇到 model.predict 问题

标签 python tensorflow keras artificial-intelligence

I am following a tutorial for TensorFlow我在模型预测阶段遇到了问题。

最后一段代码是:

import cv2
import tensorflow as tf
CATEGORIES = ["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 path
prediction = 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 and
AttributeError: 'str' object has no attribute 'shape'

有人可以帮我理解我在这里做错了什么吗?我不相信它甚至没有达到处理我的测试图像的程度。

最佳答案

您的image应该是prepare_file(img_path)而不仅仅是一个字符串。

关于python - 遵循 TensorFlow 教程并遇到 model.predict 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56895221/

相关文章:

Keras混合模型

python - 在 Keras 中对句子的词向量进行平均 - 预训练词嵌入

python - 以预编译的字节码 + 所有必需的库分发 python 脚本

java - DynamicPartition 返回单个输出而不是多个

python - Gstreamer 在 PLAYING 状态下更改源

python - 如何删除 tensorflow 中的连续重复项?

python - 训练im2txt模型时出错

python - 具有附加输入数据的 tensorflow 自定义损失函数

Python 递归 no 'in' 解决方法

Python - 绘制天线辐射模式