簡體   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