tf.contrib.saved_model.save_keras_model( model, saved_model_path, custom_objects=None, as_text=None, input_signature=None, serving_only=False )
tf.keras.Model into Tensorflow SavedModel format.
save_model generates new files/folders under the
1) a checkpoint containing the model weights.
2) a saved_model.pb file containing the model's MetaGraphs. The prediction
graph is always exported. The evaluaton and training graphs are exported
if the following conditions are met:
- Evaluation: model loss is defined.
- Training: model is compiled with an optimizer defined under
This is because
tf.keras.optimizers.Optimizer instances cannot be
saved to checkpoints.
3) Model's json configuration, if model.get_config() has been implemented.
This file can be used to reload the model using
tf.keras.models.model_from_json(). Note that if any custom objects were
used, they should be passed to the
custom_object argument when loading
- Sequential and functional models can always be saved.
- Subclassed models can only be saved when
serving_only=True. This is due to
the current implementation copying the model in order to export the training
and evaluation graphs. Because the topology of subclassed models cannot be
determined, the subclassed models cannot be cloned. Subclassed models will
be entirely exportable in the future.
Note that each mode is exported in separate graphs, so different modes do not share variables. To use the train graph with evaluation or prediction graphs, create a new checkpoint if variable values have been updated.
import tensorflow as tf # Create a tf.keras model. model = tf.keras.Sequential() model.add(tf.keras.layers.Dense(1, input_shape=)) model.summary() # Save the tf.keras model in the SavedModel format. saved_to_path = tf.contrib.saved_model.save_keras_model( model, '/tmp/my_simple_tf_keras_saved_model') # Load the saved keras model back. model_prime = tf.contrib.saved_model.load_keras_model(saved_to_path) model_prime.summary()
tf.keras.Modelto be saved. If the model is subclassed, the flag
serving_onlymust be set to True.
saved_model_path: a string specifying the path to the SavedModel directory. The SavedModel will be saved to a timestamped folder created within this directory.
custom_objects: Optional dictionary mapping string names to custom classes or functions (e.g. custom loss functions).
as_text: whether to write the
SavedModelproto in text format. Currently unavailable in serving-only mode.
input_signature: A possibly nested sequence of
tf.TensorSpecobjects, used to specify the expected model inputs.
input_signature's nested structure should match the expected nested structure of the inputs to the model. If this is not set, this function will attempt to infer the input shapes and dtypes from the model. Note that if the model is subclassed, the tensor inputs to the call function should be nested in the first argument (this is a general requirement for using subclassed models with Keras functions .fit(), .predict(), etc.).
serving_only: Export only the outputs produced from calling the model in predict mode. The losses, optimizer, and other training configurations are not saved. If the SavedModel will only be used for serving (rather than retraining), or if the model is subclassed, this can be set to True.
String path to the SavedModel folder, a subdirectory of
NotImplementedError: If the model is a subclassed model, and serving_only is False.
ValueError: If the input signature cannot be inferred from the model.