Documentation

coder.loadDeepLearningNetwork

Load deep learning network model

Description

net= coder.loadDeepLearningNetwork(filename)loads a pretrained deep learningSeriesNetworkorDAGNetworkobject saved in thefilenameMAT-file.filenamemust be a valid MAT-file existing on the MATLAB®path containing a singleSeriesNetworkorDAGNetworkobject.

example

net= coder.loadDeepLearningNetwork(functionname)calls a function that returns a pretrained deep learningSeriesNetworkorDAGNetworkobject.functionnamemust be the name of a function existing on the MATLAB path that returns aSeriesNetworkorDAGNetworkobject.

example

net= coder.loadDeepLearningNetwork(___,network_name)is the same asnet = coder.loadDeepLearningNetwork(filename)with the option to name the C++ class generated from the network.network_nameis a descriptive name for the network object saved in the MAT-file or pointed to by the function. The network name must be achartype that is a valid identifier in C++.

Use this function when generating code from a network object inference. This function generates a C++ class from this network. The class name is derived from the MAT-file name or the function name.

Examples

collapse all

Use of thecoder.loadDeepLearningNetworkfunction to load anVGG-16series network and generate C++ code for this network.

Get the MAT-file containing the pretrainedVGG-16network.

url ='//www.tatmou.com/supportfiles/gpucoder/cnn_models/VGG/vgg16.mat'; websave('vgg16.mat',url);

Create an entry-point functionmyVGG16that uses thecoder.loadDeepLearningNetworkfunction to load thevgg16.matinto the persistentmynetSeriesNetworkobject.

functionout = myVGG16(in)persistentmynet;ifisempty(mynet) mynet = coder.loadDeepLearningNetwork('vgg16.mat','myVGGnet');endout = predict(mynet,in);

The persistent object avoids reconstructing and reloading the network object during subsequent calls to the function to invoke thepredictmethod on the input.

The input layer of the pretrainedVGG-16network accepts images of size224x224x3. Use the following lines of code to read an input image from a graphics file and resize it to224x224.

in = imread('peppers.png'); in = imresize(in,[224,224]);

Create acoder.configconfiguration object for MEX code generation and set the target language to C++. On the configuration object, setDeepLearningConfigwithtargetlibas'mkldnn'. Thecodegenfunction must determine the size, class, and complexity of MATLAB function inputs. Use the-argsoption to specify the size of the input to the entry-point function. Use the-configoption to pass the code configuration object.

cfg = coder.config('mex'); cfg.TargetLang ='C++'; cfg.DeepLearningConfig = coder.DeepLearningConfig('mkldnn'); codegen-args{ones(224,224,3,'uint8')}-configcfgmyVGG16-report;

Thecodegencommand places all the generated files in thecodegenfolder. The folder contains the C++ code for the entry-point functionmyVGG16.cpp, header and source files containing the C++ class definitions for the convoluted neural network (CNN), weight, and bias files.

CallVGG-16predict on the input image and display the top five predicted labels.

predict_scores = myVGG16_mex(in); [scores,indx] = sort(predict_scores,'descend'); net = coder.loadDeepLearningNetwork('vgg16.mat'); classNames = net.Layers(end).Classes; disp(classNames(indx(1:5)));
bell pepper cucumber grocery store acorn squash butternut squash

Use of thecoder.loadDeepLearningNetworkfunction to load anresnet50series network and generate CUDA®code for this network.

Create an entry-point functionresnetFunthat uses thecoder.loadDeepLearningNetworkfunction to call the Deep Learning Toolbox™ toolbox functionresnet50. This function returns a pretrainedResNet-50network.

functionout = resnetFun(in)persistentmynet;ifisempty(mynet) mynet = coder.loadDeepLearningNetwork('resnet50','myresnet');endout = predict(mynet,in);

The persistent object avoids reconstructing and reloading the network object during subsequent calls to the function to invoke thepredictmethod on the input.

The input layer of the pretrainedResNet-50network accepts images of size224x224x3. To read an input image from a graphics file and resize it to224x224, use the following lines of code:

in = imread('peppers.png'); in = imresize(in,[224,224]);

Create acoder.gpuConfigconfiguration object for MEX code generation and set the target language to C++. Thecodegenfunction must determine the size, class, and complexity of MATLAB function inputs. Use the-argsoption to specify the size of the input to the entry-point function and the-configoption to pass the code configuration object.

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

Thecodegencommand places all the generated files in thecodegenfolder. It contains the CUDA code for the entry-point functionresnetFun.cu, header, and source files containing the C++ class definitions for the convoluted neural network (CNN), weight, and bias files.

Input Arguments

collapse all

Specifies the name of the MAT-file containing the pretrainedSeriesNetworkorDAGNetworkobject.

Data Types:string

Specifies the name of the function that returns a pretrainedSeriesNetworkorDAGNetworkobject.

Data Types:string

Descriptive name for theSeriesNetworkobject saved in the MAT-file. It must be achartype that is a valid identifier in C++.

Data Types:char

Output Arguments

collapse all

Network inference, returned as aSeriesNetworkobject or aDAGNetworkobject.

Limitations

  • coder.loadDeepLearningNetworkdoes not support loading MAT-files with multiple networks. The MAT-file must contain only the network to be loaded.

Introduced in R2017b