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

无法从keras.engine导入get_source_inputs的错误解决求助

解决ImportError: cannot import name 'get_source_inputs' from 'keras.engine'

问题场景

运行以下Python代码时触发ImportError:

import os
import cv2
import numpy as np
from matplotlib import pyplot as plt
from patchify import patchify
from PIL import Image
import segmentation_models as sm
from tensorflow.keras.metrics import MeanIoU

错误信息

ImportError: cannot import name 'get_source_inputs' from 'keras.engine' (C:\Users\xpapa\.conda\envs\TensorFlow\lib\site-packages\keras\engine\__init__.py)

完整报错回溯

2022-09-20 07:56:06.902214: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cudart64_110.dll'; dlerror: cudart64_110.dll not found
2022-09-20 07:56:06.902313: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
Traceback (most recent call last):
  File "C:\Users\xpapa\.conda\envs\TensorFlow\lib\site-packages\spyder_kernels\py3compat.py", line 356, in compat_exec
    exec(code, globals, locals)

  File "c:\users\xpapa\untitled2.py", line 38, in <module>
    import segmentation_models as sm

  File "C:\Users\xpapa\.conda\envs\TensorFlow\lib\site-packages\segmentation_models\__init__.py", line 5, in <module>
    from .unet import Unet

  File "C:\Users\xpapa\.conda\envs\TensorFlow\lib\site-packages\segmentation_models\unet\__init__.py", line 1, in <module>
    from .model import Unet

  File "C:\Users\xpapa\.conda\envs\TensorFlow\lib\site-packages\segmentation_models\unet\model.py", line 4, in <module>
    from ..backbones import get_backbone, get_feature_layers

  File "C:\Users\xpapa\.conda\envs\TensorFlow\lib\site-packages\segmentation_models\backbones\__init__.py", line 1, in <module>
    from classification_models import Classifiers

  File "C:\Users\xpapa\.conda\envs\TensorFlow\lib\site-packages\classification_models\__init__.py", line 3, in <module>
    from . import resnet as rn

  File "C:\Users\xpapa\.conda\envs\TensorFlow\lib\site-packages\classification_models\resnet\__init__.py", line 1, in <module>
    from .models import *

  File "C:\Users\xpapa\.conda\envs\TensorFlow\lib\site-packages\classification_models\resnet\models.py", line 1, in <module>
    from .builder import build_resnet

  File "C:\Users\xpapa\.conda\envs\TensorFlow\lib\site-packages\classification_models\resnet\builder.py", line 11, in <module>
    from keras.engine import get_source_inputs

ImportError: cannot import name 'get_source_inputs' from 'keras.engine' (C:\Users\xpapa\.conda\envs\TensorFlow\lib\site-packages\keras\engine\__init__.py)

解决方法

错误根源是classification_models库调用了旧版Keras的API,而新版TensorFlow/Keras中get_source_inputs的模块路径已变更,以下是三种可行解决方案:

  • 修改库源码:找到文件C:\Users\xpapa\.conda\envs\TensorFlow\lib\site-packages\classification_models\resnet\builder.py,将第11行的from keras.engine import get_source_inputs替换为from keras.utils.layer_utils import get_source_inputs,保存后重新运行代码。
  • 安装适配版库:执行命令安装兼容TensorFlow 2.x的分支版本:pip install git+https://github.com/qubvel/classification_models.git
  • 降级Keras版本:如果项目对Keras版本无特殊要求,执行命令降级到兼容版本:pip install keras==2.3.1

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.18 22:50:38