How can I deploy a 'SeriesNetwork' into C/C++ Code?

9 views (last 30 days)
I successfully trained a LSTM-neural network in MATLAB R2018a with the following code:
numResponses = size(YTrain{1},1);
featureDimension = size(XTrain{1},1);
numHiddenUnits = 200;
layers = [ ...
sequenceInputLayer(featureDimension)
lstmLayer(numHiddenUnits,'OutputMode','sequence')
fullyConnectedLayer(50)
dropoutLayer(0.5)
fullyConnectedLayer(numResponses)
regressionLayer];
maxEpochs = 100;
miniBatchSize = 32;
options = trainingOptions('adam', ...
'MaxEpochs',maxEpochs, ...
'MiniBatchSize',miniBatchSize, ...
'InitialLearnRate',0.01, ...
'GradientThreshold',1, ...
'Shuffle','never', ...
'Plots','training-progress',...
'Verbose',0);
net = trainNetwork(XTrain,YTrain,layers,options);
Is there a way to export the trained network into C/C++ code? The function "genFunction" is only applicable for type "Network". When I try to use it on the lstm-network ("SeriesNetwork"), I get following error:
genFunction(net, 'simulateStandaloneNetTest.m', 'MatrixOnly','yes', 'ShowLinks','no')
"Undefined function 'genFunction' for input arguments of type 'SeriesNetwork'."
This thread has a similar question but no (sufficient) answer:
https://de.mathworks.com/matlabcentral/answers/321647-is-there-a-way-to-c-code-a-trained-cnn-to-be-used-in-a-c-c-program
I would like to add the C++ source code in my Microsoft Visual Studio project and predict there the output of the network.
Thank you very much in advance!
Domi
  2 Comments
Steven Lord
Steven Lord on 24 Aug 2022
The list of functions in Deep Learning Toolbox that support the extended capability of C/C++ Code Generation (using MATLAB Coder) is here. Quickly skimming the list I see that lstmLayer does have this support, although there are some notes on its documentation page related to that functionality. softmaxLayer and bilstmLayer also appear in that list of functions with C/C++ Code Generation support.

Sign in to comment.

Answers (2)

Bill Chou
Bill Chou on 28 Jun 2018
In R2018a, GPU Coder added support to generate C/C++ code from deep learning networks for Intel and ARM processors. Specifically, it requires Intel processors that takes advantage of Intel® Math Kernel Library for Deep Neural Networks (MKL-DNN) and ARM processors that supports the ARM® Compute libraries for mobile platforms. This essentially means Intel Xeon processors and ARM Cortex A processors.
More info here:

Ryan Livingston
Ryan Livingston on 25 Jun 2018
Deploying these networks with MATLAB Coder is currently not supported (as of MATLAB R2018a when I write this).
A workaround is shown at:
Depending on the work you are doing, GPU Coder provides functionality to deploy some deep learning networks:

Categories

Find more on Deep Learning Toolbox in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!