EffePi
EffePi

Reputation: 376

How to keep placeholder names when merging tensorflow graphs?

I would like to add a branch to a previously created tensorflow graph. I did it by following mrry's answer to this question (Tensorflow: How to replace a node in a calculation graph?) and I have saved the new graph's definition.

When I import the new graph and try to get a placeholder of the original graph, I get the following error: ValueError: Requested return_element 'pool_3/_reshape:0' not found in graph_def., but the code works fine when I use the original graph.

How can I maintain the references to the original placeholders

My code to merge the two graph is

with tf.Session() as sess:

# Get the b64_graph and its output tensor
resized_b64_tensor, = (tf.import_graph_def(b64_graph_def, name='',
          return_elements=[B64_OUTPUT_TENSOR_NAME+":0"]))

with gfile.FastGFile(model_filename, 'rb') as f:
  inception_graph_def = tf.GraphDef()
  inception_graph_def.ParseFromString(f.read())

  # Concatenate b64_graph and inception_graph
  g_1 = tf.import_graph_def(inception_graph_def, name='graph_name',
               input_map={RESIZED_INPUT_TENSOR_NAME : resized_b64_tensor})

  # Save joined graph
  joined_graph = sess.graph
  with gfile.FastGFile(output_graph_filename, 'wb') as f:
    f.write( joined_graph.as_graph_def().SerializeToString() )

Upvotes: 3

Views: 324

Answers (1)

EffePi
EffePi

Reputation: 376

I have found the solution indirectly by reading this post Working with multiple graphs in TensorFlow.

When the graph has a name assigned, it is appended to the name of the tensors and operations it contains. In particular, if I join two graphs into a new one, the name of the latter is appended to the previous names. Therefore the right code to obtain a tensor would have been

sess.graph.get_tensor_by_name('graph_name/' + 'PreviousGraphName/PreviousTensorName:0')

Upvotes: 3

Related Questions