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

TensorFlow Object Detection训练报错:__new__()意外参数'file'

解决TensorFlow Object Detection训练时TypeError: __new__() got an unexpected keyword argument 'file'报错

这个报错我之前帮朋友排查过,核心原因就是protobuf版本不兼容,下面给你具体的分析和解决步骤:

报错原因

TensorFlow Object Detection API对Protocol Buffers(protobuf)的版本有严格要求。当你环境中的protobuf版本过高(比如3.10及以上),生成的.proto对应的Python代码(比如你报错里的preprocessor_pb2.py)会使用新版本的API,而Object Detection的核心代码还是沿用旧版本的参数调用逻辑(比如file这个参数在新版本protobuf中已经被移除或修改),就会触发这个类型错误。

解决步骤

1. 检查当前protobuf版本

先确认你环境里的protobuf版本:

pip show protobuf

如果版本是3.9.0及以上,大概率就是版本问题。

2. 安装兼容的protobuf版本

卸载当前版本,安装适配TF Object Detection API的版本(推荐3.8.0,这个版本和绝大多数TF1.x版本的Object Detection API兼容):

pip uninstall -y protobuf
pip install protobuf==3.8.0

3. 重新编译protobuf文件

因为之前生成的*_pb2.py文件是用不兼容的protobuf版本生成的,需要重新编译:
进入models/research目录,执行以下命令:

protoc object_detection/protos/*.proto --python_out=.

4. 重新启动训练

再次运行你的训练命令:

python train.py --logtostderr --train_dir=training/ --pipeline_config_path=training/ssd_mobilenet_v1_pets.config

这样应该就能解决这个报错,正常输出训练步数和准确率了。

内容的提问来源于stack exchange,提问作者bogdan.georgescu.gbm

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 03:45:01