Generate Generic C/C++ Code for Deep Learning Networks
R2026bWith MATLAB®
Coder™, you can generate generic C or C++ code for prediction from an already trained
neural network. The generated C/C++ code does not depend on third-party libraries. The
generated code implements a neural network with the architecture, layers, and parameters
specified in the input dlnetwork (Deep Learning Toolbox) object. See Networks and Layers Supported for Code Generation.
Generate code by using one of these methods:
The standard
codegencommand for C/C++ code generation from MATLAB code.The MATLAB Coder app.
Requirements
On Windows®, code generation for deep learning networks with the
codegenfunction requires Microsoft® Visual Studio® or the MinGW® compiler.MATLAB Coder Interface for Deep Learning. To install this support package, select it from the MATLAB Add-Ons menu.
Deep Learning Toolbox™.
Code Generation by Using codegen
Write an entry-point function in MATLAB that:
Uses the
coder.loadDeepLearningNetworkfunction to construct and set up a network object. For more information, see Load Pretrained Networks for Code Generation.Calls the
predict(Deep Learning Toolbox) method of the network on the entry-point function input.Specifies the size and data type of the network input.
You can pass numeric inputs directly to the
predictfunction without creating adlarrayobject. To specify dimension labels for the network input and output, use theInputDataFormatsandOutputDataFormatsname-value arguments of thepredictfunction. (since R2026b)For example:
function out = my_predict(in) %#codegen persistent mynet; if isempty(mynet) mynet = coder.loadDeepLearningNetwork("myNetwork.mat"); end out = predict(mynet, in, InputDataFormats="SSCB");
Create a deep learning configuration object
dlconfigthat is configured for generating generic C/C++ code by using thecoder.DeepLearningConfigfunction.dlconfig = coder.DeepLearningConfig(TargetLibrary="none");Create a code generation configuration object for MEX, executable, or static or dynamically linked library. To produce generic C++ code, in your code generation configuration object, set the
TargetLangparameter to"C++". Set theDeepLearningConfigparameter to the previously created objectdlconfig.cfg = coder.config("lib"); cfg.TargetLang = "C++"; cfg.DeepLearningConfig = dlconfig;
Create a single-precision input by using
coder.typeof. You can also use half-precision inputs by settingcfg.DeepLearningConfig.ComputePrecision = "FP16". Run thecodegencommand. Use the-configoption to specify the configuration object. Use the-argsoption to specify the size and data type of the input.myInput = coder.typeof(single(0), [28 28 1 4]); codegen -config cfg my_predict -args {myInput} -report
Code Generation by Using the MATLAB Coder App
Follow the usual steps for specifying the entry-point function and specifying input types. See Generate Deployable Standalone Code by Using the MATLAB Coder App.
In the Generate Code step:
Set Language to either C or C++.
Click Settings. In the Deep Learning pane, set Deep learning library to
None.
Generate code.
Relocating DNN Constants
When you generate generic C/C++/CUDA deep learning code, the code generator writes the
large constants for a deep neural network (DNN) to binary data files instead of embedding
the constants in the generated code. To customize this behavior, set the configuration
parameters LargeConstantGeneration and
LargeConstantThreshold.
By default, the generated application looks for the binary data files in the
codegen folder. If you are relocating the generated application and
data files to a different location such as an embedded board, create an environment
variable called CODER_DATA_PATH, whose value is the location of the
relocated data files. The generated application will then look for the data files in this
location.
For an example of this functionality, see Generate Code for a Deep Learning Network for x86-64 Platforms Using Advanced Vector Instructions.
See Also
codegen | coder.DeepLearningConfig | coder.loadDeepLearningNetwork