exportNetworkToTensorFlow
Description
exportNetworkToTensorFlow(
exports the MATLAB® deep learning network net,modelPackage)net and saves it as a TensorFlow™ model in the Python® package modelPackage. For information on how to load the
TensorFlow model in Python, see Load Exported TensorFlow Model.
The exportNetworkToTensorFlow function requires the Deep Learning Toolbox™ Converter for TensorFlow Models. If this support package is not installed, then
exportNetworkToTensorFlow provides a download link.
If the MATLAB network contains a custom or built-in MATLAB layer that exportNetworkToTensorFlow cannot convert to a TensorFlow layer, the exportNetworkToTensorFlow function exports this layer as a
custom TensorFlow layer. For more information on which MATLAB layers exportNetworkToTensorFlow can convert to TensorFlow layers, see Layers Supported for Exporting to TensorFlow. For an example, see Export Network with Custom Layer to TensorFlow.
Examples
Save a MATLAB deep learning network as a TensorFlow model by using the exportNetworkToTensorFlow function.
Download and install the Deep Learning Toolbox Converter for TensorFlow Models support package. You can enter exportNetworkToTensorFlow at the command prompt to check whether the support package is installed. If the support package is not installed, then the function provides a link to the required support package in the Add-On Explorer. To install the support package, click the link, and then click Install.
Load the pretrained squeezenet convolutional neural network as a dlnetwork object.
net = imagePretrainedNetwork("squeezenet")net =
dlnetwork with properties:
Layers: [68×1 nnet.cnn.layer.Layer]
Connections: [75×2 table]
Learnables: [52×3 table]
State: [0×3 table]
InputNames: {'data'}
OutputNames: {'prob_flatten'}
Initialized: 1
View summary with summary.
Export the network net to TensorFlow. The exportNetworkToTensorFlow function saves the TensorFlow model in the Python package myModel.
exportNetworkToTensorFlow(net,"myModel")Run this code in Python to load the exported TensorFlow model from the myModel package.
import myModel model = myModel.load_model()
Save the exported model in the TensorFlow SavedModel format. Saving model in SavedModel format is optional. You can perform deep learning workflows directly with model. For an example that shows how to classify an image with the exported TensorFlow model, see Export Network to TensorFlow and Classify Image.
model.save("myModelTF")
Use a MATLAB network to classify an image. Save the network as a TensorFlow model and use the TensorFlow model to classify the same image.
Classify Image in MATLAB
Load the pretrained squeezenet convolutional network as a dlnetwork object and display the network properties.
[net,ClassNames] = imagePretrainedNetwork("squeezenet");
netnet =
dlnetwork with properties:
Layers: [68×1 nnet.cnn.layer.Layer]
Connections: [75×2 table]
Learnables: [52×3 table]
State: [0×3 table]
InputNames: {'data'}
OutputNames: {'prob_flatten'}
Initialized: 1
View summary with summary.
Read the image you want to classify. Resize the image to the input size of the network.
Im = imread("peppers.png");
InputSize = net.Layers(1).InputSize;
Im = imresize(Im,InputSize(1:2));Predict class labels and classification scores.
score = predict(net,single(Im)); label = scores2label(score,ClassNames);
Show the image with the classification label.
imshow(Im) title(ClassNames(label),FontSize=12)

