Skip to main content
 首页 » 编程设计

python之Tensorflow:ImportError 无法在 retrain.py 中导入名称 graph_util

2025年05月04日31daizhj

我从源代码编译了 Tensorflow 以使用 GPU。我能够成功安装它。它能够成功运行示例 mnist convolutional.py 脚本。

但是,当我在 tensorflow/examples/image_retraining 中执行 retrain.py 脚本时,它会抛出以下错误:

Traceback (most recent call last): 
  File "retrain.py", line 70, in <module> 
    from tensorflow.python.client import graph_util 
ImportError: cannot import name graph_util 

使用命令和其他输出:

$ python retrain.py \ 
--bottleneck_dir=gpu_train/bottlenecks \ 
--model_dir=inception \ 
--output_graph=gpu_train/retrained_graph.pb \ 
--output_labels=gpu_train/retrained_labels.txt \ 
--image_dir categories 
I tensorflow/stream_executor/dso_loader.cc:108] successfully opened CUDA library libcublas.dylib locally 
I tensorflow/stream_executor/dso_loader.cc:108] successfully opened CUDA library libcudnn.dylib locally 
I tensorflow/stream_executor/dso_loader.cc:108] successfully opened CUDA library libcufft.dylib locally 
I tensorflow/stream_executor/dso_loader.cc:108] successfully opened CUDA library libcuda.dylib locally 
I tensorflow/stream_executor/dso_loader.cc:108] successfully opened CUDA library libcurand.dylib locally 
Traceback (most recent call last): 
  File "retrain.py", line 70, in <module> 
    from tensorflow.python.client import graph_util 
ImportError: cannot import name graph_util 

请您参考如下方法:

有一个commit昨天将文件 graph_util.py 从 tensorflow.python.client 移动到 tensorflow.python.framework。

奇怪的是,您有旧版本的 retrain.py 和 import from tensorflow.python.client import graph_util,但也有 graph_util。 py 已移动。


无论如何,master version graph_util.py 是最新的(使用 from tensorflow.python.framework import graph_util)所以你应该得到最新版本(不需要再次编译,只需拉) .