TensorFlow Object Detection验证时触发AttributeError错误求助
解决TensorFlow Object Detection验证时的AttributeError问题
错误详情
运行验证脚本时触发如下错误:
AttributeError: module 'keras._tf_keras.keras.layers' has no attribute 'experimental'
验证代码:
VERIFICATION_SCRIPT = os.path.join(paths['APIMODEL_PATH'], 'research', 'object_detection', 'builders', 'model_builder_tf2_test.py') # Verify Installation !python {VERIFICATION_SCRIPT}
错误原因
你安装的Keras 3.1.1是独立版本,与当前TensorFlow 2.16.x、tf-models-official 2.5.1版本不兼容。Keras 3重构了模块结构,移除了keras.layers.experimental的旧有路径,导致检测脚本无法找到该属性。
解决方案
1. 卸载独立Keras,切换到TensorFlow自带的tf_keras
执行以下命令:
pip uninstall -y keras pip install tf-keras==2.16.0
(TensorFlow 2.16.1对应tf_keras 2.16.0为兼容版本)
2. 升级tf-models-official到兼容版本
旧版tf-models-official(2.5.1)与TensorFlow 2.16匹配度差,执行升级:
pip install --upgrade tf-models-official
若升级后出现其他依赖问题,可尝试指定精准匹配版本:
pip install tf-models-official==2.16.0
3. 重新运行验证脚本
完成上述操作后,再次执行你的验证代码即可。
内容的提问来源于stack exchange,提问作者Hugo Misael Barajas Hernández
相关产品推荐
相关产品推荐

