exportNetworkToTensorFlow - Export Deep Learning Toolbox network to TensorFlow - MATLAB (original) (raw)

Export Deep Learning Toolbox network to TensorFlow

Since R2022b

Syntax

Description

exportNetworkToTensorFlow([net](#mw%5F4c6ae34b-a85b-4e5c-9770-cd77a7d06fe2),[modelPackage](#mw%5F7e875427-d06a-478f-b728-9da5b5f3df0d)) 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, thenexportNetworkToTensorFlow 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.

example

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

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

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.

01-Feb-2025 09🔞04

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

collapse all

Deep Learning Toolbox network, specified as a dlnetwork object.

You can get a trained network in these ways:

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. ThemodelPackage package contains:

Example: "myModel"

Limitations

More About

collapse all

The exportNetworkToTensorFlow function supports these Deep Learning Toolbox layers for export as TensorFlow layers.

This section describes how to load a TensorFlow model in Python from the package modelPackage, which theexportNetworkToTensorFlow 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:

  1. First, install the Keras 2 package.
  2. Then add the following lines of code at the beginning of themodel.py and __init__.py files inside the generated modelPackage Python package.
    import os
    os.environ["TF_USE_LEGACY_KERAS"] = "1"
  3. 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.

Tips

Version History

Introduced in R2022b

expand all

You 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).