Main Content

Generate Generic C/C++ Code for Deep Learning Networks

With 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 SeriesNetwork (Deep Learning Toolbox), DAGNetwork (Deep Learning Toolbox), or dlnetwork (Deep Learning Toolbox) network object. See Networks and Layers Supported for Code Generation.

Generate code by using one of these methods:

  • The standard codegen command for C/C++ code generation from MATLAB code.

  • The MATLAB Coder app.

Requirements

  • On Windows®, code generation for deep learning networks with the codegen function 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

  1. Write an entry-point function in MATLAB that:

    For example:

    function out = my_predict(in) %#codegen
    
    % A persistent object mynet is used to load the series network object.
    % At the first call to this function, the persistent object is constructed and
    % setup. When the function is called subsequent times, the same object is reused 
    % to call predict on inputs, thus avoiding reconstructing and reloading the
    % network object.
    
    persistent mynet;
    
    if isempty(mynet)
        mynet = coder.loadDeepLearningNetwork('myNetwork.mat');
    end
    
    % pass in input   
    out = predict(mynet,in,'MiniBatchSize',2); 

  2. Create a deep learning configuration object dlconfig that is configured for generating generic C/C++ code by using the coder.DeepLearningConfig function.

    dlconfig = coder.DeepLearningConfig(TargetLibrary='none');

    Create a code generation configuration object for MEX, executable, or static or dynamically linked library. By default, the code generator produces generic C code. To produce generic C++ code, in your code generation configuration object, set the TargetLang parameter to 'C++'. Set the DeepLearningConfig parameter to the previously created object dlconfig.

    cfg = coder.config('lib');
    cfg.TargetLang = 'C++';
    cfg.DeepLearningConfig = dlconfig;
  3. Run the codegen command. Use the -config option to specify the configuration object. Use the -args option to specify the input type.

    codegen -config cfg my_predict -args {myInput} -report

    Note

    You can specify half-precision inputs for code generation. However, the code generator type casts the inputs to single-precision. The Deep Learning Toolbox uses single-precision, floating-point arithmetic for all computations in MATLAB.

Code Generation by Using the MATLAB Coder App

  1. Follow the usual steps for specifying the entry-point function and specifying input types. See Generate C Code by Using the MATLAB Coder App.

  2. In the Generate Code step:

    • Set Language to either C or C++.

    • Click More Settings. In the Deep Learning pane, set Target library to None.

  3. Generate code.

Relocating DNN Constants

When you generate generic C/C++ 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

| |

Related Topics