Error running matlab convolutional network from c#

3 views (last 30 days)
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 ?

Answers (1)

Walter Roberson
Walter Roberson on 5 Jan 2017
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 Comments
Walter Roberson
Walter Roberson on 5 Jan 2017
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 on 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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!