Python 遵循TensorFlow教程并使用model.predict解决问题
我在模型预测阶段遇到了问题 代码的最后一位是:Python 遵循TensorFlow教程并使用model.predict解决问题,python,tensorflow,keras,artificial-intelligence,Python,Tensorflow,Keras,Artificial Intelligence,我在模型预测阶段遇到了问题 代码的最后一位是: import cv2 import tensorflow as tf CATEGORIES = ["bishopB", "bishopW", "empty", "kingB", "kingW", "knightB", "knightW", "pawnB", "pawnW", "queenB", "queenW", "rookB", "rookW"] def prepare(file): IMG
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'
谁能帮我理解我做错了什么?我甚至不相信它会处理我的测试图像。你的
图像应该是prepare\u file(img\u path)