Using the SavedModel format in Tensorflow
Last Updated :
21 Mar, 2023
TensorFlow is a popular deep-learning framework that provides a variety of tools to help users build, train, and deploy machine-learning models. One of the most important aspects of deploying a machine learning model is saving and exporting it to a format that can be easily used by other programs and systems.
The SavedModel format is a standard format for storing and sharing machine learning models that have been trained using TensorFlow. In this article, we will discuss how to use the SavedModel format in TensorFlow, including how to save and export a model, and how to load and use a saved model in a new program. To save a TensorFlow model in the SavedModel format, you need to use the tf.saved_model.save() function. This function takes two arguments: the model to be saved, and the path to the directory where the SavedModel should be stored. The directory should not exist already and will be created by the function.
To load a SavedModel in a new program, you need to use the tf.saved_model.load() function. This function takes a single argument, which is the path to the directory containing the SavedModel.
Syntax:
tf.saved_model.save( obj, export_dir, signatures=None, options=None)
Parameters:
obj : A trackable object (e.g. tf.Module or tf.train.Checkpoint) to export.
export_dir : A directory in which to write the SavedModel.
signatures : Optional, one of the following three types: * a tf.function instance with an input signature specified, which will use the default serving signature key; * the outcome of calling f.get concrete function on a @tf.function-decorated function; in this case, f will be used to generate a signature for the SavedModel under the default serving signature key; or * a dictionary, which maps signature keys to either tf.function instances with input signatures or concrete These dictionaries' keys could contain any text, although they usually come from the tf.saved model.signature constants module.
options : tf.saved_model.SaveOptions object for configuring save options.
Syntax of tf.saved_model.load():
tf.saved_model.load( export_dir, tags=None, options=None)
Parameters:
export_dir : The SavedModel directory to load from.
tags: A tag or sequence of tags identifying the MetaGraph to load. Optional if the SavedModel contains a single MetaGraph, as for those exported from tf.saved_model.save.
options: tf.saved_model.LoadOptions object that specifies options for loading.
Let's see an example to understand how it works.
Example:
First, we define a simple Keras model with a single dense layer. This model takes an input tensor of shape (2,) and produces an output tensor of shape (1,). We then train the model using some dummy data.
Python3
# import packages
import tensorflow as tf
# define a simple model
def build_model():
inputs = tf.keras.Input(shape=(2,), name='inputs')
x = tf.keras.layers.Dense(4, activation='relu', name='dense1')(inputs)
outputs = tf.keras.layers.Dense(1, name='outputs')(x)
model = tf.keras.Model(inputs=inputs, outputs=outputs)
return model
Once the model is trained, we save it in the SavedModel format using the tf.saved_model.save() function. This function takes the trained model and a destination folder as input, and it saves the model in the SavedModel format in the specified folder.
Python3
# create an instance of the model and save it in SavedModel format
model = build_model()
model.save('saved_model')
A directory named saved_model gets created and the contents are as below:
saved_model directory
Next, we load the SavedModel using the tf.saved_model.load() function. This function takes the path to the folder containing the SavedModel as input, and it returns a tf.saved_model.load() object that contains the loaded model.
Python3
# load the SavedModel
loaded_model = tf.saved_model.load('saved_model')
To perform inference with the loaded model, we retrieve the model's prediction function by calling the signatures attribute of the tf.saved_model.load() object, which returns a dictionary of signature keys and associated functions. We use the 'serving_default' key to retrieve the default prediction function, which we assign to the variable inference_func. Finally, we create a test input tensor and use the prediction function to obtain a prediction for the test input. The output of the prediction function is a dictionary with the model's output tensor as the value associated with the key 'output_1', so we extract the output value using indexing.
Python3
# create some test data
test_input = tf.constant([[0.5, 0.5], [0.2, 0.2]])
# call the model on the test data
result = loaded_model.signatures['serving_default'](inputs=test_input)
print(result)
Output:
{'outputs': <tf.Tensor: shape=(2, 1), dtype=float32, numpy=
array([[-0.41576898],
[-0.1663076 ]], dtype=float32)>}
Full code for SavedModel format in Tensorflow
Python3
# import packages
import tensorflow as tf
# define a simple model
def build_model():
inputs = tf.keras.Input(shape=(2,), name='inputs')
x = tf.keras.layers.Dense(4, activation='relu', name='dense1')(inputs)
outputs = tf.keras.layers.Dense(1, name='outputs')(x)
model = tf.keras.Model(inputs=inputs, outputs=outputs)
return model
# create an instance of the model and save it in SavedModel format
model = build_model()
model.save('saved_model')
# load the SavedModel
loaded_model = tf.saved_model.load('saved_model')
# create some test data
test_input = tf.constant([[0.5, 0.5], [0.2, 0.2]])
# call the model on the test data
result = loaded_model.signatures['serving_default'](inputs=test_input)
print(result)
Output:
{'outputs': <tf.Tensor: shape=(2, 1), dtype=float32, numpy=
array([[-0.3197009 ],
[-0.12788038]], dtype=float32)>}
Similar Reads
Export a SavedModel in Tensorflow
In TensorFlow, a SavedModel is basically a serialized format for storing a complete TensorFlow program. The tf.saved_model.save() function in TensorFlow can be used to export a SavedModel. A trained model and its related variables are saved to disc in the SavedModel format by this function. It inclu
4 min read
Save and Load Models using TensorFlow in Json?
If you are looking to explore Machine Learning with TensorFlow, you are at the right place. This comprehensive article explains how to save and load the models in TensorFlow along with its brief overview. If you read this article till the end, you will not need to look for further guides on how to s
6 min read
Using bfloat16 with TensorFlow models in Python
In this article, we will discuss bfloat16 (Brain Floating Point 16) in Python. It is a numerical format that occupies 16 bits in memory and is used to represent floating-point numbers. It is similar to the more commonly used 32-bit single-precision float (float32) and 64-bit double-precision float (
2 min read
Tensorflow.js tf.model() Function
Tensorflow.js is an open-source library that is developed by Google for running machine learning models as well as deep learning neural networks in the browser or node environment. The tf.model() function is used to create a model which contains layers and layers that are provided in form of input a
2 min read
Tensorflow.js tf.io.moveModel() Function
Tensorflow.js is an open-source library that is developed by Google for running machine learning models as well as deep learning neural networks in the browser or node environment. The .moveModel() function is used to move a model away from one URL towards a new one. Moreover, this method favors mov
3 min read
Tensorflow.js tf.io.removeModel() Function
Tensorflow.js is an open-source library that is developed by Google for running machine learning models as well as deep learning neural networks in the browser or node environment. The .removeModel() function is used to remove a stated model by means of a URL provided from a recorded repository medi
3 min read
Tensorflow.js tf.loadGraphModel() Function
Tensorflow.js is an open-source library developed by Google for running machine learning models as well as deep learning neural networks in the browser or node environment. The .loadGraphModel() function is used to Load a graph model given a URL to the model definition. Syntax: tf.loadGraphModel (mo
2 min read
model.evaluate() in TensorFlow
The model.evaluate() function in TensorFlow is used to evaluate a trained model on a given dataset. It returns the loss value and any additional metrics specified during model compilation. model.evaluate() function allows us to assess how well the trained model generalizes to unseen data.Syntax of m
2 min read
Tensorflow.js tf.loadLayersModel() Function
Tensorflow.js is a Google-developed open-source toolkit for executing machine learning models and deep learning neural networks in the browser or on the node platform. It also enables developers to create machine learning models in JavaScript and utilize them directly in the browser or with Node.js.
2 min read
Random number generation using TensorFlow
In the field of Machine Learning, Random numbers generation plays an important role by providing stochasticity essential for model training, initialization, and augmentation. We have TensorFlow, a powerful open-source machine learning library, that contains tf.random module. This module helps us for
6 min read