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

Python中Keras库加载图片报AttributeError问题求助

解决Keras加载图片时的AttributeError问题

报错原因

Keras版本迭代后,load_img的模块位置做了调整:

  • 旧版Keras里的keras.preprocessing.image.load_img,在新版(尤其是Keras并入TensorFlow生态后)已经被移除;
  • 你尝试的tensorflow.keras.utils.load_img并非当前的正确导入路径。

解决方法

  • 使用TensorFlow整合版Keras时,用以下正确导入语句:

    from tensorflow.keras.preprocessing.image import load_img
    

    导入后直接调用load_img(图片路径, 其他参数)即可。

  • 使用独立安装的Keras(非TensorFlow捆绑版)时,先安装配套预处理包:

    pip install keras-preprocessing
    

    再用这个导入:

    from keras_preprocessing.image import load_img
    

另外要注意:别导入了正确模块,代码里却仍用旧路径调用函数,比如导入了tensorflow.keras.preprocessing.image.load_img,却写keras.preprocessing.image.load_img(),这种情况还是会报错。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.21 04:27:39