0

I'm trying to export the multi layer perceptron example as a .pb graph. In order to do it, I have named the input variables and output operation and added the following line:

tf.train.write_graph(sess.graph_def, "./", "graph.pb", False)

To import, I did the following:

with gfile.FastGFile("graph.pb",'rb') as f:

    print("load graph")
    graph_def = tf.GraphDef()
    graph_def.ParseFromString(f.read())
    graph_def = tf.GraphDef()
    graph_def.ParseFromString(f.read())
    _ = tf.import_graph_def(graph_def, name='')
    with tf.Session() as persisted_sess:

        persisted_result = persisted_sess.graph.get_tensor_by_name("output:0")
        avd = persisted_sess.run(persisted_result, feed_dict={"input_x:0": features_t})
        print ("Result:", str(avd))

It does import fine but throws an error for the "run" line.

Traceback (most recent call last):
  File "/usr/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 972, in _do_call
    return fn(*args)
  File "/usr/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 954, in _run_fn
    status, run_metadata)
  File "/usr/lib/python3.5/contextlib.py", line 66, in __exit__
    next(self.gen)
  File "/usr/lib/python3.5/site-packages/tensorflow/python/framework/errors.py", line 463, in raise_exception_on_not_ok_status
    pywrap_tensorflow.TF_GetCode(status))
tensorflow.python.framework.errors.FailedPreconditionError: Attempting to use uninitialized value Variable_3
     [[Node: Variable_3/read = Identity[T=DT_FLOAT, _class=["loc:@Variable_3"], _device="/job:localhost/replica:0/task:0/cpu:0"](Variable_3)]]

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "teste.py", line 56, in <module>
    avd = persisted_sess.run(persisted_result, feed_dict={"input_x:0": features_t})
  File "/usr/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 717, in run
    run_metadata_ptr)
  File "/usr/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 915, in _run
    feed_dict_string, options, run_metadata)
  File "/usr/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 965, in _do_run
    target_list, options, run_metadata)
  File "/usr/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 985, in _do_call
    raise type(e)(node_def, op, message)
tensorflow.python.framework.errors.FailedPreconditionError: Attempting to use uninitialized value Variable_3
     [[Node: Variable_3/read = Identity[T=DT_FLOAT, _class=["loc:@Variable_3"], _device="/job:localhost/replica:0/task:0/cpu:0"](Variable_3)]]

Caused by op 'Variable_3/read', defined at:
  File "teste.py", line 37, in <module>
    _ = tf.import_graph_def(graph_def, name='')
  File "/usr/lib/python3.5/site-packages/tensorflow/python/framework/importer.py", line 285, in import_graph_def
    op_def=op_def)
  File "/usr/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 2380, in create_op
    original_op=self._default_original_op, op_def=op_def)
  File "/usr/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 1298, in __init__
    self._traceback = _extract_stack()

FailedPreconditionError (see above for traceback): Attempting to use uninitialized value Variable_3
     [[Node: Variable_3/read = Identity[T=DT_FLOAT, _class=["loc:@Variable_3"], _device="/job:localhost/replica:0/task:0/cpu:0"](Variable_3)]]

I have tried to initialize all variables but it does not work.

BernardoGO
  • 1,826
  • 1
  • 20
  • 35

1 Answers1

2

TensorFlow splits saving the Graph definition and the Variable values in different files (graph and checkpoint respectively).

You want to use the TF Saver.

See this answer for details: https://stackoverflow.com/a/33762168/4120005

Or the documentation here: https://www.tensorflow.org/versions/r0.11/how_tos/variables/index.html#saving-variables

If you really need to restore just from the graphdef file (*.pb), to load it from C++ for instance, you will need to use the freeze_graph.py script from here: https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/tools/freeze_graph.py

This script takes a graphdef (.pb) and a checkpoint (.ckpt) file as input and outputs a graphdef file which contains the weights in the form of constants (you can read the docs on the script for more details).

Community
  • 1
  • 1
Daniel De Freitas
  • 2,603
  • 1
  • 17
  • 15
  • I'm following this code to use it on Android: https://github.com/tensorflow/tensorflow/tree/master/tensorflow/examples/android Seems like they got the .pb file with the weights on – BernardoGO Nov 02 '16 at 03:40
  • Honestly I don't know that specific example, but I think it's quite likely that this is following the C++ deployment flow: where you run a certain official script to merge a checkpoints file into the graphdef file (by turning variable assignments into constants in the graph definition). This can be loaded from C++ – Daniel De Freitas Nov 02 '16 at 03:47
  • By "this" I meant the resulting graphdef. – Daniel De Freitas Nov 02 '16 at 03:48
  • Here's the script I mentioned: https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/tools/freeze_graph.py. It takes a *.pb and a *.ckpt and produces a "merged" *.pb with the weights in it as constants. I still recommend the saver approach if you don't need C++ inference – Daniel De Freitas Nov 02 '16 at 03:52
  • Thanks, I think that it is what I'm looking for. Is it possible to use it without building tensorflow from source? I've installed it using pip. I'm having problems while building from source. It tries to reach the host: http://ufpr.dl.sourceforge.net/project/giflib/giflib-5.1.4.tar.gz which is offline. – BernardoGO Nov 02 '16 at 04:24
  • You need to clone the TF repo, but it should be ok to build just the script. There are instructions on the script itself for building just the script. I'll edit the answer to include the pointers to script. Please accept it if you think it answered your question. ;) – Daniel De Freitas Nov 02 '16 at 05:12
  • I couldn't build just the script, it throws some import errors. I think I'll just try to change the addresses for the hosts and build everything from source. I couldn't test it yet but I think that it solves the problem. Thank you. – BernardoGO Nov 02 '16 at 05:20
  • No problem. FWIW it appears the mirror for the link you mentioned used to be in Brazil. Maybe you can try using a proxy to force it to download its dependencies from elsewhere. – Daniel De Freitas Nov 02 '16 at 05:23