Export the model as an artifact for inference.
Source:R/model-persistence.R
export_savedmodel.keras.src.models.model.Model.RdThis method lets you export a model to a lightweight artifact that contains
the model's forward pass only (its call() method). For TensorFlow
SavedModel artifacts, the forward pass is registered under the name
serve() and can be served via, for example, TF-Serving.
The original code of the model (including any custom layers you may have used) is no longer necessary to reload the artifact – it is entirely standalone.
Note: This feature is currently supported only with TensorFlow, JAX and Torch backends.
Note: Be aware that the exported artifact may contain information
from the local file system when using format = "onnx", verbose = TRUE
and Torch backend.
Usage
# S3 method for class 'keras.src.models.model.Model'
export_savedmodel(
object,
export_dir_base,
...,
format = "tf_saved_model",
verbose = NULL,
input_signature = NULL
)Arguments
- object
A keras model.
- export_dir_base
string, file path where to save the artifact.
- ...
Additional backend- or format-specific export options:
is_static: Optional boolean specific to the JAX backend andformat = "tf_saved_model". Indicates whetherfnis static. Set toFALSEiffninvolves state updates, such as RNG seeds and counters.jax2tf_kwargs: Optional dictionary specific to the JAX backend andformat = "tf_saved_model". Arguments forjax2tf.convert. Ifnative_serializationandpolymorphic_shapesare not provided, they are computed automatically.opset_version: Optional integer specific toformat = "onnx"that specifies the ONNX opset version.LiteRT-specific options. With the TensorFlow backend these are passed to the TensorFlow Lite converter and include
optimizations,representative_dataset,experimental_new_quantizer,allow_custom_ops, andenable_select_tf_ops. With the PyTorch backend, options includeoptimizationsand installedlitert_torch.convert()keyword arguments such asstrict_export,dynamic_shapes,lightweight_conversion,enable_x64,runtime_constant_folding, andquant_config.PyTorch export options specific to
format = "torch", passed totorch.export.export, includingstrict,dynamic_shapes,prefer_deferred_runtime_asserts_over_guards, andpreserve_module_call_signature.
- format
string. The export format. Supported values:
"tf_saved_model","onnx","openvino","litert", and"torch". Defaults to"tf_saved_model".- verbose
Bool. Whether to print a message during export. Defaults to
NULL, which uses the default value set by different backends and formats.- input_signature
Optional. Specifies the shape and dtype of the model inputs. Can be a structure of
keras.InputSpec,tf.TensorSpec,backend.KerasTensor, or backend tensor. If not provided, it will be automatically computed. Defaults toNULL. Withformat = "litert"and the PyTorch backend, dynamic input shapes are not supported. Any dynamic dimensions are automatically replaced with1, which may cause runtime failures for other shapes. Explicitly pass a fixed staticinput_signaturematching the maximum runtime shape and pad inputs to that shape at runtime.
Value
This is called primarily for the side effect of exporting object.
The first argument, object is also returned, invisibly, to enable usage
with the pipe.
Examples
# Create the artifact
model |> tensorflow::export_savedmodel("path/to/location")
# Later, in a different process/environment...
library(tensorflow)
reloaded_artifact <- tf$saved_model$load("path/to/location")
predictions <- reloaded_artifact$serve(input_data)
# see tfdeploy::serve_savedmodel() for serving a model over a local web api.Here's how to export an ONNX for inference.
# Export the model as a ONNX artifact
model |> export_savedmodel("path/to/location", format = "onnx")
# Load the artifact in a different process/environment
onnxruntime <- reticulate::import("onnxruntime")
ort_session <- onnxruntime$InferenceSession("path/to/location")
input_data <- list(....)
names(input_data) <- sapply(ort_session$get_inputs(), `[[`, "name")
predictions <- ort_session$run(NULL, input_data)Here's how to export a LiteRT (TFLite) artifact for inference.
model |> export_savedmodel("path/to/model.tflite", format = "litert")
tf <- reticulate::import("tensorflow")
interpreter <- tf$lite$Interpreter(model_path = "path/to/model.tflite")
interpreter$allocate_tensors()
interpreter$set_tensor(interpreter$get_input_details()[[1]]$index, input_data)
interpreter$invoke()
output_data <- interpreter$get_tensor(
interpreter$get_output_details()[[1]]$index
)Here's how to export a PyTorch ExportedProgram for inference.
# Export the model as a PyTorch ExportedProgram
model |> export_savedmodel("path/to/model.pt2", format = "torch")
# Load the artifact in a different process/environment
torch <- reticulate::import("torch")
loaded_program <- torch$export$load("path/to/model.pt2")
predictions <- loaded_program$module()(input_tensor)See also
Other saving and loading functions: layer_tfsm() load_model() load_model_weights() register_keras_serializable() save_model() save_model_config() save_model_weights() with_custom_object_scope()