Main Content

Load Pretrained Networks for Code Generation

You can generate code for a pretrained convolutional neural network (CNN). To provide the network to the code generator, load aSeriesNetwork(Deep Learning Toolbox),DAGNetwork(Deep Learning Toolbox),yolov2ObjectDetector(计算机视觉工具箱),ssdObjectDetector(计算机视觉工具箱), ordlnetwork(Deep Learning Toolbox)object from the trained network.

Load a Network by Usingcoder.loadDeepLearningNetwork

You can load a network object from any network that is supported for code generation by usingcoder.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 calledmyNetby using thetrainNetwork(Deep Learning Toolbox)function. Then, you save the workspace by enteringsave. This creates a file calledmatlab.matthat contains the network object. To load the network objectmyNet, enter:

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

你也可以specify the network by providing the name of a function that does not accept an input argument and returns a pretrainedSeriesNetwork,DAGNetwork,yolov2ObjectDetector, orssdObjectDetectorobject, 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. SeePretrained Deep Neural Networks(Deep Learning Toolbox).

Specify a Network Object for Code Generation

If you generate code by usingcodegen或应用程序,加载的网络对象内部entry-point function by usingcoder.loadDeepLearningNetwork. For example:

functionout = myNet_predict(in)%#codegenpersistentmynet;ifisempty(mynet) mynet = coder.loadDeepLearningNetwork('matlab.mat');endout = predict(mynet,in);

For pretrained networks that are available as support package functions such asalexnet,inceptionv3,googlenet, andresnet, you can directly specify the support package function, for example, by writingmynet = 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')}-configcfgmyNet_predict

Specify adlnetworkObject for Code Generation

Suppose you have a pretraineddlnetworknetwork object in themynet.matMAT-file. To predict the responses for this network, create an entry-point function in MATLAB®as shown in this code.

functiona = myDLNet_predict(in) dlIn = dlarray(in,'SSC');persistentdlnet;ifisempty(dlnet) dlnet = coder.loadDeepLearningNetwork('mynet.mat');enddlA = predict(dlnet, dlIn); a = extractdata(dlA);end

In this example, the input and output tomyDLNet_predictare of simpler datatypes and thedlarrayobject is created within the function. Theextractdata(Deep Learning Toolbox)method of thedlarrayobject returns the data in thedlarraydlAas the output ofmyDLNet_predict. The outputahas the same data type as the underlying data type indlA. 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 theextractdatafunction has the same order ('SCBTU') in both the MATLAB environment and the generated code.

  • Improves performance for MEX workflows.

  • Simplifies Simulink®workflows usingMATLAB Functionblocks as Simulink does not natively supportdlarrayobjects.

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')}-configcfgmyDLNet_predict

See Also

Functions

Objects

Related Topics