TensorFlow
Machine Learning
Neural Networks
Model Persistence
Data Serialization

TensorFlow saving into/loading a graph from a file

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

TensorFlow is an open-source machine learning framework that is widely used for building models for deep learning applications. One of the notable features of TensorFlow is the ability to save the computation graph, which represents the operations (nodes) and their dependencies (edges).

Saving a TensorFlow graph ensures that you can easily reconstruct the model for further training, evaluation, or inference. This article delves into the process of saving and loading TensorFlow graphs, accompanied by examples and technical insights.

Saving a TensorFlow Graph

When you save a TensorFlow model, you're essentially capturing the entire computation graph along with the model's variables. This allows you to reload the graph structure and the learned parameters later on. Here's a step-by-step explanation on how to save a TensorFlow graph:

Using the SavedModel Format

The SavedModel format is the recommended method for saving models in TensorFlow. It captures the entire computation graph, including operations, variables, collections, and meta-graphs. Here's how to use it:

python
1import tensorflow as tf
2
3# Define a simple computation graph
4x = tf.constant(2.0)
5y = tf.constant(3.0)
6result = x * y
7
8# Create a session
9with tf.compat.v1.Session() as sess:
10    # Save the model
11    builder = tf.compat.v1.saved_model.builder.SavedModelBuilder('saved_model_dir')
12
13    # Mark the current session's graph as an asset
14    builder.add_meta_graph_and_variables(sess, [tf.saved_model.SERVING])
15    
16    # Save the model
17    builder.save()

Key Attributes of SavedModel Format

  • Model Variants: Supports different variants of a model.
  • Language Agnostic: Save and load across various environments.
  • Deployment-ready: Suitable for TensorFlow Serving.

Using Checkpoints

While SavedModel is comprehensive, checkpoints are another option for saving a model's weights:

python
1# Define model
2model = tf.keras.Sequential([
3    tf.keras.layers.Dense(10, activation='relu', input_shape=(784,)),
4    tf.keras.layers.Dense(10, activation='softmax')
5])
6
7# Compile model
8model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
9
10# Save checkpoints during training
11checkpoint_path = "training_1/cp.ckpt"
12checkpoint_dir = os.path.dirname(checkpoint_path)
13
14# Create a callback that saves the model's weights
15cp_callback = tf.keras.callbacks.ModelCheckpoint(filepath=checkpoint_path, save_weights_only=True, verbose=1)
16
17# Train the model with the new callback
18model.fit(training_data, training_labels, epochs=10, callbacks=[cp_callback])

Loading a TensorFlow Graph

Loading a pre-saved graph allows for further operations such as fine-tuning or inference. Here's how to restore both SavedModel and checkpoint weights.

Loading from SavedModel

Loading from a SavedModel involves restoring the graph in a new session, which can then be used for inference:

python
1# Load the model
2model = tf.compat.v1.saved_model.load_v2('saved_model_dir')
3
4# Use with eager execution/graph mode
5x_val = tf.constant(2.0)
6y_val = tf.constant(3.0)
7result = model(x_val * y_val)

Loading from a Checkpoint

To restore model weights from a checkpoint:

python
1# Ensure to have the same model architecture
2model = tf.keras.Sequential([
3    tf.keras.layers.Dense(10, activation='relu', input_shape=(784,)),
4    tf.keras.layers.Dense(10, activation='softmax')
5])
6
7# Restore the weights
8model.load_weights(checkpoint_path)

Comparison Table

Below is a comparison of the key features of different TensorFlow saving techniques:

FeatureSavedModelCheckpoint
Graph & VariablesYesOnly variables
Optimizer InformationYesNo
Architecture AgnosticYesNo (requires architecture)
TensorFlow Version CompatibilityHigher version compatibilityRequires same architecture
Deployment-ReadyYesNo
Use CasesInference & Deployment Multi-platform supportTraining Intermediate checkpoints

Additional Details

Fine-Tuning Models

Fine-tuning is a method commonly used when pre-trained models require adjustments:

  • Ensure the base architecture matches the pre-trained model.
  • Load pre-trained weights via checkpoints or SavedModel.
  • Continue training with a reduced learning rate.

Considerations

  • Version Compatibility: Ensure to match the TensorFlow version used to save and load models.
  • Custom Layers: If your model includes custom layers, these must be explicitly defined before loading.

Graph Def

GraphDef is a serialized representation of a TensorFlow computation graph. While not recommended for regular usage today due to its complexity and lack of comprehensive information compared to SavedModel, it is occasionally useful for debugging:

python
1# Save the graph definition protobuf
2with tf.compat.v1.Session() as sess:
3    tf.io.write_graph(sess.graph_def, '.', 'graph.pb', as_text=False)
4
5# Load the graph
6with tf.io.gfile.GFile('graph.pb', 'rb') as f:
7    graph_def = tf.compat.v1.GraphDef()
8    graph_def.ParseFromString(f.read())
9
10# Import the graph
11with tf.compat.v1.Session() as sess:
12    tf.import_graph_def(graph_def)

This article has explored various methods to save and load TensorFlow graphs, providing insights and examples that can be employed in your machine learning tasks. Leveraging these tools effectively can streamline processes for training and deploying robust models efficiently.


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.