Main Content

Load Pretrained Networks for Code Generation

You can load a pretrained deep learning network into MATLAB® and Simulink® for code generation. To generate code from a pretrained network, you load the network using these functions:

You can also pass the dlnetwork objects:

  • Directly to an entry-point function.

  • As constant objects to an entry-point function as argument by wrapping it using coder.Constant.

Load Pretrained Networks

You can load a pretrained network in several different ways.

Load a Network Saved in a MAT File by Using coder.loadDeepLearningNetwork

You can load a dlnetwork object from any MAT file for code generation. Save the dlnetwork into a MAT file and specify this MAT file in coder.loadDeepLearningNetwork to load the network. The MAT file must contain only the network that you want to load.

For example, this command loads a MAT file named myNet.

net = coder.loadDeepLearningNetwork('myNet.mat');

Pass the Function Name to coder.loadDeepLearningNetwork to Load a Network

You can use coder.loadDeepLearningNetwork to load a pretrained network by passing the function name that returns a dlnetwork or an object detector. For more information on supported classes, see Supported Classes.

For example, write a function getNetwork that loads the Squeeze Network by using imagePretrainedNetwork.

function dlnet = getNetwork()

dlnet = imagePretrainedNetwork("squeezenet");

end

You can load a network object use this code:

net = coder.loadDeepLearningNetwork('getNetwork');

Load a Pretrained Network by Using imagePretrainedNetwork

You can use the imagePretrainedNetwork (Deep Learning Toolbox) function to load the pretrained networks, available in Deep Learning Toolbox™ for code generation.

net = imagePretrainedNetwork("googlenet")

You must download and install the required support package for a pretrained neural network such as googlenet. The imagePretrainedNetwork function provides a download link. For more information, see Pretrained Deep Neural Networks (Deep Learning Toolbox).

Load a Pretrained Network by Using the coder.load Function

If your pretrained network is a dlnetwork object, you can load the network by using the coder.load function in the entry-point function. You can only use this method to generate generic C/C++ or plain CUDA code. (since R2025a)

For example, this function loads the MAT file as a structure that contains the network:

function out = myNet_predict(matfile, varName, in)

S = coder.load(matfile);
dlnet = S.(varName);
out = predict(dlnet, in);

end

Generate code for the entry-point function by using the codegen function.

cfg = coder.config('mex');
cfg.DeepLearningConfig = coder.DeepLearningConfig('none');

dlnet = imagePretrainedNetwork('squeezenet');
save('myNet.mat','dlnet');

args = {coder.Constant('dlnet.mat'),coder.Constant('dlnet'), dlarray(ones(224,224,3,'single'), 'SSC')};

codegen -args args -config cfg myNet_predict

Load a Pretrained Network from a Compile-Time Extrinsic Function

You can load a pretrained network by declaring the getNetwork function as an extrinsic function and loading the network as a compile-time constant by using the coder.const function. You can only use this method to generate generic C/C++ or plain CUDA code. (since R2025a)

For example, write a function getNetwork that loads the Squeeze Network by using imagePretrainedNetwork.

function dlnet = getNetwork()

dlnet = imagePretrainedNetwork("squeezenet");

end

Then declare getNetwork as extrinsic function by using coder.extrinsic and call getNetwork in a coder.const statement.

function out = myNet_predict(in)

coder.extrinsic('getNetwork');
dlnet = coder.const(getNetwork());
out = predict(dlnet, in);

end

Generate code for the entry-point function by using the codegen function.

cfg = coder.gpuConfig('mex');
cfg.DeepLearningConfig = coder.DeepLearningConfig('none'); 

codegen -args {dlarray(ones(224,224,3,'single'), 'SSC')} -config cfg myNet_predict

Pass a Pretrained Network to the Entry-Point Function

If your pretrained network is a dlnetwork object, you can load the network by passing the object directly to the entry-point function. You can only use this method to generate generic C/C++ or plain CUDA code. (since R2025a)

Passing a pretrained network directly to an entry-point function is convenient for prototyping, but it is not recommended for generating standalone executables.

For example, this code pass the dlnetwork object as an input to the entry-point function:

function out = myNet_predict(dlnet,in)

out = predict(dlnet, in);

end

Generate code for the entry-point function by using the codegen function.

cfg = coder.gpuConfig('mex');
cfg.DeepLearningConfig = coder.DeepLearningConfig('none'); 
dlnet = imagePretrainedNetwork('squeezenet');
codegen -args {dlnet, dlarray(ones(224,224,3,'single'), 'SSC')} -config cfg myNet_predict

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.

See Also

Functions

Objects

Topics