Skip to main content
 首页 » 编程设计

python之Tensorflow:来自 tensorflow.python.framework 的 graph_util 的导入错误

2025年05月04日27itcoder

我正在尝试通过 TensorFlow for Poets Codelab在 OS X El Capitan、Python 2.7(通过 HomeBrew)和 TensorFlow 0.9.0 上。我没有按照说明使用 Docker,而是使用 pip 安装了 TensorFlow,然后克隆了 tensorflow repo进入我的工作文件夹。

收件人re-train the network ,我运行以下命令:

python tensorflow/tensorflow/examples/image_retraining/retrain.py \ 
--bottleneck_dir=/tf_files/bottlenecks \ 
--how_many_training_steps 500 \ 
--model_dir=/tf_files/inception \ 
--output_graph=/tf_files/retrained_graph.pb \ 
--output_labels=/tf_files/retrained_labels.txt \ 
--image_dir /tf_files/flower_photos 

失败并出现以下错误:

Traceback (most recent call last): 
  File "tensorflow/tensorflow/examples/image_retraining/retrain.py", line 80, in <module> 
    from tensorflow.python.framework import graph_util 
ImportError: cannot import name graph_util 

在我的 Python shell 中,我能够通过从 tensorflow.python.framework 导入 tensorflow、tensor_shape 来验证 TensorFlow 是否已正确安装和 tensorflow.python.platform 中的 gfile(如 retraining script 中所示)。尝试导入 graph_util 会导致相同的错误:

>>> import tensorflow 
# Success 
>>> from tensorflow.python.framework import tensor_shape 
# Success 
>>> from tensorflow.python.platform import gfile 
# Success 
>>> from tensorflow.python.framework import graph_util 
Traceback (most recent call last): 
  File "<stdin>", line 1, in <module> 
ImportError: cannot import name graph_util 

这可能是什么原因?

编辑:打开issue #3203在 tensorflow repo 协议(protocol)上。看起来这是一个错误。

备注:This older question询问相同的错误,但使用重新训练脚本引用不同的 TensorFlow 库,该脚本尝试从 tensorflow.python.client 导入 graph_util(另请参见 this commit 到 TensorFlow来源)。

请您参考如下方法:

由于这是我搜索此问题时对我来说排名第一的谷歌结果,因此我会建议一个有效的解决方法(直到更新 tensorflow/examples):

在examples/image_retraining/retrain.py

中
CHANGE 
from `tensorflow.python.framework import graph_util` 
TO 
from `tensorflow.python.client import graph_util` 

tensorflow master 中现在有一个将 graph_util 从客户端移动到框架的提交,因此在不久的将来应该不需要这个解决方法。