TensorFlow
TFLite
Python
Machine Learning
Model Conversion

Tensorflow Convert pb file to TFLITE using python

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 model to TensorFlow Lite is straightforward once you know what kind of .pb you actually have. That distinction matters because developers often say “I have a .pb file” when they really mean either a frozen GraphDef file or the saved_model.pb file inside a SavedModel directory. The conversion route is different for each case, and using the wrong converter is one of the most common reasons the process fails.

Identify the Model Format First

There are two common .pb situations:

  1. a standalone frozen graph file such as model.pb
  2. a saved_model.pb file inside a SavedModel directory

For modern TensorFlow workflows, the preferred input to tf.lite.TFLiteConverter is usually a SavedModel or a Keras model. A frozen GraphDef still works, but it uses the compatibility converter path.

That distinction is more important than the filename extension itself.

Preferred Path: Convert From a SavedModel Directory

If your model lives in a SavedModel directory, point the converter at the directory, not directly at the saved_model.pb file.

python
1import tensorflow as tf
2
3converter = tf.lite.TFLiteConverter.from_saved_model("saved_model")
4converter.optimizations = [tf.lite.Optimize.DEFAULT]
5
6tflite_model = converter.convert()
7
8with open("model.tflite", "wb") as f:
9    f.write(tflite_model)

This is the cleanest route because TensorFlow already knows the signatures, variables, and serving graph structure. In most current projects, if you can export a SavedModel, you should do that instead of preserving a separate frozen-graph workflow.

Converting a Frozen Graph .pb

If you truly have a frozen GraphDef file, the compatibility API can still convert it. You must supply the input and output tensor names explicitly, and sometimes input shapes as well.

python
1import tensorflow as tf
2
3converter = tf.compat.v1.lite.TFLiteConverter.from_frozen_graph(
4    graph_def_file="model.pb",
5    input_arrays=["input"],
6    output_arrays=["Identity"],
7    input_shapes={"input": [1, 224, 224, 3]}
8)
9
10converter.optimizations = [tf.lite.Optimize.DEFAULT]
11
12tflite_model = converter.convert()
13
14with open("model.tflite", "wb") as f:
15    f.write(tflite_model)

The hard part is rarely the converter call itself. The hard part is knowing the correct tensor names. They must match the names in the frozen graph, not the names you wish the graph used.

How To Find Input and Output Names

If you are not sure which tensors to pass, inspect the graph before conversion. A quick compatibility script can list the operation names.

python
1import tensorflow as tf
2
3with tf.io.gfile.GFile("model.pb", "rb") as f:
4    graph_def = tf.compat.v1.GraphDef()
5    graph_def.ParseFromString(f.read())
6
7for node in graph_def.node[:20]:
8    print(node.name, node.op)

This does not tell you the final answer automatically, but it gives you the graph vocabulary you need. In real conversion work, identifying the serving input and final output nodes is often the step that takes the most care.

Handle Unsupported Ops Carefully

Some models fail conversion because TFLite does not support every TensorFlow operation as a built-in Lite op. When that happens, you may need to allow selected TensorFlow ops in the converted model.

python
1import tensorflow as tf
2
3converter = tf.lite.TFLiteConverter.from_saved_model("saved_model")
4converter.target_spec.supported_ops = [
5    tf.lite.OpsSet.TFLITE_BUILTINS,
6    tf.lite.OpsSet.SELECT_TF_OPS,
7]
8
9tflite_model = converter.convert()

This can improve conversion success, but it may increase model size and reduce portability. It is best treated as a compatibility lever, not the first optimization choice.

Quantization Is a Separate Decision

Many conversion examples immediately add quantization because TensorFlow Lite is commonly used on mobile and edge devices. That is often useful, but it is separate from the basic question of converting .pb to .tflite.

A simple optimization setting is:

python
converter.optimizations = [tf.lite.Optimize.DEFAULT]

For more aggressive quantization, you may need representative datasets or stricter constraints on supported data types. That should be handled after you have a correct baseline conversion working.

Prefer Modern Export Paths for New Projects

If you control the training pipeline, the best long-term answer is usually to export either a SavedModel or a Keras model rather than maintaining a frozen .pb conversion path. Frozen graphs belong to an older TensorFlow workflow and tend to require more manual knowledge about node names and graph freezing.

So the practical guidance is:

  1. use from_saved_model() for current TensorFlow exports
  2. use tf.compat.v1.lite.TFLiteConverter.from_frozen_graph() only when a standalone frozen .pb is truly the artifact you have

That keeps the conversion story aligned with current TensorFlow tooling.

Common Pitfalls

  • Treating every .pb file as if it were the same model format.
  • Pointing the converter at saved_model.pb instead of the SavedModel directory that contains it.
  • Guessing input and output tensor names instead of inspecting the graph.
  • Assuming conversion failure always means a broken model when the real issue is unsupported ops.
  • Adding quantization too early and mixing optimization problems with basic conversion problems.

Summary

  • The correct conversion path depends on whether the .pb file is a frozen graph or part of a SavedModel.
  • For modern TensorFlow projects, tf.lite.TFLiteConverter.from_saved_model() is the preferred route.
  • Frozen GraphDef files can still be converted with tf.compat.v1.lite.TFLiteConverter.from_frozen_graph().
  • Successful frozen-graph conversion depends on accurate input and output tensor names.
  • Start with a plain working conversion, then add options such as quantization or selected TensorFlow ops if needed.

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.