TensorFlow
freeze_graph.py
Tensor error
troubleshooting
deep learning

TensorFlow freeze_graph.py The name 'save/Const0' refers to a Tensor which does not exist

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

The error The name 'save/Const:0' refers to a Tensor which does not exist occurs when using TensorFlow's freeze_graph.py utility to convert a checkpoint into a frozen graph (a single .pb file with weights baked in). This error means the graph definition file (.pb or .pbtxt) does not contain the saver nodes that freeze_graph expects, typically because the graph was exported without including the saver operations.

What freeze_graph Does

freeze_graph.py takes two inputs:

  1. A graph definition file (.pb or .pbtxt) containing the computation graph structure
  2. A checkpoint file (.ckpt) containing the trained weights

It combines them into a single frozen graph where all variables are replaced with constants, making the model portable for inference.

Why This Error Happens

The save/Const:0 tensor is part of TensorFlow's tf.train.Saver operations. When you export the graph definition using tf.train.write_graph(), the saver nodes may not be included if the saver was not yet created at export time:

python
1# This graph does NOT include saver nodes
2graph_def = tf.get_default_graph().as_graph_def()
3tf.train.write_graph(graph_def, '.', 'model.pb', as_text=False)
4
5# The saver is created AFTER the graph was exported
6saver = tf.train.Saver()
7saver.save(sess, 'model.ckpt')

The graph definition was saved before the saver was added, so freeze_graph cannot find save/Const:0.

How to Fix It

Fix 1: Export the Graph After Creating the Saver

Create the saver first, then export the graph:

python
1import tensorflow as tf
2
3# Build your model
4x = tf.placeholder(tf.float32, shape=[None, 784], name='input')
5W = tf.Variable(tf.zeros([784, 10]), name='weights')
6b = tf.Variable(tf.zeros([10]), name='bias')
7y = tf.nn.softmax(tf.matmul(x, W) + b, name='output')
8
9# Create saver BEFORE exporting graph
10saver = tf.train.Saver()
11
12with tf.Session() as sess:
13    sess.run(tf.global_variables_initializer())
14    # Train your model...
15
16    # Save checkpoint
17    saver.save(sess, './model/model.ckpt')
18
19    # Export graph AFTER saver is created
20    tf.train.write_graph(
21        sess.graph_def, './model', 'model.pb', as_text=False
22    )

Fix 2: Use tf.train.Saver Default Name

freeze_graph looks for saver nodes with the default name prefix save/. If you used a custom saver name, specify it:

bash
1python freeze_graph.py \
2    --input_graph=model.pb \
3    --input_checkpoint=model.ckpt \
4    --output_graph=frozen_model.pb \
5    --output_node_names=output \
6    --restore_op_name=save/restore_all \
7    --filename_tensor_name=save/Const:0

If your saver uses a custom name:

python
saver = tf.train.Saver(name='my_saver')

Then use:

bash
--restore_op_name=my_saver/restore_all \
--filename_tensor_name=my_saver/Const:0

Fix 3: Use freeze_graph API in Python

Instead of the command-line script, use the Python API which gives you more control:

python
1from tensorflow.python.tools import freeze_graph
2
3freeze_graph.freeze_graph(
4    input_graph='model/model.pb',
5    input_saver='',
6    input_binary=True,
7    input_checkpoint='model/model.ckpt',
8    output_node_names='output',
9    restore_op_name='save/restore_all',
10    filename_tensor_name='save/Const:0',
11    output_graph='model/frozen_model.pb',
12    clear_devices=True,
13    initializer_nodes=''
14)

For TensorFlow 2.x, freeze_graph is deprecated. Use SavedModel format instead:

python
1import tensorflow as tf
2
3# TF 2.x approach
4model = tf.keras.models.load_model('my_model')
5
6# Save as SavedModel
7tf.saved_model.save(model, 'saved_model_dir')
8
9# Convert to frozen graph if needed
10from tensorflow.python.framework.convert_to_constants import convert_variables_to_constants_v2
11
12full_model = tf.function(lambda x: model(x))
13full_model = full_model.get_concrete_function(
14    tf.TensorSpec(model.inputs[0].shape, model.inputs[0].dtype)
15)
16
17frozen_func = convert_variables_to_constants_v2(full_model)
18frozen_func.graph.as_graph_def()
19
20tf.io.write_graph(
21    graph_or_graph_def=frozen_func.graph,
22    logdir='.',
23    name='frozen_model.pb',
24    as_text=False
25)

Inspecting Graph Nodes

To debug which nodes exist in your graph:

python
1import tensorflow as tf
2
3# Load the graph
4graph_def = tf.compat.v1.GraphDef()
5with open('model.pb', 'rb') as f:
6    graph_def.ParseFromString(f.read())
7
8# List all node names
9for node in graph_def.node:
10    print(node.name)
11
12# Search for saver nodes specifically
13saver_nodes = [n.name for n in graph_def.node if 'save' in n.name.lower()]
14print("Saver nodes:", saver_nodes)

If this prints no saver nodes, the graph was exported without them.

Common Pitfalls

  • Order of operations: The saver must be created before tf.train.write_graph(). Creating it after means the graph definition does not contain saver operations.
  • MetaGraph vs GraphDef: tf.train.Saver.save() also creates a .meta file containing the saver nodes. You can use --input_saver to point to the meta file instead of relying on the graph def.
  • TF 2.x migration: freeze_graph.py is a TF 1.x tool. In TF 2.x, use convert_variables_to_constants_v2 or export as SavedModel directly.
  • Output node names: The --output_node_names must exactly match the name of your model's output tensor (without the :0 suffix). Use the node inspection code above to find the correct name.
  • Binary vs text format: If your graph file is .pbtxt (text format), set --input_binary=false.

Summary

  • This error means the graph definition file is missing saver nodes (save/Const:0)
  • Fix by creating tf.train.Saver() before calling tf.train.write_graph()
  • Alternatively, pass the .meta file via --input_saver flag
  • For TF 2.x, use SavedModel format and convert_variables_to_constants_v2 instead of freeze_graph
  • Inspect graph nodes with a Python script to verify saver operations exist

Course illustration
Course illustration

All Rights Reserved.