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

TensorFlow训练报错:无法从tensorflow.python.framework导入'tensor'

问题解决:TensorFlow目标检测训练导入'tensor'失败

问题背景

已完成ssd_mobilenet_v2_fpnlite_640x640_coco17_tpu-8预训练模型下载、protos编译,能在自定义脚本实现摄像头目标检测,但训练时触发导入错误。设备为Windows10,依赖版本如下:

tensorboard                   2.10.1
tensorboard-data-server       0.6.1
tensorboard-plugin-wit        1.8.1
tensorflow                    2.10.1
tensorflow-addons             0.22.0
tensorflow-datasets           4.9.0
tensorflow-estimator          2.10.0
tensorflow-hub                0.16.1
tensorflow-intel              2.10.1
tensorflow-io                 0.31.0
tensorflow-io-gcs-filesystem  0.31.0
tensorflow-metadata           1.13.0
tensorflow-model-optimization 0.7.5
tensorflow-text               2.10.0
termcolor                     2.4.0
terminado                     0.18.0
text-unidecode                1.3
tf-keras                      2.15.0
tf-models-official            2.10.1
tf-slim                       1.1.0

运行训练代码时报错:

import os
import tensorflow as tf
from object_detection import model_main

def main():
    # Set the paths to your pipeline configuration and model directory
    pipeline_config_path = r"C:\python\models\ssd_mobilenet_v2_fpnlite_640x640_coco17_tpu-8\pipeline.config"
    model_dir = r"C:\python\models\research\object_detection\model_main_tf2.py"  # Replace with your desired directory

    # Run the training process
    tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.INFO)
    model_main.tf_main(pipeline_config_path, model_dir)

报错信息:

ImportError: cannot import name 'tensor' from 'tensorflow.python.framework' (C:\Users\admin\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\python\framework_init_.py)

已尝试添加slim路径但无效:

import sys
sys.path.append("/path/to/clone/repo/models/research/slim")
from nets.inception_v4 import *

解决方法

1. 修正model_dir路径错误

你的代码里model_dir指向了model_main_tf2.py脚本文件,这是错误的。model_dir应该是保存训练模型权重、日志的文件夹路径,修改示例:

model_dir = r"C:\python\models\my_training_output"  # 替换为你要保存训练结果的文件夹

2. 对齐TensorFlow与TF Keras版本兼容性

当前tf-keras版本2.15.0与TensorFlow 2.10.1版本不匹配,会导致底层导入逻辑冲突。执行以下命令统一版本:

  • 卸载现有tf-keras:
    pip uninstall tf-keras -y
    
  • 安装匹配版本:
    pip install tf-keras==2.10.0
    

3. 完善Object Detection API路径配置

在训练脚本开头添加完整的API路径,确保所有模块能被正确识别:

import sys
import os

# 添加models/research和slim的绝对路径
sys.path.append(r"C:\python\models\research")
sys.path.append(r"C:\python\models\research\slim")

# 配置系统环境变量补充路径
os.environ['PYTHONPATH'] += ';' + r"C:\python\models\research" + ';' + r"C:\python\models\research\slim"

import tensorflow as tf
from object_detection import model_main

4. 直接运行官方训练脚本

避免自定义调用的潜在问题,直接通过命令行执行官方脚本:

python C:\python\models\research\object_detection\model_main_tf2.py --pipeline_config_path=C:\python\models\ssd_mobilenet_v2_fpnlite_640x640_coco17_tpu-8\pipeline.config --model_dir=C:\python\models\my_training_output

内容的提问来源于stack exchange,提问作者James Morrish

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.30 10:04:53