Main Content

Update Network Parameters After Code Generation

This example shows how to update learnable and state parameters of deep learning networks without regenerating code for the network. You can update the network parameters for SeriesNetwork, DAGNetwork and dlnetwork.

Parameter update supports MEX and standalone code generation for the Intel® Math Kernel Library for Deep Neural Networks (MKL-DNN) and the ARM® Compute libraries.

Create an Entry-Point Function

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

    1. Uses the coder.loadDeepLearningNetwork function to construct and set up a convolutional neural network (CNN) object. For more information, see Load Pretrained Networks for Code Generation.

    2. Calls predict (Deep Learning Toolbox) to predict the responses.

  2. For example:

    function out = mLayer(in, matFile)
    
    myNet = coder.loadDeepLearningNetwork(coder.const(matFile));
    
    out = myNet.predict(in); 
    

Create a Network

The network used in this example requires input images of size 4-by-5-by-3. Create sample network inputs of the same size format as the network inputs.

inputSize = [4 5 3];
im = dlarray(rand(inputSize, 'single'), 'SSCB');

Define the network architecture.

outSize = 6;
layers = [
    imageInputLayer(inputSize,'Name','input','Normalization','none')
    convolution2dLayer([3 3], 5, 'Name', 'conv-1')
    batchNormalizationLayer('Name', 'batchNorm')
    reluLayer('Name','relu1')
    transposedConv2dLayer([2 2], 5, 'Name', 'transconv')
    convolution2dLayer([2 2], 5, 'Name', 'conv2')
    reluLayer('Name','relu2')
    fullyConnectedLayer(outSize, 'Name', 'fc3')
    ];

Create an initialized dlnetwork object from the layer graph.

rng(0);
dlnet1 = dlnetwork(layers);
save('trainedNet.mat', 'dlnet1');

Code Generation by Using codegen

  1. To configure build settings such as output file name, location, and type, you create coder configuration objects. To create the objects, use the coder.config function.

  2. To specify code generation parameters for MKL-DNN, set the DeepLearningConfig property to a coder.MklDNNConfig object that you create with coder.DeepLearningConfig

    cfg = coder.config('mex');
    cfg.TargetLang = 'C++';
    cfg.DeepLearningConfig = coder.DeepLearningConfig('TargetLibrary', 'mkldnn')
    

  3. Specify the inputs.

    cnnMatFile = fullfile(pwd, 'trainedNet.mat');
    inputArgs = {im, coder.Constant(cnnMatFile)};
    

  4. Run the codegen command. The codegen command generates CUDA® code from the mLayers.m MATLAB entry-point function.

    codegen -config cfg mLayer -args inputArgs -report
    

Run the Generated MEX

Call predict on the input image and compare the results with MATLAB.

out = mLayer_mex(im,cnnMatFile)
out_MATLAB = mLayer(im,cnnMatFile)
out1 = 

  6(C) x 1(B) single dlarray

   -0.0064
   -0.1422
   -0.0897
    0.2223
    0.0329
    0.0365


out_MATLAB = 

  6(C) x 1(B) single dlarray

   -0.0064
   -0.1422
   -0.0897
    0.2223
    0.0329
    0.0365

Update Network with Different Learnable Parameters

Re-initialize dlnetwork to update learnables to different values.

rng(10);
dlnet2 = dlnetwork(layers);
save('trainedNet.mat', 'dlnet2');

Use the coder.regenerateDeepLearningParameters function to regenerate the bias files based on the new learnables and states of the network.

The first input to the coder.regenerateDeepLearningParameters function is a SeriesNetwork, DAGNetwork or dlnetwork object. The second argument is the path to the network parameter information file emitted during code generation. You can optionally specify the NetworkName=MYNET name-value pair to specify the name of the C++ class for the network in the generated code.

codegenDir = fullfile(pwd, 'codegen/mex/mLayer');
networkFileNames = (coder.regenerateDeepLearningParameters(dlnet2, codegenDir))'

The coder.regenerateDeepLearningParameters function returns a cell-array of files containing network learnables and states.

networkFileNames = 

  8×1 cell array

    {'cnn_trainedNet0_0_conv-1_b.bin'   }
    {'cnn_trainedNet0_0_conv-1_w.bin'   }
    {'cnn_trainedNet0_0_conv2_b.bin'    }
    {'cnn_trainedNet0_0_conv2_w.bin'    }
    {'cnn_trainedNet0_0_fc3_b.bin'      }
    {'cnn_trainedNet0_0_fc3_w.bin'      }
    {'cnn_trainedNet0_0_transconv_b.bin'}
    {'cnn_trainedNet0_0_transconv_w.bin'}

Note

For MEX workflows, when the generated MEX and the associated codegen folder is moved from one location to another, coder.regenerateDeepLearningParameters cannot regenerate files containing network learnables and states parameters in the new location. Set the 'OverrideParameterFiles' parameter of coder.regenerateDeepLearningParameters to true to allow the coder.regenerateDeepLearningParameters function to regenerate files containing network learnables and states parameters in the original codegen location.

For standalone workflows, coder.regenerateDeepLearningParameters can regenerate files containing network learnables and states parameters in the new location

Run the Generated MEX with Updated Learnables

Call predict on the input image and compare the results with MATLAB.

clear mLayer_mex;
outNew = mLayer_mex(im,cnnMatFile)
outNew_MATLAB = mLayer(im,cnnMatFile)
outNew = 

  6(C) x 1(B) single dlarray

    0.1408
   -0.0080
    0.0342
   -0.0065
    0.1843
    0.0799


outNew_MATLAB = 

  6(C) x 1(B) single dlarray

    0.1408
   -0.0080
    0.0342
   -0.0065
    0.1843
    0.0799

Limitations

Only the network learnables and states can be updated by using the coder.regenerateDeepLearningParameters function. For modifications that the code generator does not support, an error message is thrown. For example, using coder.regenerateDeepLearningParameters after changing the scale factor of a leaky ReLU layer throws the following error message as scale factor is not a network learnable.

Network architecture has been modified since the last code generation. Unable 
to accommodate the provided network in the generated code. Regenerate code 
for the provided network to reflect changes in the network. For more 
information, see Limitations to Regenerating Network Parameters After Code Generation.

See Also

Functions

Objects

Related Examples

More About