coder.loadDeepLearningNetwork - Load deep learning network model - MATLAB (original) (raw)
Load deep learning network model
Syntax
Description
[net](#d126e10250) = coder.loadDeepLearningNetwork([filename](#d126e10142))
loads a pretrained deep learning dlnetwork (Deep Learning Toolbox), SeriesNetwork (Deep Learning Toolbox), DAGNetwork (Deep Learning Toolbox), yolov2ObjectDetector (Computer Vision Toolbox), or ssdObjectDetector (Computer Vision Toolbox) object saved in the filename
MAT file. filename
must be a valid MAT file existing on the MATLAB® path containing a single dlnetwork
,SeriesNetwork
, DAGNetwork
,yolov2ObjectDetector
, or ssdObjectDetector
object. The MAT file must contain only the network to be loaded.
[net](#d126e10250) = coder.loadDeepLearningNetwork([functionname](#d126e10179))
calls a function that returns a pretrained deep learning dlnetwork (Deep Learning Toolbox),SeriesNetwork
, DAGNetwork
,yolov2ObjectDetector
, orssdObjectDetector
object. functionname
must be the name of a function existing on the MATLAB path that returns a dlnetwork
,SeriesNetwork
, DAGNetwork
,yolov2ObjectDetector
, or ssdObjectDetector
object.
[net](#d126e10250) = coder.loadDeepLearningNetwork(___,[network_name](#d126e10219))
is the same as net = coder.loadDeepLearningNetwork(filename)
with the option to name the C++ class generated from the network.network_name
is a descriptive name for the network object saved in the MAT file or pointed to by the function. The network name must be achar
type 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.
Note
The input argument of coder.loadDeepLearningNetwork
must be a compile-time constant.
Examples
Use of the coder.loadDeepLearningNetwork
function to load an VGG-16
series network and generate C++ code for this network.
Get the MAT file containing the pretrained VGG-16
network.
url = 'https://www.mathworks.com/supportfiles/gpucoder/cnn_models/VGG/vgg16.mat'; websave('vgg16.mat',url);
Create an entry-point function myVGG16
that uses thecoder.loadDeepLearningNetwork
function to load thevgg16.mat
into the persistentmynet
SeriesNetwork
object.
function out = myVGG16(in)
persistent mynet; if isempty(mynet) mynet = coder.loadDeepLearningNetwork('vgg16.mat', 'myVGGnet'); end
out = predict(mynet,in);
The persistent object avoids reconstructing and reloading the network object during subsequent calls to the function to invoke thepredict
method on the input.
The input layer of the pretrained VGG-16
network accepts images of size 224x224x3
. 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 a coder.config
configuration object for MEX code generation and set the target language to C++. On the configuration object, set DeepLearningConfig
with targetlib
as 'mkldnn'
. The codegen function must determine the size, class, and complexity of MATLAB function inputs. Use the -args
option to specify the size of the input to the entry-point function. Use the-config
option 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')} -config cfg myVGG16 -report;
The codegen
command places all the generated files in the codegen
folder. The folder contains the C++ code for the entry-point function myVGG16.cpp
, header and source files containing the C++ class definitions for the neural network, weight, and bias files.
Call VGG-16
predict 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 the coder.loadDeepLearningNetwork
function to load an resnet50
series network and generate CUDA® code for this network.
Create an entry-point function resnetFun
that uses thecoder.loadDeepLearningNetwork
function to call the Deep Learning Toolbox™ toolbox function resnet50
. This function returns a pretrained ResNet-50
network.
function out = resnetFun(in)
persistent mynet; if isempty(mynet) mynet = coder.loadDeepLearningNetwork('resnet50', 'myresnet'); end
out = predict(mynet,in);
The persistent object avoids reconstructing and reloading the network object during subsequent calls to the function to invoke thepredict
method on the input.
The input layer of the pretrained ResNet-50
network accepts images of size 224x224x3
. To read an input image from a graphics file and resize it to 224x224
, use the following lines of code:
in = imread('peppers.png'); in = imresize(in,[224,224]);
Create a coder.gpuConfig
configuration object for MEX code generation and set the target language to C++. The codegen function must determine the size, class, and complexity of MATLAB function inputs. Use the -args
option to specify the size of the input to the entry-point function and the-config
option 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')} -config cfg resnetFun -report;
The codegen
command places all the generated files in the codegen
folder. It contains the CUDA code for the entry-point functionresnetFun.cu
, header, and source files containing the C++ class definitions for the neural network, weight, and bias files.
Input Arguments
Specifies the name of the MAT file containing the pretraineddlnetwork
, SeriesNetwork
,DAGNetwork
, yolov2ObjectDetector
, or ssdObjectDetector
object.
This input argument must be a compile-time constant.
Data Types: string
Specifies the name of the function that returns a pretraineddlnetwork
, SeriesNetwork
,DAGNetwork
, yolov2ObjectDetector
, or ssdObjectDetector
object.
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 char
type that is a valid identifier in C++.
This input argument must be a compile-time constant.
Data Types: char
Output Arguments
Network inference, returned as a dlnetwork
,SeriesNetwork
, DAGNetwork
,yolov2ObjectDetector
, orssdObjectDetector
object.
Limitations
coder.loadDeepLearningNetwork
does not support loading MAT files that have multiple networks or multiple object detectors.- The locale setting determines the 8-bit ASCII codeset that the code generator uses to represent characters. Therefore, the use of non-ASCII characters in file, folder, or network names can result in errors. For more information, see Encoding of Characters in Code Generation.
Extended Capabilities
Version History
Introduced in R2017b
See Also
Functions
- codegen | imagePretrainedNetwork (Deep Learning Toolbox)