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
Export Network to TensorFlow
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")
Export Network to TensorFlow and Classify Image
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");
net
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.
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 Network with Custom Layer to TensorFlow
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.m
classdef 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. # 20-Jul-2024 17:05:22 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
net
— Deep Learning Toolbox network
dlnetwork
object
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
imagePretrainedNetwork
function.Download a pretrained network from the MATLAB Deep Learning Model Hub.
Train a
dlnetwork
object by using thetrainnet
function or a custom training loop.
You can also export an initialized dlnetwork
object to TensorFlow.
modelPackage
— Name of Python package containing exported model
string scalar | character vector
Name of the Python package containing the exported TensorFlow model, specified as a string scalar or character vector. The
modelPackage
package contains:
The
_init_.py
file, which defines themodelPackage
folder as a regular Python package.The
model.py
file, which contains the code that defines the untrained TensorFlow-Keras model.The
README.txt
file, which provides instructions on how to load the TensorFlow model and save it inHDF5
orSavedModel
format. For more details, see Load Exported TensorFlow Model and Save Exported TensorFlow Model in Standard Format.The
weights.h5
file, which contains the model weights inHDF5
format.The
customLayers
folder, 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 thecustomLayers
folder only when the MATLAB network contains a custom or built-in MATLAB layer thatexportNetworkToTensorFlow
cannot 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
tfa
for a MATLAB network that contains one or more of the following layers:groupNormalizationLayer
instanceNormalizationLayer
layerNormalizationLayer
withOperationDimension
set to"batch-excluded"
More About
Layers Supported for Exporting to TensorFlow
The exportNetworkToTensorFlow
function supports these Deep Learning Toolbox layers for export as TensorFlow layers.
Deep Learning Toolbox Layers
Convolution and Fully Connected Layers convolution1dLayer
convolution2dLayer
convolution3dLayer
groupedConvolution2dLayer
fullyConnectedLayer
transposedConv2dLayer
transposedConv3dLayer
*
exportNetworkToTensorFlow
exportsgruProjectedLayer
andlstmProjectedLayer
objects 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 clippedReluLayer
eluLayer
geluLayer
functionLayer
leakyReluLayer
preluLayer
reluLayer
swishLayer
tanhLayer
Utility Layers identityLayer
Computer 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)
Load Exported TensorFlow Model
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.
Load the exported TensorFlow model with weights.
import modelPackage
model = modelPackage.load_model()
Load the exported TensorFlow model without weights.
import modelPackage
model = modelPackage.load_model(load_weights=False)
Save Exported TensorFlow Model in Standard Format
Optionally, you can save the exported TensorFlow model in SavedModel
or HDF5
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")
Save the loaded TensorFlow model in HDF5
format.
model.save("modelName",save_format="h5")
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 R2022bR2024b: Export networks with four new layers
You can now export a Deep Learning Toolbox network that contains the following layers:
Comando MATLAB
Hai fatto clic su un collegamento che corrisponde a questo comando MATLAB:
Esegui il comando inserendolo nella finestra di comando MATLAB. I browser web non supportano i comandi MATLAB.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- 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)