model conversion
ckpt to pb
TensorFlow
machine learning
tutorial

How to convert .ckpt to .pb?

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

Converting a TensorFlow checkpoint into a .pb file usually means restoring trained variables and freezing them into a graph that can be used for inference. This is mainly a TensorFlow 1.x workflow, so the first thing to confirm is whether you truly need a legacy .pb graph or whether a modern SavedModel export would be more appropriate.

Understand what the two files represent

A checkpoint stores variable values. It does not, by itself, represent a complete standalone inference artifact. A .pb file, by contrast, usually stores a serialized graph definition, and in the frozen-graph case it also contains the weights embedded as constants.

That means a checkpoint is not enough on its own. To freeze it correctly, you generally need:

  • the graph structure
  • the checkpoint path
  • the output node names used for inference

Without the output node names, TensorFlow cannot know which parts of the graph should be preserved in the final exported file.

Classic TensorFlow 1.x freezing flow

A compatibility-style conversion looks like this:

python
1import tensorflow as tf
2
3meta_path = "model.ckpt.meta"
4ckpt_path = "model.ckpt"
5output_pb = "frozen_model.pb"
6output_node_names = ["output"]
7
8with tf.compat.v1.Session(graph=tf.Graph()) as sess:
9    saver = tf.compat.v1.train.import_meta_graph(meta_path)
10    saver.restore(sess, ckpt_path)
11
12    graph_def = tf.compat.v1.get_default_graph().as_graph_def()
13    frozen_graph_def = tf.compat.v1.graph_util.convert_variables_to_constants(
14        sess,
15        graph_def,
16        output_node_names
17    )
18
19    with tf.io.gfile.GFile(output_pb, "wb") as f:
20        f.write(frozen_graph_def.SerializeToString())
21
22print("Saved", output_pb)

This restores the checkpoint, turns variables into constants, and writes the final .pb graph.

Finding the right output node names

Output node names are often the hardest part of the conversion. If you are not sure what they are, inspect the graph operations after restoring it:

python
for op in tf.compat.v1.get_default_graph().get_operations():
    print(op.name)

The goal is to keep only the graph needed to compute your real inference outputs. If you freeze the wrong nodes, the .pb file may load successfully while still being useless for inference. In practice, spending time on node inspection usually saves more time than blindly retrying exports.

Loading the frozen graph later

A classic load path for the resulting .pb file looks like this:

python
1import tensorflow as tf
2
3with tf.io.gfile.GFile("frozen_model.pb", "rb") as f:
4    graph_def = tf.compat.v1.GraphDef()
5    graph_def.ParseFromString(f.read())
6
7with tf.Graph().as_default() as graph:
8    tf.import_graph_def(graph_def, name="")
9
10print("Loaded ops:", len(graph.get_operations()))

This is the older inference-side pattern for graph-based TensorFlow deployments.

When SavedModel is the better answer

If you are working with TensorFlow 2.x or a Keras model, direct .ckpt to .pb conversion is often not the best path. In many modern projects, the right export is simply:

python
model.save("saved_model_dir")

That produces a SavedModel, which is easier to serve and maintain in current TensorFlow workflows. It also preserves more model metadata and signatures than a raw frozen graph workflow usually does. Many requests for .ckpt to .pb come from legacy tutorials or downstream tooling that still expects frozen graphs.

Common Pitfalls

  • Assuming a checkpoint already contains the full deployable model.
  • Forgetting that the graph structure and output node names are required for freezing.
  • Using the wrong output node names and exporting the wrong slice of the graph.
  • Applying old TensorFlow 1.x freezing steps to a modern 2.x model when SavedModel would be simpler.

Summary

  • Converting .ckpt to .pb usually means restoring a graph and freezing variables into constants.
  • You need the graph structure, checkpoint path, and correct output node names.
  • This is primarily a TensorFlow 1.x workflow.
  • In modern TensorFlow projects, SavedModel is usually the better export target unless legacy tooling requires .pb.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free 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.