16

I try to simply save and restore a graph, but the simplest example does not work as expected (this is done using version 0.9.0 or 0.10.0 on Linux 64 without CUDA using python 2.7 or 3.5.2)

First I save the graph like this:

import tensorflow as tf
v1 = tf.placeholder('float32') 
v2 = tf.placeholder('float32')
v3 = tf.mul(v1,v2)
c1 = tf.constant(22.0)
v4 = tf.add(v3,c1)
sess = tf.Session()
result = sess.run(v4,feed_dict={v1:12.0, v2:3.3})
g1 = tf.train.export_meta_graph("file")
## alternately I also tried:
## g1 = tf.train.export_meta_graph("file",collection_list=["v4"])

This creates a file "file" that is non-empty and also sets g1 to something that looks like a proper graph definition.

Then I try to restore this graph:

import tensorflow as tf
g=tf.train.import_meta_graph("file")

This works without an error, but does not return anything at all.

Can anyone provide the necessary code to simply just save the graph for "v4" and completely restore it so that running this in a new session will produce the same result?

2 Answers 2

34

To reuse a MetaGraphDef, you will need to record the names of interesting tensors in your original graph. For example, in the first program, set an explicit name argument in the definition of v1, v2 and v4:

v1 = tf.placeholder(tf.float32, name="v1")
v2 = tf.placeholder(tf.float32, name="v2")
# ...
v4 = tf.add(v3, c1, name="v4")

Then, you can use the string names of the tensors in the original graph in your call to sess.run(). For example, the following snippet should work:

import tensorflow as tf
_ = tf.train.import_meta_graph("./file")

sess = tf.Session()
result = sess.run("v4:0", feed_dict={"v1:0": 12.0, "v2:0": 3.3})

Alternatively, you can use tf.get_default_graph().get_tensor_by_name() to get tf.Tensor objects for the tensors of interest, which you can then pass to sess.run():

import tensorflow as tf
_ = tf.train.import_meta_graph("./file")
g = tf.get_default_graph()

v1 = g.get_tensor_by_name("v1:0")
v2 = g.get_tensor_by_name("v2:0")
v4 = g.get_tensor_by_name("v4:0")

sess = tf.Session()
result = sess.run(v4, feed_dict={v1: 12.0, v2: 3.3})

UPDATE: Based on discussion in the comments, here a the complete example for saving and loading, including saving the variable contents. This illustrates the saving of a variable by doubling the value of variable vx in a separate operation.

Saving:

import tensorflow as tf
v1 = tf.placeholder(tf.float32, name="v1") 
v2 = tf.placeholder(tf.float32, name="v2")
v3 = tf.mul(v1, v2)
vx = tf.Variable(10.0, name="vx")
v4 = tf.add(v3, vx, name="v4")
saver = tf.train.Saver([vx])
sess = tf.Session()
sess.run(tf.initialize_all_variables())
sess.run(vx.assign(tf.add(vx, vx)))
result = sess.run(v4, feed_dict={v1:12.0, v2:3.3})
print(result)
saver.save(sess, "./model_ex1")

Restoring:

import tensorflow as tf
saver = tf.train.import_meta_graph("./model_ex1.meta")
sess = tf.Session()
saver.restore(sess, "./model_ex1")
result = sess.run("v4:0", feed_dict={"v1:0": 12.0, "v2:0": 3.3})
print(result)

The bottom line is that, in order to make use of a saved model, you must remember the names of at least some of the nodes (e.g. a training op, an input placeholder, an evaluation tensor, etc.). The MetaGraphDef stores the list of variables that are contained in the model, and helps to restore these from a checkpoint, but you are required to reconstruct the tensors/operations used in training/evaluating the model yourself.

8
  • Thank you very much, I think understand now that the import_meta_graph function just updates the default graph and is not supposed to return anything useful. Also, there is no way to access what I need from the default graph unless I assigned a name to it originally. And apparently, the ":0" in the name used after restoring is somehow used to distinguis the operation from its output.
    – jpp1
    Aug 8, 2016 at 17:54
  • That's right. The return value from import_meta_graph() is a tf.train.Saver, which is only useful if your graph contains variables that you want to restore.
    – mrry
    Aug 8, 2016 at 17:56
  • Ah right, so the values of variables within the model are not getting saved and restored automatically by this? Assuming that v4 would also depend on an unknown number of trained varaibles, what would be the code to store them as well and then restore them as well later? My example code was just meant to be easy, but I just want to save a trained model, then use it. So save trained model would mean that I want to save the graph plus all variables values they have at the time of saving and then restore that later.
    – jpp1
    Aug 8, 2016 at 18:05
  • You could create a tf.train.Saver() in the first program and call saver.save(...). This writes out a MetaGraphDef alongside the trained weights. Then in the second program, do saver = tf.train.import_meta_graph(...); saver.restore(checkpoint_file_name) to load the trained weights back into the new session.
    – mrry
    Aug 8, 2016 at 18:23
  • 6
    This should be added to the official documentation since it is much clearer than the current example.
    – Paz
    Oct 6, 2016 at 14:27
1

Because tf.train.import_meta_graph is deprecated version now.

replace tf.train.import_meta_graph in your code with tf.compat.v1.train.import_meta_graph

It will solve your error.

2
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Jul 4, 2022 at 15:33
  • i had precisely mentioned which line of code should be changed if programmer gets 'tensorflow' doesn't have 'tf.import_meta_graph' module error. Thanks.
    – Fenil
    Jul 5, 2022 at 9:49

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.