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

TensorFlow 2.15目标检测环境报错:AttributeError问题求助

解决AttributeError: module 'keras._tf_keras.keras.layers' has no attribute 'experimental'

问题本质

TensorFlow 2.15整合Keras后,keras.layers.experimental的API路径发生变化,而你使用的tensorflow/models仓库代码仍依赖旧路径,导致属性查找失败。

分步修复方案

1. 对齐tensorflow/models版本与TF2.15

确保models仓库分支与TensorFlow 2.15兼容:

cd tensorflow/models
git checkout r2.15

2. 修正代码中的API引用

找到报错代码里调用keras._tf_keras.keras.layers.experimental的地方,替换为tf.keras.layers.experimental:

  • 若使用SyncBatchNormalization,直接写tf.keras.layers.experimental.SyncBatchNormalization
  • 部分原experimental API已转正(如Resizing),可直接用tf.keras.layers.Resizing替代

3. 锁定依赖版本,避免Keras冲突

卸载独立Keras包,安装TF2.15对应版本的依赖:

pip uninstall -y keras
pip install tensorflow==2.15.0 keras==2.15.0

4. 重新执行验证脚本

完成上述操作后,重新运行测试:

python model_builder_tf2_test.py

额外排查建议

  • 用虚拟环境隔离依赖,避免多版本冲突:
    python -m venv tfod_env
    # Windows激活环境
    tfod_env\Scripts\activate
    # Linux/macOS激活环境
    source tfod_env/bin/activate
    # 安装指定版本依赖
    pip install tensorflow==2.15.0 keras==2.15.0 numpy==1.24.3 pillow==10.1.0
    
  • 确保models的research目录已正确安装:
    cd tensorflow/models/research
    protoc object_detection/protos/*.proto --python_out=.
    cp object_detection/packages/tf2/setup.py .
    python -m pip install .
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.27 12:50:00