TensorFlow - Exporting
TensorFlow - Exporting Models
Section titled “TensorFlow - Exporting Models”Exporting a trained TensorFlow model is a crucial step for deploying it in various environments, such as servers, mobile devices, or web browsers. TensorFlow’s standard deployment format is the SavedModel. A SavedModel contains the complete TensorFlow program, including the computation graph, learned weights (variables), and any assets required by the model.
For Keras models, which are commonly used in TensorFlow, the model.save() method is the primary way to export a model in the SavedModel format. You can also save in the older Keras H5 format, but SavedModel is generally preferred for broader compatibility within the TensorFlow ecosystem.
Here’s a basic example of how to save a Keras model and then load it back:
import tensorflow as tfimport numpy as np
# 1. Define and compile a simple Keras model (example)model = tf.keras.Sequential([ tf.keras.layers.Dense(128, activation='relu', input_shape=(784,)), tf.keras.layers.Dropout(0.2), tf.keras.layers.Dense(10, activation='softmax')])
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
# (Imagine the model is trained here with some data)# Example dummy training:# x_dummy = np.random.rand(100, 784)# y_dummy = np.random.randint(0, 10, 100)# model.fit(x_dummy, y_dummy, epochs=1)
# 2. Save the model to a directory in SavedModel formatsaved_model_path = './my_simple_model'model.save(saved_model_path)print(f"Model saved to {saved_model_path}")
# 3. Load the model from the SavedModel directoryloaded_model = tf.keras.models.load_model(saved_model_path)print("Model loaded successfully.")
# Verify the loaded model (optional)# loaded_model.summary()# You can now use loaded_model for inference, e.g., loaded_model.predict(...)
# For non-Keras models or custom TensorFlow modules/functions,# you can use `tf.saved_model.save()` and `tf.saved_model.load()`.# Example:# tf.saved_model.save(your_tf_module, './my_custom_tf_module_saved_model')# loaded_custom_module = tf.saved_model.load('./my_custom_tf_module_saved_model')Once a model is in SavedModel format, it can be:
- Served using TensorFlow Serving for production environments.
- Converted to TensorFlow Lite (
.tflite) format for deployment on mobile and embedded devices. This often involves optimizations like quantization to reduce model size and improve inference speed. - Used with TensorFlow.js to run models directly in web browsers or Node.js applications.
- Deployed on various cloud platforms and edge devices that support TensorFlow models.
For further information on model saving and serialization, refer to the official TensorFlow documentation: https://www.tensorflow.org/guide/saved_model