You are now following this Submission
- You will see updates in your followed content feed
- You may receive emails, depending on your communication preferences
Overview
The Deep Learning Toolbox™ Converter for TensorFlow™ Models support package allows you to import pretrained TensorFlow and Keras models into MATLAB®, and to export MATLAB networks to TensorFlow.
Import
For TensorFlow versions 2.16 and later
Import a Keras 3 model using importNetworkFromKeras (introduced in R2026a)
Starting with TensorFlow 2.16, Keras 3 is the recommended high-level model API for TensorFlow. To import a pretrained Keras 3 model and weights into MATLAB, use the importNetworkFromKeras function
Save Keras 3 models using matlabsaver.py
Before using importNetworkFromKeras to import your pretrained Keras 3 model, you must save the model using the matlabsaver.py utility provided with the Deep Learning Toolbox™ Converter for TensorFlow™ Models.
Copy the matlabsaver.py file to the directory where the Keras 3 model is trained.
matlabsaverPath = which("matlabsaver.py");
copyfile(matlabsaverPath, <Keras 3 model directory>);
Save the trained Keras 3 model in Python®.
import matlabsaver
...
matlabsaver.save_for_matlab(trainedModel, "modelFolder")
(Alternative Functionality) Import a TensorFlow-Keras model using importNetworkFromTensorFlow
If you are using TensorFlow versions 2.15 and earlier or can downgrade to Keras 2, use the importNetworkFromTensorFlow function. This will result in more layers imported as MATLAB layers capable of code generation. The importNetworkFromTensorFlow function accepts TensorFlow models in the SavedModel format.
First install tf_keras and configure legacy Keras support:
import os
os.environ["TF_USE_LEGACY_KERAS"] = "1"
import tensorflow
Then import as tf_keras:
import tf_keras as keras
from tf_keras import layers
Verify the Keras version and save the model:
model.save("modelName")
SavedModels saved with versions 2.16 and higher may also work without issues, but Keras 3-specific features remain unsupported by importNetworkFromTensorFlow.
For TensorFlow versions 2.15 and earlier
To import a pretrained TensorFlow model and weights into MATLAB, use the importNetworkFromTensorFlow function. The importNetworkFromTensorFlow function accepts TensorFlow models in the SavedModel format. Save the model with the following Python command:
model.save("modelName")
Export
Export supports:
- TensorFlow v2.0 or later, no support for Keras 3.0
- Python version 3.0 or later
MATLAB Release Compatibility
- Compatible with R2017b to R2026b
Platform Compatibility
- Windows
- macOS (Apple Silicon)
- macOS (Intel)
- Linux