Main Content

exportNetworkToTensorFlow

Export Deep Learning Toolbox network or layer graph to TensorFlow

Description

example

exportNetworkToTensorFlow(net,modelPackage) exports the MATLAB® deep learning network 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.

example

exportNetworkToTensorFlow(lgraph,modelPackage) exports the MATLAB deep learning layer graph lgraph and saves it as a TensorFlow model in the Python package modelPackage.

If the MATLAB network or layer graph 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 Layer Graph with Custom Layer to TensorFlow.

Examples

collapse all

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 DAGNetwork object.

net = squeezenet
net = 
  DAGNetwork with properties:

         Layers: [68x1 nnet.cnn.layer.Layer]
    Connections: [75x2 table]
     InputNames: {'data'}
    OutputNames: {'ClassificationLayer_predictions'}

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 DAGNetwork object.

net = squeezenet
net = 
  DAGNetwork with properties:

         Layers: [68x1 nnet.cnn.layer.Layer]
    Connections: [75x2 table]
     InputNames: {'data'}
    OutputNames: {'ClassificationLayer_predictions'}

Specify the class names.

ClassNames = net.Layers(end).Classes;

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.

[label,score] = classify(net,Im);

Show the image with the classification label.

imshow(Im)
title(ClassNames(label),FontSize=12)

Figure contains an axes object. The axes object with title bell pepper contains an object of type image.

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 an untrained layer graph to TensorFlow and train the exported TensorFlow model.

Create Layer Graph

Create a long short-term memory (LSTM) network to classify sequence data. An LSTM network takes sequence data as input and makes predictions based on the individual time steps of the sequence data.

inputSize = 12;
numHiddenUnits = 100;
numClasses = 9;

layers = [
       sequenceInputLayer(inputSize)
       bilstmLayer(numHiddenUnits,OutputMode="last")
       fullyConnectedLayer(numClasses)
       softmaxLayer];

lgraph = layerGraph(layers);

Create Training Data Set

Load the Japanese Vowels training data set. XTrain is a cell array containing 270 sequences of dimension 12 and varying length. YTrain is a categorical vector of labels "1","2",..."9", which correspond to the nine speakers.

[XTrain,YTrain] = japaneseVowelsTrainData;

Prepare the sequence data in XTrain for padding. For more information, see Sequence Classification Using Deep Learning.

numObservations = numel(XTrain);
for i=1:numObservations
    sequence = XTrain{i};
    sequenceLengths(i) = size(sequence,2);
end

[sequenceLengths,idx] = sort(sequenceLengths);
XTrain = XTrain(idx);
YTrain = YTrain(idx);

Pad XTrain along the second dimension.

XTrain = padsequences(XTrain,2);

Permute the sequence data from the Deep Learning Toolbox™ ordering (CSN) to the TensorFlow ordering (NSC), where C is the number of features of the sequence, S is the sequence length, and N is the number of sequence observations. Save the training data to a MAT file.

XTrain = permute(XTrain,[3,2,1]);
YTrain = double(YTrain)-1;

filename = "training_data.mat";
save(filename,"XTrain","YTrain")

Export Layer Graph to TensorFlow

Export the layer graph lgraph to TensorFlow. The exportNetworkToTensorFlow function saves the TensorFlow model in the Python package myModel.

exportNetworkToTensorFlow(lgraph,"myModel")

Train Exported TensorFlow Model

Run this code in Python to load the exported model from the Python package myModel. You can compile and train the exported model in Python. To train model, use the training data in training_data.mat. For more information on how to load data from a MAT file into Python, see Inference Comparison Between TensorFlow and Imported Networks for Image Classification.

import myModel
model = myModel.load_model()

Export a layer graph, which contains a MATLAB custom layer, to TensorFlow.

Create Layer Graph

Create a PReLU layer by defining the custom layer preluLayer. Display the definition of the custom layer.

