Main Content

Deploy Imported TensorFlow Model with MATLAB Compiler

This example shows how to import a pretrained TensorFlow™ model using importNetworkFromTensorFlow, 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 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 Pretrained Networks from External Platforms.

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 importNetworkFromTensorFlow, importNetworkFromPyTorch, or importNetworkFromONNX.

You can modify this example to deploy a network that you import from ONNX™ or PyTorch® by using the importNetworkFromONNX or importNetworkFromPyTorch function, respectively. To use the importNetworkFromONNX 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 importNetworkFromTensorFlow function requires the Deep Learning Toolbox Converter for TensorFlow Models support package. If this support package is not installed, importNetworkFromTensorFlow 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 R2023b.

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

Import Pretrained TensorFlow Model

Specify the model folder and class names.

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

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

net = importNetworkFromTensorFlow(modelFolder);
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);
ClassNames = string(0:9);
scores = predict(net,single(I));
[~,idx] = max(scores);
label = ClassNames(idx);
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.

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