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

导入keras_vggface的VGGFace时提示No module named 'keras.engine.topology'错误

报错原因

keras_vggface官方pip包长期未更新,仅适配Keras 2.2及更早版本。旧版Keras的Layer基类、get_source_inputs工具均存放在keras.engine.topology模块下,而Keras 2.4+、TensorFlow内置的tf.keras已经将相关接口迁移到了keras.engine.base_layer模块,路径不匹配就会触发该报错,和安装环境无关。

解决方案

方法一:动态模块映射(即开即用,无需修改文件/降级依赖)

导入VGGFace前先执行以下代码,给旧模块路径做别名映射,适配新版Keras接口:

import keras.engine.base_layer
import sys
sys.modules['keras.engine.topology'] = keras.engine.base_layer

映射完成后再执行原有导入语句即可正常加载模型:

from keras_vggface.vggface import VGGFace

方法二:修改包源码(永久生效)

找到当前Python环境下安装的keras_vggface包目录,打开其中的vggface.py文件,将头部的导入语句:

from keras.engine.topology import get_source_inputs

替换为适配新版Keras的路径:

from keras.engine.base_layer import get_source_inputs

保存后重新运行代码即可。

方法三:降级依赖版本

如果不需要使用新版Keras特性,也可以直接将Keras降级到官方适配的版本:

pip install keras==2.2.4

该版本推荐搭配TensorFlow 1.15使用,避免其他兼容性问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.07 15:48:03