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.
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:
- a standalone frozen graph file such as
model.pb - a
saved_model.pbfile 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.
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.
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.
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.
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:
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:
- use
from_saved_model()for current TensorFlow exports - use
tf.compat.v1.lite.TFLiteConverter.from_frozen_graph()only when a standalone frozen.pbis truly the artifact you have
That keeps the conversion story aligned with current TensorFlow tooling.
Common Pitfalls
- Treating every
.pbfile as if it were the same model format. - Pointing the converter at
saved_model.pbinstead 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
.pbfile 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
- Tensorflow. Converting unknown dimension size of a tensor to int
- Tensorflow Convolution Neural Network with different sized images
- Tensorflow Convolutions with different filter for each sample in the mini-batch
- ''tensorflow_core.estimator'' has no attribute ''inputs'', why does this happen?
- TensorFlow Correct way of excluding one class from loss function
- tensorflow Could not load dynamic library 'cudnn64_8.dll'; dlerror cudnn64_8.dll not found
- TensorFlow create dataset from numpy array
- Tensorflow Creating a graph in a class and running it outside
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.