Deploy Imported TensorFlow Model with MATLAB Compiler - MATLAB & Simulink (original) (raw)

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 programmatically by using the compiler.build.standaloneApplication function or the mcc function.

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'

For more information about support packages, see Pretrained Networks from External Platforms.

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.

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.

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 compiler.build.standaloneApplication

Build a standalone application using the compiler.build.standaloneApplication (MATLAB Compiler) function.

MATLAB Compiler uses a dependency analysis function to find required software support packages. Because you have installed the Deep Learning Toolbox Converter for TensorFlow Models and Deep Learning Toolbox Converter for ONNX Model Format support packages, MATLAB Compiler may include both support packages. 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 if it is included. You can prevent its inclusion by manually specifying the Deep Learning Toolbox Converter for TensorFlow Models support package.

compiler.build.standaloneApplication("tfNetClassify.m","SupportPackages","Deep Learning Toolbox Converter for TensorFlow Models");

The software compiles the standalone application. The executable file tfNetClassify.exe is located in the folder named tfNetClassifystandaloneApplication.

Create Executable Using mcc

As an alternative to compiler.build.standaloneApplication, you can compile the classification function into a standalone application using the mcc function.

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 Image Using Standalone Application Executable

Copy the image file testImg.png to the folder that contains the executable file. For instance, if you used compiler.build.standaloneApplication, copy the image to the tfNetClassifystandaloneApplication folder.

copyfile("testImg.png","tfNetClassifystandaloneApplication") cd("tfNetClassifystandaloneApplication")

Classify the image testImg.png using the executable file.

!tfNetClassify.exe testImg.png

To run the application outside of MATLAB, you must install MATLAB Runtime. For more information, see About MATLAB Runtime (MATLAB Compiler).

See Also

Functions

Apps

Topics