Main Content

Deploy Imported Network with MATLAB Compiler

This topic shows how to import a pretrained network and then deploy the imported network using MATLAB® Compiler™. You can import a pretrained TensorFlow™-Keras or ONNX™ (Open Neural Network Exchange) network using importKerasNetwork or importONNXNetwork, respectively. These functions require the corresponding support package: Deep Learning Toolbox™ Converter for TensorFlow Models or Deep Learning Toolbox Converter for ONNX Model Format. If the required support package is not installed, then importKerasNetwork or importONNXNetwork provides a download link.

The imported network might include Keras or ONNX layers that MATLAB Coder™ does not support for deployment. For a list of supported layers, see Networks and Layers Supported for Code Generation (MATLAB Coder). In this case, you can deploy the imported network as a standalone application using MATLAB Compiler. The standalone executable you create with MATLAB Compiler is independent of MATLAB; therefore, you can deploy it to users who do not have access to MATLAB.

In the deployment workflow, you first define a classification function that loads the imported network and predicts class labels. Then, you compile the classification function into a standalone application either programmatically, using the mcc (MATLAB Compiler) function, or interactively, using the Application Compiler (MATLAB Compiler) app.

  • Use mcc if you prefer to work at the command line. You must manually specify the path to the Keras or ONNX layers folder that MATLAB Compiler includes in the standalone application. For an example of deploying an imported Keras network, see Deploy Imported Pretrained Network Using mcc. You can use the same workflow to deploy a network imported from ONNX using mcc.

  • Use the Application Compiler app if you prefer an interactive workflow. You can access the app using the deploytool (MATLAB Compiler) function or the apps gallery. The app suggests the support packages that MATLAB Compiler can include in the standalone application. Then, the app automatically includes the paths to the Keras or ONNX layers folder in the selected support package. For an example of deploying an imported Keras network, see Deploy Imported Pretrained Network Using Application Compiler App. You can use the same workflow to deploy a network imported from ONNX using the Application Compiler app.

Deploy Imported Pretrained Network Using mcc

Import a pretrained Keras network to classify an image, and then compile the classification function into a standalone application using mcc. This example uses a helper function that imports the network with importKerasNetwork, specifies the class names, and saves the imported network. To view the code for this function, see Helper Function.

Download Required Support Package

The function importKerasNetwork requires the Deep Learning Toolbox Converter for TensorFlow Models support package. If this support package is not installed, importKerasNetwork provides a download link to the required support package in the Add-On Explorer. A recommended practice is to download the support package to the default location for the version of MATLAB you are running. However, you can specify a different location during installation.

Display the support package root and the release number for the version of MATLAB you are running. You need this information when specifying the path to the Keras layers folder later in the example. The support package is located in the default location for MATLAB R2020b.

supportPKGFolder = matlabshared.supportpkg.getSupportPackageRoot
supportPKGFolder = 
'C:\ProgramData\MATLAB\SupportPackages\R2020b'
version('-release')
ans = 
'2020b'

Import Pretrained Network

Import and save the pretrained network digitsDAGnet, which contains a DAG (directed acyclic graph) convolutional neural network that classifies images of digits.

net = importDAGnet
net = 
  DAGNetwork with properties:

         Layers: [13×1 nnet.cnn.layer.Layer]
    Connections: [13×2 table]
     InputNames: {'input_1'}
    OutputNames: {'ClassificationLayer_activation_1'}

Read and Save Image

Read and save the image to classify.

digitDatasetPath = fullfile(toolboxdir('nnet'),'nndemos','nndatasets','DigitDataset');
I = imread(fullfile(digitDatasetPath,'5','image4009.png'));
imwrite(I,'testImg.png')

Display the image.

imshow(I)

Define Classification Function

Define a classification function named KerasNetClassify that accepts a digit image, loads the imported Keras network, and predicts the class label using the loaded network.

type KerasNetClassify.m
function KerasNetClassify(imFile)
% KERASNETCLASSIFY Classify image using imported network
% KERASNETCLASSIFY loads the imported Keras pretrained network
% 'digitsDAGnet.mat', reads the image in imFile, and predicts the image
% label using the imported network.
load('digitsDAGnet.mat','net');
I = imread(imFile);
label = classify(net, I);
disp(label)    
end

Create Executable File

To deploy the imported network using mcc, you must manually specify the path to the Keras layers folder. The layers folder is located in the support package folder. Display the path to the Keras layers folder.

