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

TensorFlow目标检测报错:无法从object_detection导入model_lib_v2

无法从object_detection导入model_lib_v2的ImportError问题

环境配置

  • TensorFlow == 2.10.0
  • Python 3.10.9
  • 使用Jupyter Notebook,未启用TensorFlow GPU
  • 模型:ssd_mobilenet_v2_fpnlite_320x320_coco17_tpu-8(TF2 20200711版本)

错误详情

执行以下训练命令时触发ImportError:

(odball) C:\Users\someone\Desktop\website\2023\TensorFlow Object Detection\TFODBall>python Tensorflow\models\research\object_detection\model_main_tf2.py --model_dir=Tensorflow\workspace\models\my_ssd_mobnet --pipeline_config_path=Tensorflow\workspace\models\my_ssd_mobnet\pipeline.config --num_train_steps=4000

错误日志:

Traceback (most recent call last):
  File "C:\Users\someone\Desktop\website\2023\TensorFlow Object Detection\TFODBall\Tensorflow\models\research\object_detection\model_main_tf2.py", line 31, in <module>
    from object_detection import model_lib_v2
ImportError: cannot import name 'model_lib_v2' from 'object_detection' (C:\Users\someone\Desktop\website\2023\TensorFlow Object Detection\TFODBall\odball\lib\site-packages\object_detection\__init__.py)

IDE提示无法解析object_detection导入。

已尝试的解决方法

  • pip install tensorflow-object-detection-api
  • 卸载并重装TensorFlow
  • 重启Jupyter内核

当前环境pip包列表

PackageVersionEditable project location
absl-py2.1.0
anyio4.3.0
apache-beam2.54.0
argon2-cffi23.1.0
argon2-cffi-bindings21.2.0
arrow1.3.0
asttokens2.4.1
astunparse1.6.3
async-lru2.0.4
attrs23.2.0
avro-python31.10.2
Babel2.14.0
beautifulsoup44.12.3
bleach6.1.0
cachetools5.3.3
certifi2024.2.2
cffi1.16.0
charset-normalizer3.3.2
colorama0.4.6
comm0.2.1
contextlib221.6.0
contourpy1.2.0
cycler0.12.1
Cython3.0.8
debugpy1.8.1
decorator5.1.1
defusedxml0.7.1
dm-tree0.1.8
docutils0.20.1
exceptiongroup1.2.0
executing2.0.1
fastjsonschema2.19.1
flatbuffers23.5.26
fonttools4.49.0
fqdn1.5.1
gast0.4.0
google-auth2.28.1
google-auth-oauthlib0.4.6
google-pasta0.2.0
grpcio1.62.0
h110.14.0
h5py3.10.0
httpcore1.0.4
httpx0.27.0
idna3.6
importlib-metadata7.0.1
ipykernel6.29.3
ipython8.22.1
ipywidgets8.1.2
isoduration20.11.0
jaraco.classes3.3.1
jedi0.19.1
Jinja23.1.3
json50.9.17
jsonpointer2.4
jsonschema4.21.1
jsonschema-specifications2023.12.1
jupyter1.0.0
jupyter_client8.6.0
jupyter-console6.6.3
jupyter_core5.7.1
jupyter-events0.9.0
jupyter-lsp2.2.3
jupyter_server2.12.5
jupyter_server_terminals0.5.2
jupyterlab4.1.2
jupyterlab_pygments0.3.0
jupyterlab_server2.25.3
jupyterlab_widgets3.0.10
keras2.10.0
Keras-Preprocessing1.1.2
keyring24.3.1
kiwisolver1.4.5
libclang16.0.6
lvis0.5.3
lxml5.1.0
Markdown3.5.2
markdown-it-py3.0.0
MarkupSafe2.1.5
matplotlib3.8.3
matplotlib-inline0.1.6
mdurl0.1.2
mistune3.0.2
ml-dtypes0.3.2
more-itertools10.2.0
namex0.0.7
nbclient0.9.0
nbconvert7.16.1
nbformat5.9.2
nest-asyncio1.6.0
nh30.2.15
notebook7.1.1
notebook_shim0.2.4
numpy1.26.4
oauthlib3.2.2
object-detection0.1
opencv-python4.9.0.80
opt-einsum3.3.0
overrides7.7.0
packaging23.2
pandas2.2.1
pandocfilters1.5.1
parso0.8.3
pillow10.2.0
pip24.0
pkginfo1.9.6
platformdirs4.2.0
portalocker2.8.2
prometheus_client0.20.0
prompt-toolkit3.0.43
protobuf3.19.6
psutil5.9.8
pure-eval0.2.2
pyasn10.5.1
pyasn1-modules0.3.0
pycocotools2.0.7
pycparser2.21
Pygments2.17.2
pyparsing2.4.7
PyQt55.15.10
PyQt5-Qt55.15.2
PyQt5-sip12.13.0
python-dateutil2.8.2
python-json-logger2.0.7
pytz2024.1
pywin32306
pywin32-ctypes0.2.2
pywinpty2.0.13
PyYAML6.0.1
pyzmq25.1.2
qtconsole5.5.1
QtPy2.4.1
readme_renderer43.0
referencing0.33.0
regex2023.12.25
requests2.31.0
requests-oauthlib1.3.1
requests-toolbelt1.0.0
rfc3339-validator0.1.4
rfc39862.0.0
rfc3986-validator0.1.1
rich13.7.0
rpds-py0.18.0
rsa4.9
sacrebleu2.2.0
scipy1.12.0
Send2Trash1.8.2
setuptools65.5.0
six1.16.0
slim0.1c:\users\someone\desktop\website\2023\tensorflow object detection\tfodball\tensorflow\models\research\slim
sniffio1.3.1
soupsieve2.5
stack-data0.6.3
tabulate0.9.0
tensorboard2.10.1
tensorboard-data-server0.6.1
tensorboard-plugin-wit1.8.1
tensorflow2.10.0
tensorflow-estimator2.10.0
tensorflow-io0.31.0
tensorflow-io-gcs-filesystem0.31.0
tensorflow-object-detection-api0.1.1
termcolor2.4.0
terminado0.18.0
tf-models-official2.15.0
tf-slim1.1.0
tinycss21.2.1
tomli2.0.1
tornado6.4
traitlets5.14.1
twine5.0.0
types-python-dateutil2.8.19.20240106
typing_extensions4.10.0
uri-template1.3.0
urllib32.2.1
wcwidth0.2.13
webcolors1.13
webencodings0.5.1
websocket-client1.7.0
Werkzeug3.0.1
wget3.2
wheel0.42.0
widgetsnbextension4.0.10
wrapt1.16.0
zipp3.17.0

解决方案

1. 修正Python路径优先级

错误显示Python优先使用site-packages中的object_detection包,而非本地克隆的TensorFlow Models库。需将本地库路径加入环境变量:

  • 命令行训练前执行:
    set PYTHONPATH=C:\Users\someone\Desktop\website\2023\TensorFlow Object Detection\TFODBall\Tensorflow\models\research;C:\Users\someone\Desktop\website\2023\TensorFlow Object Detection\TFODBall\Tensorflow\models\research\slim
    
  • Jupyter Notebook中添加:
    import sys
    sys.path.insert(0, 'C:/Users/someone/Desktop/website/2023/TensorFlow Object Detection/TFODBall/Tensorflow/models/research')
    sys.path.insert(0, 'C:/Users/someone/Desktop/website/2023/TensorFlow Object Detection/TFODBall/Tensorflow/models/research/slim')
    

2. 编译Protobuf文件

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.28 23:47:58