本地虚拟环境运行TensorFlow目标检测测试代码遇模块缺失错误
解决本地虚拟环境中TensorFlow Object Detection模块缺失问题
已安装所有所需依赖包,代码在Colaboratory中可正常运行,但在本地PC的虚拟环境中执行命令python object_detection/builders/model_builder_tf2_test.py时出现模块缺失错误,具体报错信息如下:
Traceback (most recent call last): File "C:\Users\cdelaney\Documents\TensorflowTraining\tfod\Lib\site-packages\object_detection\models\ssd_efficientnet_bifpn_feature_extractor.py", line 36, in <module> from official.legacy.image_classification.efficientnet import efficientnet_model ModuleNotFoundError: No module named 'official.legacy' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:\Users\cdelaney\Documents\TensorflowTraining\models\research\object_detection\builders\model_builder_tf2_test.py", line 24, in <module> from object_detection.builders import model_builder File "C:\Users\cdelaney\Documents\TensorflowTraining\tfod\Lib\site-packages\object_detection\builders\model_builder.py", line 70, in <module> from object_detection.models import ssd_efficientnet_bifpn_feature_extractor as ssd_efficientnet_bifpn File "C:\Users\cdelaney\Documents\TensorflowTraining\tfod\Lib\site-packages\object_detection\models\ssd_efficientnet_bifpn_feature_extractor.py", line 38, in <module> from official.vision.image_classification.efficientnet import efficientnet_model File "C:\Users\cdelaney\Documents\TensorflowTraining\tfod\Lib\site-packages\official\vision\image_classification\efficientnet\efficientnet_model.py", line 37, in <module> from official.vision.image_classification import preprocessing File "C:\Users\cdelaney\Documents\TensorflowTraining\tfod\Lib\site-packages\official\vision\image_classification\preprocessing.py", line 25, in <module> from official.vision.image_classification import augment File "C:\Users\cdelaney\Documents\TensorflowTraining\tfod\Lib\site-packages\official\vision\image_classification\augment.py", line 31, in <module> from tensorflow.python.keras.layers.preprocessing import image_preprocessing as image_ops ModuleNotFoundError: No module named 'tensorflow.python.keras.layers.preprocessing'
核心原因
问题根源是本地虚拟环境的TensorFlow版本与Object Detection API依赖的版本不兼容:
tensorflow.python.keras.layers.preprocessing模块路径在不同TF版本中存在结构差异,本地版本过高或过低都会导致找不到该模块;official.legacy模块缺失说明TensorFlow官方Models库的版本和当前TF版本不匹配。
解决方案
1. 对齐Colab的TensorFlow版本
先在Colab中执行!pip show tensorflow查看当前使用的TF版本,然后在本地虚拟环境安装完全一致的版本:
pip install tensorflow==x.x.x # 替换成Colab显示的版本号,例如2.15.0
2. 重新安装兼容的Object Detection依赖
卸载现有冲突包,再安装适配版本:
pip uninstall -y object-detection tensorflow-models-official # 安装与TF版本匹配的官方Models包 pip install tensorflow-models-official==对应版本号
或者直接从TensorFlow Models仓库克隆对应分支的代码,本地编译安装:
git clone https://github.com/tensorflow/models.git cd models/research # 切换到与TF版本兼容的分支(如TF2.15对应r2.15分支) git checkout r2.15 # 编译proto文件 protoc object_detection/protos/*.proto --python_out=. # 安装Object Detection API cp object_detection/packages/tf2/setup.py . python -m pip install .
3. 验证修复
重新运行测试命令,确认错误消失:
python object_detection/builders/model_builder_tf2_test.py
内容的提问来源于stack exchange,提问作者Conor Delaney
相关产品推荐
相关产品推荐

