繁体   English   中英

“AttributeError:‘str’object 没有属性‘predict’”。 我正在开发垃圾检测器 model,这是错误

[英]"AttributeError: 'str' object has no attribute 'predict'". I am developing garbage detector model and this is the error

from tensorflow import keras
from keras.models import load_model
from PIL import Image, ImageOps
import numpy as np

model = './ C:/Parth/cs11/converted_keras/saved_model.h5'


data = np.ndarray(shape=(1, 224, 224, 3), dtype=np.float32)

image = Image.open('C:/cs11/pic.jpg')

size = (224, 224)
image = ImageOps.fit(image, size, Image.ANTIALIAS)

image_array = np.asarray(image)

normalized_image_array = (image_array.astype(np.float32) / 127.0) - 1

data[0] = normalized_image_array

prediction = model.predict(data)
print(prediction)

我收到的错误是

  File "cs11.py", line 22, in <module>
    prediction = model.predict('data')
AttributeError: 'str' object has no attribute 'predict'

即使数据是np.ndarray. 有人可以告诉我解决这个问题的方法吗? 或者它的替代方法?

您指定了 model 的路径。 现在您必须加载 model

model_path=r'./ C:/Parth/cs11/converted_keras/saved_model.h5'
model = keras.models.load_model(model_path)

现在做预测。 您可以检查 model 是否正确加载

print (model.summary())

这应该打印出 model 摘要

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM