AttributeError:“ str”对象没有属性“ shape”错误

时间:2019-10-08 06:55:08

标签: python tensorflow keras conv-neural-network

您好,当我尝试测试CNN模型时,我目前遇到此错误。

import cv2
import tensorflow as tf
CATEGORIES = ["cats","dogs"]
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 = "datasets/test_set/dogs/dog.4001.jpg" 
prediction = model.predict([image])
prediction = list(prediction[0])
print(CATEGORIES[prediction.index(max(prediction))])

运行代码时出现错误:

File "C:\Anaconda\lib\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'

谁能告诉我我的错误?

1 个答案:

答案 0 :(得分:1)

您忘记了调用prepare ...,因此,您的image变量应如下所示:

image = prepare("datasets/test_set/dogs/dog.4001.jpg")