TensorFlow freeze_graph.py: The name 'save/Const:0' refers to a Tensor which does not exist
<p>I am currently trying to export a trained TensorFlow model as a ProtoBuf file to use it with the TensorFlow C++ API on Android. Therefore, I'm using the <a href="https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/tools/freeze_graph.py" rel="nofollow"><code>freeze_graph.py</code></a> script.</p>

<p>I exported my model using <code>tf.train.write_graph</code>:</p>

<p><code>tf.train.write_graph(graph_def, FLAGS.save_path, out_name, as_text=True)
</code></p>

<p>and I'm using a checkpoint saved with <code>tf.train.Saver</code>.</p>

<p>I invoke <code>freeze_graph.py</code> as described at the top of the script. After compiling, I run</p>

<pre><code>bazel-bin/tensorflow/python/tools/freeze_graph \
--input_graph=&lt;path_to_protobuf_file&gt; \
--input_checkpoint=&lt;model_name&gt;.ckpt-10000 \
--output_graph=&lt;output_protobuf_file_path&gt; \
--output_node_names=dropout/mul_1
</code></pre>

<p>This gives me the following error message:</p>

<pre><code>TypeError: Cannot interpret feed_dict key as Tensor: The name 'save/Const:0' refers to a Tensor which does not exist. The operation, 'save/Const', does not exist in the graph.
</code></pre>

<p>As the error states I do not have a tensor <code>save/Const:0</code> in my exported model. However, the code of <code>freeze_graph.py</code> says that one can specify this tensor name by the flag <code>filename_tensor_name</code>. Unfortunately I cannot find any information on what this tensor should be and how to set it correctly for my model.</p>

<p>Can somebody tell my either how to produce a <code>save/Const:0</code> tensor in my exported ProtoBuf model or how to set the flag <code>filename_tensor_name</code> correctly?</p>

<p>The <code>--filename_tensor_name</code> flag is used to specify the name of a placeholder tensor created when you construct a <a href="https://www.tensorflow.org/versions/r0.10/api_docs/python/state_ops.html#Saver" rel="nofollow"><code>tf.train.Saver</code></a> for your model.*</p>

<p>In your original program, you can print out the value of <code>saver.saver_def.filename_tensor_name</code> to get the value that you should pass for this flag. You may also want to print the value of <code>saver.saver_def.restore_op_name</code> to get a value for the <code>--restore_op_name</code> flag (since I suspect the default won't be correct for your graph).</p>

<p>Alternatively, the <a href="https://github.com/tensorflow/tensorflow/blob/91a70cbf1c627117b70a3d2dd4c612779369e293/tensorflow/core/protobuf/saver.proto" rel="nofollow"><code>tf.train.SaverDef</code> protocol buffer</a> includes all of the information you need to reconstruct the relevant information for these flags. If you prefer, you can write <code>saver.saver_def</code> to a file, and pass the name of that file as the <code>--input_saver</code> flag to <code>freeze_graph.py</code>.</p>

<hr>

<p>&nbsp;* The default name scope for a <code>tf.train.Saver</code> is <code>"save/"</code> and the placeholder is <a href="https://github.com/tensorflow/tensorflow/blob/91a70cbf1c627117b70a3d2dd4c612779369e293/tensorflow/python/training/saver.py#L609" rel="nofollow">actually a <code>tf.constant()</code></a> whose name defaults to <code>"Const:0"</code>, which explains why the flag defaults to <code>"save/Const:0"</code>.</p>

python tensorflow protocol-buffers google-protobuf