Error running matlab convolutional network from c#

I am trying to develop a Xamarin mobile app for android that recognizes objects on images taken for the user, basically I already trained the CNN on matlab and the plan is to call it from a c# web service!.
I wrote this piece of code in a .m file , and I converted it into a .net assembly using the Matlab deploying tool.
function label = RedNeuronal()
load('E:xxxx\capa.mat');
load('E:xxxx\CNN.mat');
load('E:xxxx\SVM.mat');
newImage ='E:xxxxx\GatoBoyaca.jpg';
I = imread(newImage);
if ismatrix(I)
I = cat(3,I,I,I);
end
img = imresize(I, [227 227]);
imageFeatures = activations(convnet, img, featureLayer);
label = char(predict(classifier, imageFeatures));
end
As you can notice this code simply loads the CNN , the name of the feature layer and also a SVM, finally it preprocess the image and get the result using the predict command. If i run this .m fie from matlab it runs perfectly.
On the other hand, in c# I wrote this piece of code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MathWorks.MATLAB.NET.Arrays;
using MathWorks.MATLAB.NET.Utility;
using RedNeuronalComp;
namespace CargarDll
{
class Program
{
static void Main(string[] args)
{
MWArray SalidaCNN = null;
MLCnnClass CNN = new MLCnnClass();
SalidaCNN=CNN.RedNeuronal();
}
}
}
But Sadly this code gives me the following error:
It seems that it can not use the CNN , like if it would not have access to the toolbox or something. What should I do ?

Antworten (1)

Walter Roberson
Walter Roberson am 5 Jan. 2017

0 Stimmen

I advise against using load() without an output parameter in a compiled module. You are relying upon variables being "poofed" into existence, which is something that the compiler can have difficulty in figuring out.

6 Kommentare

Do you mean write something like:
Convnet = load('E:xxxx\CNN.mat');
Do you think that convolutional neural networks are already supported for deploying?
As soon you answer I will try your method, thank you.
Hi Walter, I have been working in what you said and I tried with this piece of code:
function label = ActivationsDos()
featureLayer = 'fc7';
net=load('E:xxxxx\CNN.mat');
convnet = net.convnet;
newImage = 'xxxxxxs\GatoBoyaca.jpg';
I = imread(newImage);
if ismatrix(I)
I = cat(3,I,I,I);
end
img = imresize(I, [227 227]);
imageFeatures = activations(convnet, img, featureLayer);
label = 'yes';
end
As you can notice I am getting just the image features , It seem that you were right, maybe I need to avoid using a simply load(), because It always loads the file as a structure, but I tried to cast it into Series network, ofcourse it works fine in matlab but stills not working in c sharp. How could I load this .mat file ?
That is the sort of code, yes.
Sorry, I do not have any experience with C#
But do you know if it is possible to load a "Network Series Object" Directly as a networkSeries object using
xxx = load(cnnpath)
?
No, the output of the function form of load() is always a structure. You would reference the appropriate field in the structure to get the object.
Guillaume
Guillaume am 5 Jan. 2017
Note that the problem is not with C# per say, the error messages shown are not typical CLR error messages, so must come from the interface provided by matlab.
Like Walter, my first suspicion is that the load confuse matlab code generator which probably does not know where the variables come from and thus fail to include some necessary code. This is the first thing that I'd change. It's always good practice to load variables into a structure anyway (to avoid overwriting existing variables / functions).
There may be some other deeper issues like matlab coder not supporting some of the functions you're using. I wouldn't know, I do not have or have ever used matlab compiler SDK.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu WSNs finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 4 Jan. 2017

Kommentiert:

am 5 Jan. 2017

Community Treasure Hunt

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

Start Hunting!

Translated by