You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Keras加载模型调用predict报'NoneType'无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)
问题根因

代码一共存在两处明确问题,另有一处潜在隐患:

  • 核心报错原因:Keras的load_weights()是原地加载权重的方法,没有返回值,代码中把该方法的返回值(固定为None)重新赋值给了loaded_model变量,导致后续调用predict()方法时,操作对象变成了None,触发属性不存在的报错。
  • 隐藏维度错误:代码中给测试数组增加batch维度时设置了axis=1,Keras模型要求输入的batch维度在第0位,该参数设置错误会导致模型加载正常后依然触发输入维度不匹配的报错。
  • 路径隐患:Windows本地路径直接写双引号包裹的字符串时,反斜杠会被识别为转义字符,路径中如果出现\t、\n这类字符组合时会触发文件找不到错误。
修复方案
  1. 去掉加载权重语句的赋值操作,直接调用loaded_model.load_weights("modell.h5")即可,不要把方法返回值重新赋给模型变量。
  2. 将np.expand_dims的axis参数改为0,匹配模型输入的维度要求。
  3. 本地路径加r前缀标记为原始字符串,避免反斜杠转义问题。

修复后的核心代码段如下:

# 加载模型结构
json_file=open('modell.json','r')
loaded_model_json=json_file.read()
json_file.close()
loaded_model=model_from_json(loaded_model_json)

# 加载权重 不要做赋值操作
loaded_model.load_weights("modell.h5")
print("Loaded model succesfully**")

# 标签定义部分保持不变即可

# 路径加r前缀标记为原始字符串
path=r"E:\AI and ML-pr\day14\images_for_test\AppleCedarRust1.jpg"
test_image=image.load_img(path,target_size=(128,128))
test_image=image.img_to_array(test_image)
# 维度扩展axis设为0,增加batch维度
test_image=np.expand_dims(test_image,axis=0)
result = loaded_model.predict(test_image)
print(result)
fresult=np.max(result)
label2=label[result.argmax()]
print(label2)

内容的提问来源于stack exchange,提问作者Dev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.01 02:24:15