Main Content

coder.loadDeepLearningNetwork

Load deep learning network model

Description

net= coder.loadDeepLearningNetwork(filename)loads a pretrained deep learningSeriesNetwork(Deep Learning Toolbox),DAGNetwork(Deep Learning Toolbox),yolov2ObjectDetector(Computer Vision Toolbox), orssdObjectDetector(Computer Vision Toolbox)object saved in thefilenameMAT-file.filenamemust be a valid MAT-file existing on the MATLAB®path containing a singleSeriesNetwork,DAGNetwork,yolov2ObjectDetector, orssdObjectDetectorobject. The MAT-file must contain only the network to be loaded.

example

net= coder.loadDeepLearningNetwork(functionname)calls a function that returns a pretrained deep learningSeriesNetwork,DAGNetwork,yolov2ObjectDetector, orssdObjectDetectorobject.functionnamemust be the name of a function existing on the MATLAB path that returns aSeriesNetwork,DAGNetwork,yolov2ObjectDetector, orssdObjectDetectorobject.

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 a字符type that is a valid identifier in C++.

使用这个函数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.

Note

The input argument ofcoder.loadDeepLearningNetworkmust be a compile-time constant.

Examples

collapse all

Use of thecoder.loadDeepLearningNetwork函数加载一个VGG-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;

Thecodegen命令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.loadDeepLearningNetwork函数加载一个resnet50series 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;

Thecodegen命令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 pretrainedSeriesNetwork,DAGNetwork,yolov2ObjectDetector, orssdObjectDetectorobject.

This input argument must be a compile-time constant.

Data Types:string

Specifies the name of the function that returns a pretrainedSeriesNetwork,DAGNetwork,yolov2ObjectDetector, orssdObjectDetectorobject.

This input argument must be a compile-time constant.

Data Types:string

Descriptive name for the network object saved in the MAT-file. It must be a字符type that is a valid identifier in C++.

This input argument must be a compile-time constant.

Data Types:字符

Output Arguments

collapse all

Network inference, returned as aSeriesNetwork,DAGNetwork,yolov2ObjectDetector, orssdObjectDetectorobject.

Limitations

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

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

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

GPU Code Generation
Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.

Version History

Introduced in R2017b