Main Content

Deploy Imported TensorFlow Model with MATLAB Compiler

This example shows how to import a pretrained TensorFlow™ model using importTensorFlowNetwork, and deploy the imported network using MATLAB® Compiler™. The example shows programmatic and interactive deployment workflows.

The imported network might include TensorFlow-Keras layers that MATLAB Coder™ does not support for deployment. For a list of layers that MATLAB Coder supports, 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 by using the mcc (MATLAB Compiler) function, or interactively by using the Application Compiler (MATLAB Compiler) app. The app suggests the support packages that MATLAB Compiler can include in the standalone application. For more information about support packages, see Deep Learning Import and Export.

You can deploy only the imported network using MATLAB Compiler. The programmatic and interactive workflows do not support the deployment of Network Import functions, such as importTensorFlowNetwork and importONNXNetwork.

You can modify this example to deploy a network that you import from ONNX™ or PyTorch® by using the importONNXNetwork or importNetworkFromPyTorch function, respectively. To use the importONNXNetwork function, you need the Deep Learning Toolbox Converter for ONNX Model Format support package. To use the importNetworkFromPyTorch function, you need the Deep Learning Toolbox Converter for PyTorch Models.

Download Required Support Package

The importTensorFlowNetwork function requires the Deep Learning Toolbox Converter for TensorFlow Models support package. If this support package is not installed, importTensorFlowNetwork 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 release number for the version of MATLAB you are running. The support package is in the default location for MATLAB R2021b.

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

Import Pretrained TensorFlow Model

Specify the model folder and class names.

if ~exist('digitsDAGnet','dir')
    unzip('digitsDAGnet.zip')
end
modelFolder = './digitsDAGnet';

ClassNames = string(0:9);

Import the digitsDAGnet TensorFlow model in the saved model format. By default, importTensorFlowNetwork imports the network as a DAGNetwork object.

net = importTensorFlowNetwork(modelFolder, ...
    Classes=ClassNames);
Importing the saved model...
Translating the model, this may take a few minutes...
Finished translation. Assembling network...
Import finished.

Save the imported network to a MAT file. The digitsDAGnet file contains a convolutional neural network that classifies images of digits.

save("digitsDAGnet.mat","net");

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 tfNetClassify that accepts a digit image, loads the imported network, and predicts the class label using the loaded network.

type tfNetClassify.m
function tfNetClassify(imFile)
% TFNETCLASSIFY Classify image using imported network 
% TFNETCLASSIFY loads the imported TensorFlow 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 Using mcc

Compile the classification function into the standalone executable tfNetClassify.exe by using the mcc function.

mcc -m tfNetClassify.m

The executable in this example was created on a Windows® 10 system.

If you are using a MATLAB version older than R2021b, you must manually specify the path to the Keras layers folder. The layers folder is located in the support package folder. First, display the path to the Keras layers folder, and then create the executable file by using the mcc function.

fullfile(supportPKGFolder,'\toolbox\nnet\supportpackages\keras_importer\+nnet\+keras\+layer')
mcc -m tfNetClassify.m...
    -a 'C:\ProgramData\MATLAB\SupportPackages\R2020b\toolbox\nnet\supportpackages\keras_importer\+nnet\+keras\+layer'...
    -n

Classify the image testImg.png by using the executable file.

!tfNetClassify.exe testImg.png
     5  
 

Create Executable Using Application Compiler App

Start the Application Compiler app by using the deploytool (MATLAB Compiler) 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 tfNetClassify.m.

deploytoolImportNet_MainFile.png

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.

deploytoolImportNet_SPKGs.png

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

deploytoolImportNet_Package.png

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

deploytoolImportNet_Compile.png

Copy the image file testImg.png to the folder that contains the executable file. Change the current folder to the folder containing the executable file.

copyfile("testImg.png","tfNetClassify\for_redistribution_files_only")
cd("tfNetClassify\for_redistribution_files_only")

Classify the image testImg.png by using the executable file.

!tfNetClassify.exe testImg.png

See Also

Functions

Apps

Related Topics