从object_detection导入model_main失败,TensorFlow无contrib属性报错求助
AttributeError: module 'tensorflow' has no attribute 'contrib' when importing model_main
报错信息
Traceback (most recent call last): File "../LearningSpace/ObJTEST01/main.py", line 9, in <module> from object_detection import model_main File "../anaconda3/envs/ObJTESTV01/lib/python3.10/site-packages/object_detection/model_main.py", line 26, in <module> from object_detection import model_lib File "../anaconda3/envs/ObJTESTV01/lib/python3.10/site-packages/object_detection/model_lib.py", line 27, in <module> from object_detection import eval_util File "../anaconda3/envs/ObJTESTV01/lib/python3.10/site-packages/object_detection/eval_util.py", line 35, in <module> slim = tf.contrib.slim AttributeError: module 'tensorflow' has no attribute 'contrib'
我的代码
import tensorflow as tf from object_detection.utils import config_util from object_detection.protos import pipeline_pb2 from google.protobuf import text_format import os import requests import shutil import tarfile from object_detection import model_main # 报错的导入语句 CUSTOM_MODEL_NAME = 'my_ssd_mobnet' PRETRAINED_MODEL_NAME = 'ssd_mobilenet_v2_fpnlite_320x320_coco17_tpu-8' PRETRAINED_MODEL_URL = 'http://download.tensorflow.org/models/object_detection/tf2/20200711/ssd_mobilenet_v2_fpnlite_320x320_coco17_tpu-8.tar.gz' TF_RECORD_SCRIPT_NAME = 'generate_tfrecord.py' LABEL_MAP_NAME = 'label_map.pbtxt' paths = { 'WORKSPACE_PATH': os.path.join('Tensorflow', 'workspace'), 'SCRIPTS_PATH': os.path.join('Tensorflow','scripts'), 'APIMODEL_PATH': os.path.join('Tensorflow','models'), 'ANNOTATION_PATH': os.path.join('Tensorflow', 'workspace','annotations'), 'IMAGE_PATH': os.path.join('Tensorflow', 'workspace','images'), 'MODEL_PATH': os.path.join('Tensorflow', 'workspace','models'), 'PRETRAINED_MODEL_PATH': os.path.join('Tensorflow', 'workspace','pre-trained-models'), 'CHECKPOINT_PATH': os.path.join('Tensorflow', 'workspace','models',CUSTOM_MODEL_NAME), 'OUTPUT_PATH': os.path.join('Tensorflow', 'workspace','models',CUSTOM_MODEL_NAME, 'export'), 'TFJS_PATH':os.path.join('Tensorflow', 'workspace','models',CUSTOM_MODEL_NAME, 'tfjsexport'), 'TFLITE_PATH':os.path.join('Tensorflow', 'workspace','models',CUSTOM_MODEL_NAME, 'tfliteexport'), 'PROTOC_PATH':os.path.join('Tensorflow','protoc') } files = { 'PIPELINE_CONFIG':os.path.join('Tensorflow', 'workspace','models', CUSTOM_MODEL_NAME, 'pipeline.config'), 'TF_RECORD_SCRIPT': os.path.join(paths['SCRIPTS_PATH'], TF_RECORD_SCRIPT_NAME), 'LABELMAP': os.path.join(paths['ANNOTATION_PATH'], LABEL_MAP_NAME) } for path in paths.values(): if not os.path.exists(path): if os.name == 'posix' or os.name == 'nt': os.makedirs(path, exist_ok=True) labels = [{'name':'licence', 'id':1}] with open(files['LABELMAP'], 'w') as f: for label in labels: f.write('item { \n') f.write('\tname:\'{}\'\n'.format(label['name'])) f.write('\tid:{}\n'.format(label['id'])) f.write('}\n') import subprocess # 训练命令 command = ( "--model_dir=Tensorflow/workspace/models/my_ssd_mobnet " "--pipeline_config_path=Tensorflow/workspace/models/my_ssd_mobnet/pipeline.config " "--num_train_steps=10000" )
已安装的包
pip install tensorflow-macos pip install tensorflow-object-detection-api
需要用上述命令训练模型,但目前无法正常导入相关包,请求解决。
解决方案
1. 核心问题
TensorFlow 2.x 已彻底移除tf.contrib模块,你安装的tensorflow-object-detection-api是适配TensorFlow 1.x的版本,导致代码调用tf.contrib.slim时出错。另外,原tf.contrib.slim功能现在作为独立包tensorflow-slim存在。
2. 修复步骤
步骤1:卸载冲突包
pip uninstall -y tensorflow-object-detection-api
步骤2:安装TF2兼容依赖
# 确保tensorflow-macos正常安装 pip install tensorflow-macos # 安装独立的slim库 pip install tensorflow-slim # 安装TF2官方适配的Object Detection API pip install tf-models-official
步骤3:调整代码逻辑
- 删除原报错导入语句
from object_detection import model_main - 替换为TF2专用的训练脚本调用方式:
修改代码中的训练部分:
# 指定TF2训练脚本路径(若通过tf-models-official安装,也可直接用`python -m object_detection.model_main_tf2`调用) TRAINING_SCRIPT = os.path.join(paths['APIMODEL_PATH'], 'research', 'object_detection', 'model_main_tf2.py') # 拼接完整训练命令 full_command = f"python {TRAINING_SCRIPT} {command}" # 执行训练 subprocess.run(full_command, shell=True, check=True)
步骤4:验证环境
运行以下代码确认依赖加载正常:
import tensorflow as tf import tensorflow_slim as slim from object_detection.utils import config_util print("Dependencies loaded successfully")
内容的提问来源于stack exchange,提问作者Sandeep Kumar Rudhravaram
相关产品推荐
相关产品推荐

