Add weights to .pb file exported by TensorFlow
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
The phrase "add weights to a .pb file" usually signals a misunderstanding of how TensorFlow saves models. In older TensorFlow workflows, the graph structure and the trained variable values were often stored separately. A .pb file could contain only the graph definition, while the actual weights lived in checkpoint files. To produce a single deployable graph, you did not manually insert weights afterward. You restored the checkpoint and exported or froze the model correctly.
Graph Versus Weights
In TensorFlow 1.x, these pieces were commonly separate:
- graph definition, often in
.pbor.pbtxt - variable values in checkpoint files such as
.ckpt
That separation is useful during training because variables remain mutable. But for inference deployment, people often want one artifact that no longer depends on checkpoints. The usual answer was a frozen graph, where variable values are converted into constants embedded in the graph.
That is the key idea: you do not hand-edit a .pb to add weights. You load the graph and checkpoint together, then export a new artifact.
Freezing a TensorFlow 1.x Graph
A classic TensorFlow 1.x flow looked like this:
- rebuild or import the graph
- restore variables from the checkpoint
- convert variables to constants
- write the frozen graph to disk
A simplified example:
The important part is that the checkpoint supplies the learned values. The exported frozen graph is the artifact that now contains them as constants.
TensorFlow 2.x Uses SavedModel First
Modern TensorFlow prefers SavedModel, not ad hoc graph-plus-checkpoint juggling. If you have a trained Keras model, save it as a SavedModel and convert from there only if a downstream tool specifically requires a graph artifact.
If you truly need a frozen graph for inference tooling, the process is to build a concrete function and convert variables to constants during export. The modern concept is still the same: export a new artifact from a loaded model state. Do not try to patch bytes into a .pb file manually.
When People Get Stuck
A common failure mode is having only a graph .pb with no checkpoint and expecting the original trainable weights to be recoverable. If that .pb contains only the graph structure, the trainable variable values are gone unless you still have the checkpoint.
Another failure mode is confusing a frozen inference graph with a training graph. A frozen graph is useful for prediction, but once variables become constants, you do not continue normal training from it the same way.
So before you attempt any conversion, ask:
- do I have the checkpoint or SavedModel
- is the current
.pba plain graph or already a frozen graph - do I need training continuation or inference deployment
The answer changes the correct workflow.
A Better Mental Model
Think of a model artifact in terms of state ownership:
- training graph plus checkpoint means mutable model state
- frozen graph means embedded fixed weights for inference
- SavedModel means a modern packaged export that can include variables, signatures, and assets
Once that distinction is clear, the question shifts from "how do I inject weights into this .pb" to "which export format do I actually need for this stage of the pipeline."
Deployment Guidance
If you control the deployment system, prefer SavedModel for TensorFlow 2.x workflows. It is the standard format for serving, conversion, and further processing.
Use a frozen graph only when a specific consumer expects it, such as a legacy mobile or embedded pipeline. In those cases, produce it from the trained model source, not from manual editing.
Common Pitfalls
The biggest mistake is assuming a .pb file always contains both graph and weights. It may not.
Another issue is trying to continue training from a frozen graph. Freezing is primarily an inference-oriented export step.
Be careful with output node names during graph freezing. If you supply the wrong names, the exported graph may be incomplete or unusable.
Finally, do not build new workflows around deprecated TensorFlow 1.x conventions if you have the option to use SavedModel instead.
Summary
- A
.pbfile may contain only the graph structure, not the trained weights. - In TensorFlow 1.x, weights were often stored separately in checkpoints.
- To create a single deployable graph, restore the checkpoint and freeze the graph.
- In TensorFlow 2.x, prefer
SavedModelfor standard model export. - You generally do not manually add weights to a
.pbfile byte by byte. - Always determine whether you need a trainable model artifact or an inference artifact before exporting.

