Load Pretrained Networks for Code Generation - MATLAB & Simulink (original) (raw)

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 calledmyNet by using the trainNetwork (Deep Learning Toolbox) function. Then, you save the workspace by enteringsave. 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, orssdObjectDetector 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 asalexnet, 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.config('mex'); cfg.TargetLang = 'C++'; cfg.DeepLearningConfig = coder.DeepLearningConfig('mkldnn'); 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 themynet.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 thedlarray dlA as the output of myDLNet_predict. The outputa has the same data type as the underlying data type indlA. This entry-point design has the following advantages:

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

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

Limitations

See Also

Functions

Objects