Exporting a custom trained network to C#. MATLAB coder or ONNX?

8 visualizaciones (últimos 30 días)
undefined undefined
undefined undefined el 26 de Abr. de 2024
Respondida: Ram Kokku el 6 de Mayo de 2024
I have a simple working trained custom network code that does a great job for me in classifying new data (arrays of 202 integers which represent some physical curves, which need to be classified to either 'good' or 'bad' curves).
I have used a starightforward patternnet(30) network.
Now I need to delpoy my prediction function into a C# project. I relized that matlab coder can't handle loading and training networks, so I have to save my trained network via Matlab and then load it through C# and run there the predict function, which matlab coder will hopefully run.
For this reason I realized that I might need to use exportONNXNetwork, but it forces me to use other network types (and not patternnet) such as SeriesNework or dlnetwork, which I didnt succeed to manually build layers to produce them.
I have also tried to use both classification learner and deep network designer apps to generate networks code for me that would be exported to ONNX and would also mimic the success of my simple patternnet network, but each time they generate code that the exportONNXNetwork function doesn't like, and produce errors like:
Error using nnet.internal.cnn.onnx.exportONNXNetwork>iValidateNetwork
First argument must be a SeriesNetwork, DAGNetwork, dlnetwork, or layerGraph.
My original code of the working patternnet is:
function [PredictedLabels,PredictedScores]=NeuralNetTLD(handles)
close all;
clear;
load('C:\Users\user\Documents\MATLAB\Data files\GoodMAT_2024');
load('C:\Users\user\Documents\MATLAB\Data files\labels_2024');
load('C:\Users\user\Documents\MATLAB\Data files\BadMAT_2024');
goodId = (strcmp(labels,'GOOD'));
badId = (strcmp(labels,'BAD'));
Alllabels = [badId ; goodId];
DATA = [BadMAT1 GoodMAT1];
DATA=DATA(1:200,:);
DATA(201,:)=skewness(DATA,0);
DATA(202,:)=kurtosis(DATA,0);
net = patternnet(30);
net = configure(net,DATA,Alllabels);
net.divideParam.trainRatio = 60/100;
net.divideParam.valRatio = 20/100;
net.divideParam.testRatio = 20/100;
rng(1);
[net,tr] = train(net,DATA,Alllabels);
y = net(DATA);
predictions =net(trial);
figure;plot([1:202], trial(:,1:4)); figure; plot([1:202], trial(:,5));
PredictedScores= y(2,:)
PredictedLabels=labels;
end
I would love to hear a simple solution for this problem, as I think deploying a prediction function of a simple network to C# should be quite a common task to perform.
Thanks in advance,
Gal

Respuestas (1)

Ram Kokku
Ram Kokku el 6 de Mayo de 2024
@undefined undefined, MATLAB Coder can generate code for trained deep learning models. You can use coder.loadDeepLearningNetwork API to achieve this https://www.mathworks.com/help/gpucoder/ref/coder.loaddeeplearningnetwork.html. You can find more examples here https://www.mathworks.com/help/coder/deep-learning-with-matlab-coder.html.
Once the C/C++ code is generated, the code can be integrated to into C# project.

Categorías

Más información sobre Deep Learning with Simulink en Help Center y File Exchange.

Productos


Versión

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by