Main Content

coder.loadDeepLearningNetwork

Load deep learning network model

Description

net = coder.loadDeepLearningNetwork(filename) loads a pretrained deep learning SeriesNetwork (Deep Learning Toolbox), DAGNetwork (Deep Learning Toolbox), yolov2ObjectDetector (Computer Vision Toolbox), ssdObjectDetector (Computer Vision Toolbox), or dlnetwork (Deep Learning Toolbox) object saved in the filename MAT-file. filename must be a valid MAT-file existing on the MATLAB® path containing a single SeriesNetwork, DAGNetwork, yolov2ObjectDetector, ssdObjectDetector, or dlnetwork object. The MAT-file must contain only the network to be loaded.

example

net = coder.loadDeepLearningNetwork(functionname) calls a function that returns a pretrained deep learning SeriesNetwork, DAGNetwork, yolov2ObjectDetector, ssdObjectDetector, or dlnetwork object. functionname must be the name of a function existing on the MATLAB path that returns a SeriesNetwork, DAGNetwork, yolov2ObjectDetector, ssdObjectDetector, or dlnetwork object.

example

net = coder.loadDeepLearningNetwork(___,network_name) 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 a char 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

collapse all

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 the coder.loadDeepLearningNetwork function to load the vgg16.mat into the persistent mynet 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 the predict 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 to 224x224.

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 the coder.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 the predict 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 function resnetFun.cu, header, and source files containing the C++ class definitions for the neural network, weight, and bias files.

Input Arguments

collapse all

Specifies the name of the MAT-file containing the pretrained SeriesNetwork, DAGNetwork, yolov2ObjectDetector, ssdObjectDetector, or dlnetwork object.

This input argument must be a compile-time constant.

Data Types: string

Specifies the name of the function that returns a pretrained SeriesNetwork, DAGNetwork, yolov2ObjectDetector, ssdObjectDetector, or dlnetwork 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

collapse all

Network inference, returned as a SeriesNetwork, DAGNetwork, yolov2ObjectDetector, ssdObjectDetector, or dlnetwork object.

Limitations

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

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

  • The code generator represents characters in an 8-bit ASCII codeset that the locale setting determines. Therefore, the use of non-ASCII characters in file, folder, or network names might result in errors. For more information, see Encoding of Characters in Code Generation.

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