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

Windows下TensorFlow目标检测API运行报错:缺失official.legacy等模块

解决TensorFlow目标检测API模块找不到的问题

处理official.legacy缺失

  • 确认你克隆的TensorFlow Models仓库分支和TF版本匹配,TF 2.12对应r2.12分支,别用master分支(master是最新版,和旧TF版本不兼容)。重新克隆指定分支:
    git clone -b r2.12 https://github.com/tensorflow/models.git
    
  • 检查models/official目录下有没有legacy文件夹,没有的话就是分支不对或者克隆不完整,重新拉取该分支代码。

处理tensorflow.python.keras.layers.preprocessing缺失

  • TF 2.12里Keras预处理层已经移到keras.layers.preprocessing,不再是tensorflow.python.keras.layers.preprocessing。打开你的测试脚本,把导入语句:
    from tensorflow.python.keras.layers.preprocessing import ...
    
    改成:
    from keras.layers.preprocessing import ...
    
    或者:
    from tensorflow.keras.layers.preprocessing import ...
    
  • 如果是官方脚本出问题,还是因为Models分支和TF版本不匹配,回到第一步换r2.12分支。

额外验证步骤

  • 重新设置PYTHONPATH(Windows命令行):
    set PYTHONPATH=你的models目录路径;你的models\research目录路径;你的models\research\slim目录路径
    
    举个例子:
    set PYTHONPATH=C:\models;C:\models\research;C:\models\research\slim
    
  • 进入models/research目录,重新运行protoc编译:
    protoc object_detection/protos/*.proto --python_out=.
    
  • 再次安装对应依赖,确保版本适配:
    pip install -r models/research/object_detection/packages/tf2/requirements.txt
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.19 21:05:05