Exporting a custom trained network to C#. MATLAB coder or ONNX?
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:UsersuserDocumentsMATLABData filesGoodMAT_2024’);
load(‘C:UsersuserDocumentsMATLABData fileslabels_2024’);
load(‘C:UsersuserDocumentsMATLABData filesBadMAT_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,
GalI 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:UsersuserDocumentsMATLABData filesGoodMAT_2024’);
load(‘C:UsersuserDocumentsMATLABData fileslabels_2024’);
load(‘C:UsersuserDocumentsMATLABData filesBadMAT_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 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:UsersuserDocumentsMATLABData filesGoodMAT_2024’);
load(‘C:UsersuserDocumentsMATLABData fileslabels_2024’);
load(‘C:UsersuserDocumentsMATLABData filesBadMAT_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 c# deployment, neural network, matlab coder, onnx MATLAB Answers — New Questions