TensorFlow
TFLite
model conversion
machine learning
.pb file

How to convert .pb to TFLite format?

Master System Design with Codemia

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

Introduction

Converting a .pb TensorFlow model to .tflite is possible, but the first step is identifying what the .pb file actually contains. In older TensorFlow projects it is often a frozen GraphDef, while in newer workflows the preferred deployment input is a SavedModel. The conversion path depends on that distinction.

Understand What the .pb File Is

A .pb extension only tells you that the file is a serialized protobuf. It does not tell you whether it is:

  • a frozen graph used in TensorFlow 1.x
  • one piece of a SavedModel export
  • some other protobuf artifact unrelated to direct TFLite conversion

TFLite conversion works best when you know the model inputs and outputs clearly. For frozen graphs, you usually must specify them explicitly.

Frozen Graph Conversion Path

If the .pb file is a frozen graph, you can use the TensorFlow Lite converter for frozen graphs through the compatibility API.

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_tensor"],
6    output_arrays=["output_tensor"],
7    input_shapes={"input_tensor": [1, 224, 224, 3]},
8)
9
10tflite_model = converter.convert()
11
12with open("model.tflite", "wb") as f:
13    f.write(tflite_model)

This only works if you know the correct tensor names and shapes. Those names are graph node names, not friendly layer labels guessed from code comments.

Finding Input and Output Names

A common obstacle is not knowing the actual graph tensor names. You can inspect them by loading the graph and printing operations.

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:
8    print(node.name)

This gives you a starting point for identifying candidate input and output nodes. In practice, you usually combine this with knowledge from the training code so you know which tensors represent the model boundary.

SavedModel Is Usually Cleaner

If you can reconstruct or export the model as a SavedModel, conversion is usually simpler and less brittle than converting directly from a frozen graph.

A typical TFLite conversion from SavedModel looks like this:

python
1import tensorflow as tf
2
3converter = tf.lite.TFLiteConverter.from_saved_model("saved_model_dir")
4tflite_model = converter.convert()
5
6with open("model.tflite", "wb") as f:
7    f.write(tflite_model)

This path is preferred because TensorFlow can infer much more of the model signature and metadata from the exported model structure.

Add Optimization or Quantization

Once conversion works, you can shrink the model or target mobile deployment constraints with optimization settings.

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

For some models, full integer quantization requires a representative dataset. That is a deployment optimization step, not a requirement for basic conversion.

Test the Converted Model

Do not stop after the file is written. Load the .tflite file with an interpreter and verify the input and output details.

python
1import tensorflow as tf
2
3interpreter = tf.lite.Interpreter(model_path="model.tflite")
4interpreter.allocate_tensors()
5
6input_details = interpreter.get_input_details()
7output_details = interpreter.get_output_details()
8
9print(input_details)
10print(output_details)

That catches issues such as unexpected tensor shapes, unsupported ops, or wrong assumptions about the model signature before you move the file to a mobile app or embedded device.

Unsupported Operations

Some TensorFlow graphs contain operations that TFLite cannot lower directly. In that case, conversion fails unless you refactor the model, use supported ops only, or enable selected fallback paths where appropriate.

This is why conversion is not just a file-format rename. TFLite is a different runtime with a smaller supported operator set optimized for edge deployment.

Common Pitfalls

The most common mistake is assuming every .pb file can be converted the same way without first checking whether it is a frozen graph or part of a SavedModel. Another is guessing the input and output tensor names and then debugging the wrong conversion problem. Developers also often treat conversion success as proof that the model is deployment-ready, even though unsupported behavior may only become obvious during interpreter testing. A final issue is trying to force a complicated legacy TensorFlow 1.x graph through TFLite when exporting a clean SavedModel would be a more reliable route.

Summary

  • A .pb file may represent different TensorFlow artifacts, so identify it before converting.
  • Frozen graphs can be converted with tf.compat.v1.lite.TFLiteConverter.from_frozen_graph.
  • 'SavedModel is usually a cleaner and more maintainable conversion path.'
  • You need correct input and output tensor names for frozen-graph conversion.
  • Always validate the resulting .tflite file with a TFLite interpreter before deployment.

Course illustration
Course illustration

All Rights Reserved.