繁体   English   中英

我收到此错误 --> AttributeError: 'NoneType' object has no attribute 'predict'

[英]I get this error --> AttributeError: 'NoneType' object has no attribute 'predict'

我多次收到此错误。

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)

请解决我的问题

问题似乎是这一行: loaded_model = loaded_model.load_weights("modell.h5")

load_weights() 的文档中

加载 TensorFlow 格式的权重文件时,返回与 tf.train.Checkpoint.restore 相同的状态对象。 在构建图形时,一旦网络构建完成,恢复操作就会自动运行(在第一次调用从模型继承的用户定义类时,如果它已经构建,则立即运行)。

以 HDF5 格式加载权重时,返回 None。

因此,看起来就像将行更改为: loaded_model.load_weights("modell.h5")可以解决您的问题。

暂无
暂无

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

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