freeze_graph.py
TensorFlow v1
model freezing
deep learning
machine learning

How to use freeze_graph.py tool in TensorFlow v1

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

In TensorFlow v1, freeze_graph.py is used to combine a graph definition with checkpoint weights and produce a frozen graph file, usually a .pb file. Freezing replaces variable nodes with constants so the graph can be deployed without a separate checkpoint.

This is a TensorFlow v1 workflow, so the main challenge is not the command itself. It is knowing which graph file, checkpoint prefix, and output node names to pass into the tool.

What You Need Before Freezing

You typically need:

  • a graph definition file such as .pbtxt
  • a checkpoint prefix such as model.ckpt-1000
  • the names of the output nodes

If any of those are wrong, the freeze step will fail or produce a graph that loads but is unusable.

The checkpoint argument is usually the prefix, not the individual .index or .data file.

Typical freeze_graph.py Command

bash
1python freeze_graph.py \
2  --input_graph=graph.pbtxt \
3  --input_checkpoint=model.ckpt-1000 \
4  --output_graph=frozen_graph.pb \
5  --output_node_names=predictions

That command tells TensorFlow:

  • where the graph structure lives
  • which checkpoint contains the trained variable values
  • which nodes must remain reachable in the frozen output

Everything not needed to reach the output nodes can be pruned away.

Why Output Node Names Matter

The output nodes define the part of the graph you want to keep. If you freeze with the wrong output node names, TensorFlow may discard the layers you actually need.

A practical way to inspect node names inside a v1 graph is:

python
1import tensorflow as tf
2
3with tf.Graph().as_default():
4    with tf.gfile.GFile("graph.pbtxt", "r") as f:
5        graph_def = tf.GraphDef()
6        tf.import_graph_def(graph_def, name="")
7
8    for op in tf.get_default_graph().get_operations():
9        print(op.name)

Once you identify the final inference nodes, pass those names to freeze_graph.py.

This is usually the step that takes the most care, because a syntactically successful freeze can still be useless if the wrong outputs were preserved.

A Minimal Training-and-Freeze Example

python
1import tensorflow as tf
2
3tf.reset_default_graph()
4
5x = tf.placeholder(tf.float32, shape=[None, 4], name="input")
6w = tf.Variable(tf.ones([4, 1]), name="weights")
7b = tf.Variable(tf.zeros([1]), name="bias")
8y = tf.add(tf.matmul(x, w), b, name="predictions")
9
10saver = tf.train.Saver()
11
12with tf.Session() as sess:
13    sess.run(tf.global_variables_initializer())
14    tf.train.write_graph(sess.graph_def, ".", "graph.pbtxt", as_text=True)
15    saver.save(sess, "./model.ckpt")

After running that script, the freeze step can use:

bash
1python freeze_graph.py \
2  --input_graph=graph.pbtxt \
3  --input_checkpoint=model.ckpt \
4  --output_graph=frozen_graph.pb \
5  --output_node_names=predictions

When Freezing Is Useful

Freezing is useful when you want:

  • one portable inference graph
  • no separate checkpoint files
  • simpler deployment to older TF1-style inference pipelines

It is less about training and more about packaging the trained model for inference.

In other words, freeze_graph.py belongs near the end of a TensorFlow v1 workflow, after training and before deployment or offline inference export.

Common Pitfalls

  • Passing the wrong output node names and pruning away needed computation.
  • Pointing to .index or .data files instead of the checkpoint prefix.
  • Trying to use TensorFlow v2 habits directly with a v1 freeze workflow.
  • Freezing a training graph instead of the inference graph.
  • Forgetting that freeze_graph.py belongs to older TensorFlow tooling and expects a TF1-style graph/checkpoint setup.

Summary

  • 'freeze_graph.py in TensorFlow v1 converts a graph plus checkpoint into a frozen .pb graph.'
  • You need the graph file, checkpoint prefix, and correct output node names.
  • Output node names determine what part of the graph is preserved.
  • Freezing is mainly for inference packaging, not for training.
  • Most failures come from wrong node names or wrong checkpoint arguments, not from the freeze tool itself.

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.