mbaytas
mbaytas

Reputation: 3024

Tensorflow: ImportError for graph_util from tensorflow.python.framework

I am trying to go through the TensorFlow for Poets Codelab on OS X El Capitan, Python 2.7 (via HomeBrew), and TensorFlow 0.9.0. Instead of using Docker as instructed, I installed TensorFlow with pip and I cloned the tensorflow repo into my working folder.

To re-train the network, I run the following command:

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

Which fails with the following error:

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

In my Python shell, I am able to verify that TensorFlow is installed correctly by importing tensorflow, tensor_shape from tensorflow.python.framework, and gfile from tensorflow.python.platform (as in the retraining script). Trying to import graph_util results in the same error:

>>> 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

What could be the reason for this?

Edit: Opened issue #3203 on the tensorflow repo. Seems like this is a bug.

Note: This older question asks about the same error, but refers a different TensorFlow library with a retraining script which tries to import graph_util from tensorflow.python.client (see also this commit to the TensorFlow source).

Upvotes: 1

Views: 5131

Answers (3)

hututu
hututu

Reputation: 1

I also encountered the same problem as yours, and I used the following two steps to solve the problem:

  1. Activate TensorFlow,and stay active during following steps as well as running retrain.py: source ~/tensorflow/bin/activate # bash, sh, ksh, or zsh
  2. Upgrade TensorFlow version by those steps:

    (tensorflow)$ easy_install -U pip

    (tensorflow)$ pip install --upgrade tensorflow # for Python 2.7

Reference tensorflow official link:https://www.tensorflow.org/install/install_linux

Upvotes: 0

Rana Ranvijay Singh
Rana Ranvijay Singh

Reputation: 6155

I was facing the same issue. Once I did this

replace    from `tensorflow.python.framework import graph_util`
with       from `tensorflow.python.client import graph_util`

this below mentioned issue was gone.

File "retrain.py", line 75, in <module>
from tensorflow.python.framework import graph_util
ImportError: cannot import name graph_util

and new one came up.

File "retrain.py", line 1042, in <module>
    tf.app.run(main=main, argv=[sys.argv[0]] + unparsed)
TypeError: run() got an unexpected keyword argument 'argv'

To resolve this I replaced entire content of retain.py file (from master branch) with retain.py file (from r0.9 branch).

Upvotes: 3

Philip Clarke
Philip Clarke

Reputation: 735

Since this was the number 1 google result for me when I searched for this problem, I'll suggest a workaround which works (until tensorflow/examples get updated):

In examples/image_retraining/retrain.py

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

There is a commit now present in the tensorflow master to move graph_util from client to framework, so this workaround should not be required in the near future.

Upvotes: 3

Related Questions