type preluLayer.m
classdef preluLayer < nnet.layer.Layer
    % Example custom PReLU layer.

    properties (Learnable)
        % Layer learnable parameters.

        % Scaling coefficient.
        Alpha
    end

    methods
        function layer = preluLayer(args)
            % layer = preluLayer creates a PReLU layer.
            %
            % layer = preluLayer(numChannels,Name=name) also specifies the
            % layer name.

            arguments
                args.Name = "";
            end

            % Set layer name.
            layer.Name = args.Name;

            % Set layer description.
            layer.Description = "PReLU";
        end

        function layer = initialize(layer,layout)
            % layer = initialize(layer,layout) initializes the learnable
            % parameters of the layer for the specified input layout.

            % Skip initialization of nonempty parameters.
            if ~isempty(layer.Alpha)
                return
            end

            % Input data size.
            sz = layout.Size;
            ndims = numel(sz);

            % Find number of channels.
            idx = finddim(layout,"C");
            numChannels = sz(idx);

            % Initialize Alpha.
            szAlpha = ones(1,ndims);
            szAlpha(idx) = numChannels;
            layer.Alpha = rand(szAlpha);
        end

        function Z = predict(layer, X)
            % Z = predict(layer, X) forwards the input data X through the
            % layer and outputs the result Z.

            Z = max(0, X) + layer.Alpha .* min(0, X);
        end
    end
end

Create a layer graph.

layers = [
    imageInputLayer([31 53 3],Name="image",Normalization="none")
    preluLayer(Name="prelu")
    regressionLayer];

lgraph = layerGraph(layers);

Export Layer Graph to TensorFlow

Export the layer graph lgraph 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(lgraph,"myModel")
Warning: Layer 'prelu': Layer class 'preluLayer' 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 preluLayer.py.

type ./myModel/customLayers/preluLayer.py
#    This file was created by
#    MATLAB Deep Learning Toolbox Converter for TensorFlow Models.
#    31-Aug-2022 01:59:47

import tensorflow as tf
import sys     # Remove this line after completing the layer definition.

class preluLayer(tf.keras.layers.Layer):
    # Add any additional layer hyperparameters to the constructor's
    # argument list below.
    def __init__(self, Alpha_Shape_=None, name=None):
        super(preluLayer, self).__init__(name=name)
        # Learnable parameters: These have been exported from MATLAB and will be loaded automatically from the weight file:
        self.Alpha = tf.Variable(name="Alpha", initial_value=tf.zeros(Alpha_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 preluLayer in the customLayers folder.")
        print("Exiting...")
        sys.exit("See the warning message above.")

        return output1

Load Exported Layer Graph

This section describes the steps that you must perform in Python to load the exported TensorFlow model.

Edit the definition of preluLayer.py by implementing the forward computation in call.

def call(self, input1):
    output1 = tf.math.maximum(input1,0.0) + self.Alpha + tf.math.minimum(0.0,input1)
    return output1

Delete the lines in preluLayer.py, as instructed by the comments in the file. View the updated custom layer preluLayer.py.

import tensorflow as tf

class preluLayer(tf.keras.layers.Layer):
    # Add any additional layer hyperparameters to the constructor's
    # argument list below.
    def __init__(self, Alpha_Shape_=None, name=None):
        super(preluLayer, self).__init__(name=name)
        # Learnable parameters: These have been exported from MATLAB and will be loaded automatically from the weight file:
        self.Alpha = tf.Variable(name="Alpha", initial_value=tf.zeros(Alpha_Shape_), trainable=True)

    def call(self, input1):
        output1 = tf.math.maximum(input1,0.0) + self.Alpha + tf.math.minimum(0.0,input1)
        return output1

In this example, you only have to edit preluLayer.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

collapse all

Deep Learning Toolbox network, specified as a SeriesNetwork object, DAGNetwork object, or dlnetwork object.

You can get a trained network by:

  • Using a Deep Learning Toolbox function to load a pretrained network. For example, use the efficientnetb0 function.

  • Downloading a pretrained network from the MATLAB Deep Learning Model Hub.

  • Training your own network. Use trainNetwork to train a DAGNetwork or SeriesNetwork object. Use a custom training loop to train a dlnetwork object.

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_.py file, which defines the modelPackage 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 in HDF5 or SavedModel 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 in HDF5 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 the customLayers folder only when the MATLAB network or layer graph contains a custom or built-in MATLAB layer that exportNetworkToTensorFlow cannot convert to a TensorFlow layer.

Example: "myModel"

Deep Learning Toolbox layer graph, specified as a LayerGraph object or Layer array.

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 or layer graph that contains groupNormalizationLayer, instanceNormalizationLayer, or layerNormalizationLayer layers

More About

collapse all

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 on MATLAB indexing, see Array Indexing. In MATLAB, to use an array of indices (ind) created in Python, convert the array to ind+1.

Version History

Introduced in R2022b