我多次收到此錯誤。
Traceback (most recent call last):
File "E:\AI and ML-pr\day14\test.py", line 38, in <module>
result = loaded_model.predict(test_image)
AttributeError: 'NoneType' object has no attribute 'predict'
我想找出植物葉子的病害。
我的代碼::
# importing libraries
import numpy as np
from keras.preprocessing import image
from keras.models import Sequential
from keras.layers.core import Dense
from keras.models import model_from_json
import os
import cv2
#loading tha model
json_file=open('modell.json','r')
loaded_model_json=json_file.read()
json_file.close()
loaded_model=model_from_json(loaded_model_json)
# load weights into new model
loaded_model = loaded_model.load_weights("modell.h5")
print("Loaded model succesfully**")
label = ['Apple___Apple_scab','Apple___Black_rot','Apple___Cedar_apple_rust','Apple___healthy',
'Blueberry___healthy','Cherry_(including_sour)___healthy','Cherry_(including_sour)___Powdery_mildew','Corn_(maize)___Cercospora_leaf_spot Gray_leaf_spot','Corn_(maize)___Common_rust_', 'Corn_(maize)___healthy','Corn_(maize)___Northern_Leaf_Blight','Grape___Black_rot','Grape___Esca_(Black_Measles)','Grape___healthy','Grape___Leaf_blight_(Isariopsis_Leaf_Spot)','Orange___Haunglongbing_(Citrus_greening)','Peach___Bacterial_spot','Peach___healthy','Pepper,_bell___Bacterial_spot','Pepper,_bell___healthy','Potato___Early_blight','Potato___healthy','Potato___Late_blight','Raspberry___healthy','Soybean___healthy','Squash___Powdery_mildew','Strawberry___healthy','Strawberry___Leaf_scorch','Tomato___Bacterial_spot','Tomato___Early_blight','Tomato___healthy','Tomato___Late_blight','Tomato___Leaf_Mold','Tomato___Septoria_leaf_spot','Tomato___Spider_mites Two-spotted_spider_mite','Tomato___Target_Spot','Tomato___Tomato_mosaic_virus','Tomato___Tomato_Yellow_Leaf_Curl_Virus']
path="E:\AI and ML-pr\day14\images_for_test\AppleCedarRust1.jpg"
test_image=image.load_img(path,target_size=(128,128))
#print(test_image)
test_image=image.img_to_array(test_image)
test_image=np.expand_dims(test_image,axis=1)
result = loaded_model.predict(test_image)
print(result)
fresult=np.max(result)
label2=label[result.argmax()]
print(label2)
請解決我的問題
uj5u.com熱心網友回復:
問題似乎是這條線:
loaded_model = loaded_model.load_weights("modell.h5")
從load_weights() 的檔案中:
加載 TensorFlow 格式的權重檔案時,回傳與 tf.train.Checkpoint.restore 相同的狀態物件。在構建圖形時,一旦網路構建完成,恢復操作就會自動運行(在第一次呼叫從模型繼承的用戶定義類時,如果它已經構建,則立即運行)。
以 HDF5 格式加載權重時,回傳 None。
因此,看起來將行更改為 just:
loaded_model.load_weights("modell.h5")
會解決您的問題。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/487612.html