fullfile(supportPKGFolder,'\toolbox\nnet\supportpackages\keras_importer\+nnet\+keras\+layer')
ans = 
'C:\ProgramData\MATLAB\SupportPackages\R2020b\toolbox\nnet\supportpackages\keras_importer\+nnet\+keras\+layer'

Compile the classification function into the standalone executable KerasNetClassify.exe by using the mcc function. Specify the path to the Keras layers folder that MATLAB Compiler includes in the standalone application by using -a path.

mcc -m KerasNetClassify.m...
    -a 'C:\ProgramData\MATLAB\SupportPackages\R2020b\toolbox\nnet\supportpackages\keras_importer\+nnet\+keras\+layer'...
    -n

Classify Image

Compare the labels classified using classify, KerasNetClassify.m, and KerasNetClassify.exe.

classify(net,I)
ans = categorical
     5 

KerasNetClassify('testImg.png')
     5 
!KerasNetClassify.exe 'testImg.png'
     5  
 

All three ways to classify the image return the same label.

Helper Function

This section provides the code of the importDAGnet helper function. The importDAGnet function imports the pretrained network in the file digitsDAGnet.h5, specifies the class names, and saves the imported network to digitsDAGnet.mat.

function net = importDAGnet
 
    % Specify the model file.
    modelfile = 'digitsDAGnet.h5';
    
    % Specify the class names.
    classNames = {'0','1','2','3','4','5','6','7','8','9'};
    
    % Import the Keras network with the class names.
    net = importKerasNetwork(modelfile,'Classes',classNames);
    
    % Save the imported network to a MAT file.
    save('digitsDAGnet.mat', 'net');
    
end

Deploy Imported Pretrained Network Using Application Compiler App

Import the pretrained Keras network digitsDAGnet to classify an image, and then compile the classification function into a standalone application using the Application Compiler app.

Import Pretrained Network

Use importKerasNetwork to import the digitsDAGnet network, and then save it to a MAT file. The function importKerasNetwork requires the Deep Learning Toolbox Converter for TensorFlow Models support package. If this support package is not installed, importKerasNetwork provides a download link to the required support package in the Add-On Explorer. (For more details on how to import the pretrained network and save the image to classify, see Deploy Imported Pretrained Network Using mcc.)

Define Classification Function

Define a classification function named KerasNetClassify that accepts a digit image, loads the imported Keras network, and predicts the class label using the loaded network.

function KerasNetClassify(imFile)
% KERASNETCLASSIFY Classify image using imported network
% KERASNETCLASSIFY loads the imported Keras pretrained network
% 'digitsDAGnet.mat', reads the image in imFile, and predicts the image
% label using the imported network.
load('digitsDAGnet.mat','net');
I = imread(imFile);
label = classify(net, I);
disp(label)    
end

Create Executable File

Open a list of application deployment apps by using the deploytool function.

deploytool

In the MATLAB Compiler window, click Application Compiler. (You can also open the app by selecting it from the apps gallery, available from the Apps tab.)

In the Main File section of the Compiler tab, add the main file of the application by clicking the plus sign. In the Add Files dialog box, specify the main file as the classification function KerasNetClassify.m.

Add main file for the standalone application

The app suggests software support packages from the installed support packages, which the executable can include. Because you have installed the Deep Learning Toolbox Converter for TensorFlow Models and Deep Learning Toolbox Converter for ONNX Model Format support packages, the app displays both. You must select the Deep Learning Toolbox Converter for TensorFlow Models support package. Selecting the Deep Learning Toolbox Converter for ONNX Model Format support package does not influence the execution of the application, but unnecessarily increases the application footprint.

Support packages suggested by app

In the Package section, click Package to save the standalone application.

Package the standalone application

The software compiles the standalone application. The default name for the output folder is KerasNetClassify, and the executable file KerasNetClassify.exe is located in the subfolder for_redistribution_files_only.

Software compiling the standalone application

Classify Image

Copy the image file testImg.png (image of digit 5) to the folder containing the executable file. Change the current folder to the folder containing the executable file.

copyfile('testImg.png','KerasNetClassify\for_redistribution_files_only')
cd('KerasNetClassify\for_redistribution_files_only')

Run the executable file KerasNetClassify.exe, which you created with the Application Compiler app, to classify the image testImg.png.

!KerasNetClassify.exe testImg.png
     5  
 

The classification label returned by KerasNetClassify.exe is correct.

See Also

| | | | (MATLAB Compiler) | (MATLAB Compiler)

Related Topics