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

1 次查看(过去 30 天)
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 个评论
Claudio Aguiar
Claudio Aguiar 2022-8-23
After spening 18 hours (twice) learning how to train a LSTM neural network at MATLAB (finished self-pace course deep learning with matlab for the second time), I am pleased to find out that there is no pratical way to export that "seriesNetwork" out of matlab to use in any useful c-code. What a waste of my time. In a another post the support team says: "Furthermore, the objects that MATLAB uses to represent Neural Networks are transparent, and you can therefore access all the information that describes your trained network." OK, but you should have told that before I even tried, since you never told me how to use any of this information, and I have no idea how to code a "softmax" or "biLSTM" layer.
Steven Lord
Steven Lord 2022-8-24
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.

请先登录,再进行评论。

回答(2 个)

Bill Chou
Bill Chou 2018-6-28
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 2018-6-25
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:

类别

Help CenterFile Exchange 中查找有关 Deep Learning Code Generation Fundamentals 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by