tensorflow: How to use tf.train.write_graph() and tf.import_graph_def()? It seems that it does not work.

How to use tf.train.write_graph() and tf.import_graph_def()? It seems that it does not work.

Please refer the following codes and prints, the final result is None, but it is expected to be [101 102 103 104 105 106 107 108 109 110].

code:

import tensorflow as tf import os import numpy as np from tensorflow.python.platform import gfile

input1= tf.placeholder(tf.int32, [10], name=“input”) data = np.arange(10) output1= tf.add(input1, tf.constant(1), name=“output”)

with tf.Session() as sess: os.system(“rm -rf /tmp/load”) tf.train.write_graph(sess.graph_def, “/tmp/load”, “test.pb”, False) print sess.run(output1,{input1:data}) print “Done”

load graph

with gfile.FastGFile(“/tmp/load/test.pb”,‘rb’) as f: graph_def = tf.GraphDef() graph_def.ParseFromString(f.read()) tf.import_graph_def(graph_def, name=‘’)

run

with tf.Session() as sess: input_x = sess.graph.get_tensor_by_name(“input:0”) print input_x Const = sess.graph.get_tensor_by_name(“Const:0”) print Const
output = sess.graph.get_operation_by_name(“output”) print output

data1 = np.arange(10)+100
print "data1:", data1
result = sess.run(output, {input_x: data1})
print "result:",result

prints: [ 1 2 3 4 5 6 7 8 9 10] Done Tensor(“input:0”, shape=TensorShape([Dimension(10)]), dtype=int32) Tensor(“Const:0”, shape=TensorShape([]), dtype=int32) name: “output” op: “Add” input: “input” input: “Const” attr { key: “T” value { type: DT_INT32 } }

data1: [100 101 102 103 104 105 106 107 108 109] result: None

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 44 (14 by maintainers)

Most upvoted comments

im sure this is a complex issue, but there seems to be a lot of problems around saving/loading models. isnt this one of the fundamental things tensorflow and not the developer should take care of?

This fundamental functionality should really be one call in core tensorflow. It should not be reinvented painfully in dozens lines of code by every developer.

I’m having an issue using freeze_graph.py on a modified version of the cifar10 example. In short, I get all the root nodes of the graph using [n.name for n in sess.graph_def.node if '/' not in n.name and n.name != 'init'], and use that as the output_node_names parameter of freeze_graph. The freezing appears to work; 30 variables are converted to const ops, with ~100k ops in the final graph. However, upon examining the final protobuf final, there are a bunch of assign ops in the graph. This then leads to a similar problem as @po0ya, where I get

ValueError: graph_def is invalid at node u'conv_1/W/Assign': Input tensor 'conv_1/W:0' Cannot convert a tensor of type float32 to an input of type float32_ref.

upon attempting to import the frozen graph (with no input_map). It seems to me like there are some issues in convert_variables_to_constants, specifically the call to extract_sub_graph. Going by the comments, these functions should be stripping the graph of assignment nodes; however, judging by my protobuf file and the import call, this is not the case. @mrry, any thoughts?

I think the appropriate fix is to change the tensors that you’re remapping in input_map from the (names of the) variables themselves to (names of) the values of the variables. I think you can achieve it by replacing the following loop:

for v in tf.trainable_variables():
    vars[v.name] = sess.run(v)

…with this code:

for v in tf.trainable_variables():
    vars[v.value().name] = sess.run(v)

Assigned to @mrry since he wrote the instructions. 😃

Why is something that should be so simple, so incredibly complex and failure prone? These are supposed to be checkpoints to protect against failures, not to provoke failures. There should be a simple call to checkpoint everything and another to restore it. The restore should wipe out whatever was in memory before. I’ve spent hours trying to untangle this mess because I was hoping it would save me re-running a model that crashed. Now I see it is just easier to rerun the model and go see a movie than to make checkpoints work. If it is good, I’ll post a movie review - that way you have a backup plan ready when checkpoint recovery fails. 😃

@arcticmatt At a guess you’re probably including too many nodes with that list comprehension. Without seeing the whole graph, it’s hard to say what node is causing the assign to appear in the extracted subgraph, but I’d recommend being much more selective in your choice of output nodes if possible. If there’s still a problem, feel free to open a new issue!

raised a new issue https://github.com/tensorflow/tensorflow/issues/3628 for the freezing batchnorm

@lilac I see a similar error when saving a graph that has a batch normalization layer. Tried this with both 0.9 and 0.10.0rc0. When loading the saved graph, I get an error:

ValueError: graph_def is invalid at node u'BatchNorm/cond/AssignMovingAvg/Switch': Input tensor 'BatchNorm/moving_mean:0' Cannot convert a tensor of type float32 to an input of type float32_ref.

Without the batch normalization layer (i.e. tensorflow.contrib.layers.python.layers.batch_norm) this error is not thrown. Seems related to your problem, as this also contains a moving average.

Yeah, I was able to export a trained graph and use it my android application, I followed some of the instructions here http://stackoverflow.com/questions/34343259/is-there-an-example-on-how-to-generate-protobuf-files-holding-trained-tensorflow/34343517#34343517 by using tf.constant when saving the trained graph def