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包列表
| Package | Version | Editable project location |
|---|---|---|
| absl-py | 2.1.0 | |
| anyio | 4.3.0 | |
| apache-beam | 2.54.0 | |
| argon2-cffi | 23.1.0 | |
| argon2-cffi-bindings | 21.2.0 | |
| arrow | 1.3.0 | |
| asttokens | 2.4.1 | |
| astunparse | 1.6.3 | |
| async-lru | 2.0.4 | |
| attrs | 23.2.0 | |
| avro-python3 | 1.10.2 | |
| Babel | 2.14.0 | |
| beautifulsoup4 | 4.12.3 | |
| bleach | 6.1.0 | |
| cachetools | 5.3.3 | |
| certifi | 2024.2.2 | |
| cffi | 1.16.0 | |
| charset-normalizer | 3.3.2 | |
| colorama | 0.4.6 | |
| comm | 0.2.1 | |
| contextlib2 | 21.6.0 | |
| contourpy | 1.2.0 | |
| cycler | 0.12.1 | |
| Cython | 3.0.8 | |
| debugpy | 1.8.1 | |
| decorator | 5.1.1 | |
| defusedxml | 0.7.1 | |
| dm-tree | 0.1.8 | |
| docutils | 0.20.1 | |
| exceptiongroup | 1.2.0 | |
| executing | 2.0.1 | |
| fastjsonschema | 2.19.1 | |
| flatbuffers | 23.5.26 | |
| fonttools | 4.49.0 | |
| fqdn | 1.5.1 | |
| gast | 0.4.0 | |
| google-auth | 2.28.1 | |
| google-auth-oauthlib | 0.4.6 | |
| google-pasta | 0.2.0 | |
| grpcio | 1.62.0 | |
| h11 | 0.14.0 | |
| h5py | 3.10.0 | |
| httpcore | 1.0.4 | |
| httpx | 0.27.0 | |
| idna | 3.6 | |
| importlib-metadata | 7.0.1 | |
| ipykernel | 6.29.3 | |
| ipython | 8.22.1 | |
| ipywidgets | 8.1.2 | |
| isoduration | 20.11.0 | |
| jaraco.classes | 3.3.1 | |
| jedi | 0.19.1 | |
| Jinja2 | 3.1.3 | |
| json5 | 0.9.17 | |
| jsonpointer | 2.4 | |
| jsonschema | 4.21.1 | |
| jsonschema-specifications | 2023.12.1 | |
| jupyter | 1.0.0 | |
| jupyter_client | 8.6.0 | |
| jupyter-console | 6.6.3 | |
| jupyter_core | 5.7.1 | |
| jupyter-events | 0.9.0 | |
| jupyter-lsp | 2.2.3 | |
| jupyter_server | 2.12.5 | |
| jupyter_server_terminals | 0.5.2 | |
| jupyterlab | 4.1.2 | |
| jupyterlab_pygments | 0.3.0 | |
| jupyterlab_server | 2.25.3 | |
| jupyterlab_widgets | 3.0.10 | |
| keras | 2.10.0 | |
| Keras-Preprocessing | 1.1.2 | |
| keyring | 24.3.1 | |
| kiwisolver | 1.4.5 | |
| libclang | 16.0.6 | |
| lvis | 0.5.3 | |
| lxml | 5.1.0 | |
| Markdown | 3.5.2 | |
| markdown-it-py | 3.0.0 | |
| MarkupSafe | 2.1.5 | |
| matplotlib | 3.8.3 | |
| matplotlib-inline | 0.1.6 | |
| mdurl | 0.1.2 | |
| mistune | 3.0.2 | |
| ml-dtypes | 0.3.2 | |
| more-itertools | 10.2.0 | |
| namex | 0.0.7 | |
| nbclient | 0.9.0 | |
| nbconvert | 7.16.1 | |
| nbformat | 5.9.2 | |
| nest-asyncio | 1.6.0 | |
| nh3 | 0.2.15 | |
| notebook | 7.1.1 | |
| notebook_shim | 0.2.4 | |
| numpy | 1.26.4 | |
| oauthlib | 3.2.2 | |
| object-detection | 0.1 | |
| opencv-python | 4.9.0.80 | |
| opt-einsum | 3.3.0 | |
| overrides | 7.7.0 | |
| packaging | 23.2 | |
| pandas | 2.2.1 | |
| pandocfilters | 1.5.1 | |
| parso | 0.8.3 | |
| pillow | 10.2.0 | |
| pip | 24.0 | |
| pkginfo | 1.9.6 | |
| platformdirs | 4.2.0 | |
| portalocker | 2.8.2 | |
| prometheus_client | 0.20.0 | |
| prompt-toolkit | 3.0.43 | |
| protobuf | 3.19.6 | |
| psutil | 5.9.8 | |
| pure-eval | 0.2.2 | |
| pyasn1 | 0.5.1 | |
| pyasn1-modules | 0.3.0 | |
| pycocotools | 2.0.7 | |
| pycparser | 2.21 | |
| Pygments | 2.17.2 | |
| pyparsing | 2.4.7 | |
| PyQt5 | 5.15.10 | |
| PyQt5-Qt5 | 5.15.2 | |
| PyQt5-sip | 12.13.0 | |
| python-dateutil | 2.8.2 | |
| python-json-logger | 2.0.7 | |
| pytz | 2024.1 | |
| pywin32 | 306 | |
| pywin32-ctypes | 0.2.2 | |
| pywinpty | 2.0.13 | |
| PyYAML | 6.0.1 | |
| pyzmq | 25.1.2 | |
| qtconsole | 5.5.1 | |
| QtPy | 2.4.1 | |
| readme_renderer | 43.0 | |
| referencing | 0.33.0 | |
| regex | 2023.12.25 | |
| requests | 2.31.0 | |
| requests-oauthlib | 1.3.1 | |
| requests-toolbelt | 1.0.0 | |
| rfc3339-validator | 0.1.4 | |
| rfc3986 | 2.0.0 | |
| rfc3986-validator | 0.1.1 | |
| rich | 13.7.0 | |
| rpds-py | 0.18.0 | |
| rsa | 4.9 | |
| sacrebleu | 2.2.0 | |
| scipy | 1.12.0 | |
| Send2Trash | 1.8.2 | |
| setuptools | 65.5.0 | |
| six | 1.16.0 | |
| slim | 0.1 | c:\users\someone\desktop\website\2023\tensorflow object detection\tfodball\tensorflow\models\research\slim |
| sniffio | 1.3.1 | |
| soupsieve | 2.5 | |
| stack-data | 0.6.3 | |
| tabulate | 0.9.0 | |
| tensorboard | 2.10.1 | |
| tensorboard-data-server | 0.6.1 | |
| tensorboard-plugin-wit | 1.8.1 | |
| tensorflow | 2.10.0 | |
| tensorflow-estimator | 2.10.0 | |
| tensorflow-io | 0.31.0 | |
| tensorflow-io-gcs-filesystem | 0.31.0 | |
| tensorflow-object-detection-api | 0.1.1 | |
| termcolor | 2.4.0 | |
| terminado | 0.18.0 | |
| tf-models-official | 2.15.0 | |
| tf-slim | 1.1.0 | |
| tinycss2 | 1.2.1 | |
| tomli | 2.0.1 | |
| tornado | 6.4 | |
| traitlets | 5.14.1 | |
| twine | 5.0.0 | |
| types-python-dateutil | 2.8.19.20240106 | |
| typing_extensions | 4.10.0 | |
| uri-template | 1.3.0 | |
| urllib3 | 2.2.1 | |
| wcwidth | 0.2.13 | |
| webcolors | 1.13 | |
| webencodings | 0.5.1 | |
| websocket-client | 1.7.0 | |
| Werkzeug | 3.0.1 | |
| wget | 3.2 | |
| wheel | 0.42.0 | |
| widgetsnbextension | 4.0.10 | |
| wrapt | 1.16.0 | |
| zipp | 3.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文件
相关产品推荐
相关产品推荐

