无法从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
相关产品推荐
相关产品推荐

