Main Content

Load Pretrained Networks for Code Generation

You can generate code for a pretrained neural network. To provide the network to the code generator, load a SeriesNetwork (Deep Learning Toolbox), DAGNetwork (Deep Learning Toolbox), yolov2ObjectDetector (Computer Vision Toolbox), yolov3ObjectDetector (Computer Vision Toolbox), yolov4ObjectDetector (Computer Vision Toolbox), ssdObjectDetector (Computer Vision Toolbox), or dlnetwork (Deep Learning Toolbox) object from the trained network.

Load a Network by Using coder.loadDeepLearningNetwork

You can load a network object from any network that is supported for code generation by using coder.loadDeepLearningNetwork. You can specify the network from a MAT-file. The MAT-file must contain only the network to be loaded.

For example, suppose that you create a trained network object called myNet by using the trainNetwork (Deep Learning Toolbox) function. Then, you save the workspace by entering save. This creates a file called matlab.mat that contains the network object. To load the network object myNet, enter:

net = coder.loadDeepLearningNetwork('matlab.mat');

You can also specify the network by providing the name of a function that returns a pretrained SeriesNetwork,DAGNetwork, dlnetwork, yolov2ObjectDetector, yolov3ObjectDetector, yolov4ObjectDetector, or ssdObjectDetector object, such as:

For example, load a network object by entering:

net = coder.loadDeepLearningNetwork('googlenet');

The Deep Learning Toolbox™ functions in the previous list require that you install a support package for the function. See Pretrained Deep Neural Networks (Deep Learning Toolbox).

Specify a Network Object for Code Generation

If you generate code by using codegen or the app, load the network object inside of your entry-point function by using coder.loadDeepLearningNetwork. For example:

function out = myNet_predict(in) %#codegen

persistent mynet;

if isempty(mynet)
    mynet = coder.loadDeepLearningNetwork('matlab.mat');
end
out = predict(mynet,in);

For pretrained networks that are available as support package functions such as alexnet, inceptionv3, googlenet, and resnet, you can directly specify the support package function, for example, by writing mynet = googlenet.

Next, generate code for the entry-point function. For example:

cfg = coder.gpuConfig('mex');
cfg.TargetLang = 'C++';
cfg.DeepLearningConfig = coder.DeepLearningConfig('cudnn'); 
codegen -args {ones(224,224,3,'single')} -config cfg myNet_predict

Specify a dlnetwork Object for Code Generation

Suppose you have a pretrained dlnetwork network object in the mynet.mat MAT-file. To predict the responses for this network, create an entry-point function in MATLAB® as shown in this code.

function a = myDLNet_predict(in)
dlIn = dlarray(in, 'SSC');

persistent dlnet;
if isempty(dlnet)
    dlnet = coder.loadDeepLearningNetwork('mynet.mat');
end

dlA = predict(dlnet, dlIn);

a = extractdata(dlA);

end

In this example, the input and output to myDLNet_predict are of simpler datatypes and the dlarray object is created within the function. The extractdata (Deep Learning Toolbox) method of the dlarray object returns the data in the dlarray dlA as the output of myDLNet_predict. The output a has the same data type as the underlying data type in dlA. This entry-point design has the following advantages:

  • Easier integration with standalone code generation workflows such as static, dynamic libraries, or executables.

  • The data format of the output from the extractdata function has the same order ('SCBTU') in both the MATLAB environment and the generated code.

  • Improves performance for MEX workflows.

  • Simplifies Simulink® workflows using MATLAB Function blocks as Simulink does not natively support dlarray objects.

Next, generate code for the entry-point function. For example:

cfg = coder.gpuConfig('lib');
cfg.TargetLang = 'C++';
cfg.DeepLearningConfig = coder.DeepLearningConfig('cudnn'); 
codegen -args {ones(224,224,3,'single')} -config cfg myDLNet_predict

Limitations

  • coder.loadDeepLearningNetwork does not support loading MAT-files with multiple networks.

  • The MAT-file must contain only the network to be loaded.

  • The code generator represents characters in an 8-bit ASCII codeset that the locale setting determines. Therefore, the use of non-ASCII characters in file, folder, or network names might result in errors. For more information, see Encoding of Characters in Code Generation.

See Also

Functions

Objects

Related Topics