Export Network and Image Data
Export the network net to TensorFlow. The exportNetworkToTensorFlow function saves the TensorFlow model in the Python package myModel.
exportNetworkToTensorFlow(net,"myModel")Permute the 2-D image data from the Deep Learning Toolbox™ ordering (HWCN) to the TensorFlow ordering (NHWC), where H, W, and C are the height, width, and number of channels of the image, respectively, and N is the number of images. Save the image in a MAT file.
ImTF = permute(Im,[4,1,2,3]); filename = "peppers.mat"; save(filename,"ImTF")
Classify Image with Exported TensorFlow Model
Run this code in Python to load the exported TensorFlow model and use the model for image classification.
Load the exported model from the Python package myModel.
import myModel model = myModel.load_model()
Classify the image with the exported model. For more information on how to compare prediction results between MATLAB and TensorFlow, see Inference Comparison Between TensorFlow and Imported Networks for Image Classification.
score_tf = model.predict(ImTF)
Export a network, which contains a MATLAB custom layer, to TensorFlow.
Create Network
Create a SReLU layer by defining the custom layer sreluLayer. Display the definition of the custom layer.
type sreluLayer.mclassdef sreluLayer < nnet.layer.Layer ...
& nnet.layer.Acceleratable
% Example custom SReLU layer.
properties (Learnable)
% Layer learnable parameters.
LeftSlope
RightSlope
LeftThreshold
RightThreshold
end
methods
function layer = sreluLayer(args)
% layer = sreluLayer creates a SReLU layer.
%
% layer = sreluLayer(Name=name) also specifies the layer name.
arguments
args.Name = "";
end
% Set layer name.
layer.Name = args.Name;
% Set layer description.
layer.Description = "SReLU";
end
function layer = initialize(layer,layout)
% layer = initialize(layer,layout) initializes the learnable
% parameters of the layer for the specified input layout.
% Find number of channels.
idx = finddim(layout,"C");
numChannels = layout.Size(idx);
% Initialize empty learnable parameters.
sz = ones(1,numel(layout.Size));
sz(idx) = numChannels;
if isempty(layer.LeftSlope)
layer.LeftSlope = rand(sz);
end
if isempty(layer.RightSlope)
layer.RightSlope = rand(sz);
end
if isempty(layer.LeftThreshold)
layer.LeftThreshold = rand(sz);
end
if isempty(layer.RightThreshold)
layer.RightThreshold = rand(sz);
end
end
function Y = predict(layer, X)
% Y = predict(layer, X) forwards the input data X through the
% layer and outputs the result Y.
tl = layer.LeftThreshold;
al = layer.LeftSlope;
tr = layer.RightThreshold;
ar = layer.RightSlope;
Y = (X <= tl) .* (tl + al.*(X-tl)) ...
+ ((tl < X) & (X < tr)) .* X ...
+ (tr <= X) .* (tr + ar.*(X-tr));
end
end
end
Create a network.
layers = [
imageInputLayer([31 53 3],Name="image",Normalization="none")
sreluLayer(Name="srelu")];
net = dlnetwork(layers);Export Network to TensorFlow
Export the network net to TensorFlow. The exportNetworkToTensorFlow function saves the TensorFlow model in the Python package myModel and the definition of the custom layer in the customLayers folder of the myModel package.
exportNetworkToTensorFlow(net,"myModel")Warning: Layer "srelu": Layer class "sreluLayer" was exported into an incomplete TensorFlow custom layer file. The custom layer definition must be completed or the file must be replaced before the model can be loaded into TensorFlow.
Display the definition of the TensorFlow custom layer sreluLayer.py.
type ./myModel/customLayers/sreluLayer.py# This file was created by
# MATLAB Deep Learning Toolbox Converter for TensorFlow Models.
# 09-Aug-2025 13:43:44
import tensorflow as tf
import sys # Remove this line after completing the layer definition.
class sreluLayer(tf.keras.layers.Layer):
# Add any additional layer hyperparameters to the constructor's
# argument list below.
def __init__(self, LeftSlope_Shape_=None, RightSlope_Shape_=None, LeftThreshold_Shape_=None, RightThreshold_Shape_=None, name=None):
super(sreluLayer, self).__init__(name=name)
# Learnable parameters: These have been exported from MATLAB and will be loaded automatically from the weight file:
self.LeftSlope = tf.Variable(name="LeftSlope", initial_value=tf.zeros(LeftSlope_Shape_), trainable=True)
self.RightSlope = tf.Variable(name="RightSlope", initial_value=tf.zeros(RightSlope_Shape_), trainable=True)
self.LeftThreshold = tf.Variable(name="LeftThreshold", initial_value=tf.zeros(LeftThreshold_Shape_), trainable=True)
self.RightThreshold = tf.Variable(name="RightThreshold", initial_value=tf.zeros(RightThreshold_Shape_), trainable=True)
def call(self, input1):
# Add code to implement the layer's forward pass here.
# The input tensor format(s) are: BSSC
# The output tensor format(s) are: BSSC
# where B=batch, C=channels, T=time, S=spatial(in order of height, width, depth,...)
# Remove the following 3 lines after completing the custom layer definition:
print("Warning: load_model(): Before you can load the model, you must complete the definition of custom layer sreluLayer in the customLayers folder.")
print("Exiting...")
sys.exit("See the warning message above.")
return output1
Load Exported Network
This section describes the steps that you must perform in Python to load the exported TensorFlow model.
Edit the definition of sreluLayer.py by implementing the forward computation in call.
def call(self, input1):
al = self.LeftSlope;
ar = self.RightSlope;
tl = self.LeftThreshold;
tr = self.RightThreshold;
output1 = tf.where(input1 <= tl, tl + al*(input1-tl), 0.0) + \
tf.where(((tl < input1) & (input1 < tr)), input1, 0.0) + \
tf.where((tr <= input1), tr + ar*(input1-tr), 0.0)
return output1
Delete the lines in sreluLayer.py, as instructed by the comments in the file. View the updated custom layer sreluLayer.py.
import tensorflow as tf
class sreluLayer(tf.keras.layers.Layer):
# Add any additional layer hyperparameters to the constructor's
# argument list below.
def __init__(self, LeftSlope_Shape_=None, RightSlope_Shape_=None, LeftThreshold_Shape_=None, RightThreshold_Shape_=None, name=None):
super(sreluLayer, self).__init__(name=name)
# Learnable parameters: These have been exported from MATLAB and will be loaded automatically from the weight file:
self.LeftSlope = tf.Variable(name="LeftSlope", initial_value=tf.zeros(LeftSlope_Shape_), trainable=True)
self.RightSlope = tf.Variable(name="RightSlope", initial_value=tf.zeros(RightSlope_Shape_), trainable=True)
self.LeftThreshold = tf.Variable(name="LeftThreshold", initial_value=tf.zeros(LeftThreshold_Shape_), trainable=True)
self.RightThreshold = tf.Variable(name="RightThreshold", initial_value=tf.zeros(RightThreshold_Shape_), trainable=True)
def call(self, input1):
al = self.LeftSlope;
ar = self.RightSlope;
tl = self.LeftThreshold;
tr = self.RightThreshold;
output1 = tf.where(input1 <= tl, tl + al*(input1-tl), 0.0) + \
tf.where(((tl < input1) & (input1 < tr)), input1, 0.0) + \
tf.where((tr <= input1), tr + ar*(input1-tr), 0.0)
return output1
In this example, you only have to edit sreluLayer.py. In other cases, you might have to edit model.py to pass arguments to custom layer calls.
Before loading the model, you might have to restart your Python kernel for the changes to take effect. Load the model from the Python package myModel.
import myModel model = myModel.load_model()
Input Arguments
Deep Learning Toolbox network, specified as a dlnetwork object.
You can get a trained network in these ways:
Load a pretrained network by using the
imagePretrainedNetworkfunction.Download a pretrained network from the MATLAB Deep Learning Model Hub.
Train a
dlnetworkobject by using thetrainnetfunction or a custom training loop.
You can also export an initialized dlnetwork object to TensorFlow.
Name of the Python package containing the exported TensorFlow model, specified as a string scalar or character vector. The
modelPackage package contains:
The
_init_.pyfile, which defines themodelPackagefolder as a regular Python package.The
model.pyfile, which contains the code that defines the untrained TensorFlow-Keras model.The
README.txtfile, which provides instructions on how to load the TensorFlow model and save it inHDF5orSavedModelformat. For more details, see Load Exported TensorFlow Model and Save Exported TensorFlow Model.The
weights.h5file, which contains the model weights inHDF5format.The
customLayersfolder, which contains one file for each exported custom layer. Each file is an incomplete definition of a TensorFlow custom layer. You must edit or replace each of these files before you can load the model in Python. The software creates thecustomLayersfolder only when the MATLAB network contains a custom or built-in MATLAB layer thatexportNetworkToTensorFlowcannot convert to a TensorFlow layer.
Example: "myModel"
Limitations
To load an exported TensorFlow model, you must have:
TensorFlow version r2.0 or later
Python version 3.0 or later
The TensorFlow module
tfafor a MATLAB network that contains one or more of the following layers:groupNormalizationLayerinstanceNormalizationLayerlayerNormalizationLayerwithOperationDimensionset to"batch-excluded"
More About
The exportNetworkToTensorFlow function supports these Deep Learning Toolbox layers for export as TensorFlow layers.
Deep Learning Toolbox Layers
Convolution and Fully Connected Layers convolution1dLayerconvolution2dLayerconvolution3dLayergroupedConvolution2dLayerfullyConnectedLayertransposedConv2dLayertransposedConv3dLayer*
exportNetworkToTensorFlowexportsgruProjectedLayerandlstmProjectedLayerobjects to TensorFlow as standard GRU and LSTM layers, respectively. That is, the function exports the full-rank learnable matrices, not the factored lower-rank matrices. This behavior does not change the numerical output of the layer.Activation Layers clippedReluLayereluLayergeluLayerleakyReluLayerpreluLayerreluLayerswishLayertanhLayerUtility Layers identityLayerComputer Vision Toolbox™ Layers
patchEmbeddingLayer(Computer Vision Toolbox)Image Processing Toolbox™ Layers
depthToSpace2dLayer(Image Processing Toolbox)resize2dLayer(Image Processing Toolbox)resize3dLayer(Image Processing Toolbox)spaceToDepthLayer(Image Processing Toolbox)Lidar Toolbox™ Layers
pointCloudInputLayer(Lidar Toolbox)Text Analytics Toolbox™ Layers
wordEmbeddingLayer(Text Analytics Toolbox)
This section describes how to load a TensorFlow model in Python from the package modelPackage, which the
exportNetworkToTensorFlow creates. For an example, see Export Network to TensorFlow.
exportNetworkToTensorFlow exports models that are compatible with Keras 2. From
TensorFlow versions 2.16 and later, Keras 2 is not installed with TensorFlow by default. Therefore, to use exported models with TensorFlow versions 2.16 and later, run the following commands before loading the
exported model:
First, install the Keras 2 package.
pip install tf_keras
Then add the following lines of code at the beginning of the
model.pyand__init__.pyfiles inside the generatedmodelPackagePython package.import os os.environ["TF_USE_LEGACY_KERAS"] = "1"
To load the exported TensorFlow model with weights, run the following commands.
import modelPackage model = modelPackage.load_model()To load the model without weights, run the following commands.
import modelPackage model = modelPackage.load_model(load_weights=False)
Note
For TensorFlow versions earlier than 2.16, you can skip steps 1 and 2. You only need to perform step 3 to load the exported model.
Optionally, you can save the exported TensorFlow model in SavedModel format. You must first load the
exported TensorFlow model by following the instructions in Load Exported TensorFlow Model. For an example that shows
how to save an exported model to SavedModel format, see Export Network to TensorFlow.
Save the loaded TensorFlow model in SavedModel format.
model.save("modelName")Tips
MATLAB uses one-based indexing, whereas Python uses zero-based indexing. In other words, the first element in an array has an index of 1 and 0 in MATLAB and Python, respectively. For more information about MATLAB indexing, see Array Indexing. In MATLAB, to use an array of indices (
ind) created in Python, convert the array toind+1.
Version History
Introduced in R2022bYou can now deploy code containing the exportNetworkToTensorFlow function in
standalone applications compiled with MATLAB
Compiler SDK™. As exportNetworkToTensorFlow is a support package function, you need
to add the required supporting files to compile. For more information, see Manage Support Packages (MATLAB Compiler SDK).
You can now export a Deep Learning Toolbox network that contains the following layers:
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)