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 calledmyNet
by using thetrainNetwork
(Deep Learning Toolbox)function. Then, you save the workspace by enteringsave
. This creates a file calledmatlab.mat
that contains the network object. To load the network objectmyNet
, enter:
net = coder.loadDeepLearningNetwork('matlab.mat');
你可以肌萎缩性侧索硬化症o specify the network by providing the name of a function that does not accept an input argument and returns a pretrainedSeriesNetwork
,DAGNetwork
,yolov2ObjectDetector
, orssdObjectDetector
object, such as:
alexnet
(Deep Learning Toolbox)densenet201
(Deep Learning Toolbox)googlenet
(Deep Learning Toolbox)inceptionv3
(Deep Learning Toolbox)mobilenetv2
(Deep Learning Toolbox)resnet18
(Deep Learning Toolbox)resnet50
(Deep Learning Toolbox)resnet101
(Deep Learning Toolbox)squeezenet
(Deep Learning Toolbox)vgg16
(Deep Learning Toolbox)vgg19
(Deep Learning Toolbox)xception
(Deep Learning Toolbox)
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
or the app, load the network object inside of your entry-point function by usingcoder.loadDeepLearningNetwork
. For example:
functionout = myNet_predict(in)%#codegenpersistentmynet;ifmynet = coder.loadDeepLearningNetwo isempty (mynet)rk('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 adlnetwork
Object for Code Generation
Suppose you have a pretraineddlnetwork
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.
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_predict
are of simpler datatypes and thedlarray
object is created within the function. Theextractdata
(Deep Learning Toolbox)method of thedlarray
object returns the data in thedlarray
dlA
as the output ofmyDLNet_predict
. The outputa
has 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 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 usingMATLAB Functionblocks as Simulink does not natively support
dlarray
objects.
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
codegen
|trainNetwork
(Deep Learning Toolbox)|coder.loadDeepLearningNetwork
Objects
SeriesNetwork
(Deep Learning Toolbox)|DAGNetwork
(Deep Learning Toolbox)|yolov2ObjectDetector
(计算机视觉工具箱)|ssdObjectDetector
(计算机视觉工具箱)|dlarray
(Deep Learning Toolbox)|dlnetwork
(Deep Learning Toolbox)