Neural network genfunction and matlab codegen generates incorrect parameters.

7 views (last 30 days)
I have a trained neural network. The input is a array of 62500 rows and the output is an array of 4 rows. (The input is an image of size 250 * 250 reshaped to a single column array of 62500 rows)
Using, this net object I generated its matlab function using a similar syntax as the shown below:
Once, i get the funtion, i tested within matlab and it worked fine.
Generating the C code: My next step to generate the C code is to now define the input and output datatype. Based on the example shown in the neural network genfunction page, I used a similar syntax to generate the code:
x1Type = coder.typeof(double(0),[13 Inf]); % Coder type of input 1
codegen -c -v men_women_netFn.m -config:dll -o libmen_women_nn -args {x1Type}
The code is generated on a linux based machine. The 1st few lines of the generated code looks like:
void men_women_netFn(const char_T x1[7], real_T b_y1[28])
static real_T xp1[437500];
static const real_T dv0[62500] = { 253.0, 253.0, 253.0, 253.0, 253.0, 253.0,
Now what does not make sense: Why do we have an array of x1[7]? should not it just be x1[1] ?
the generated file states:
/* [y1] = men_women_netFn(x1) takes these arguments: */
/* x = 62500xQ matrix, input #1 */
/* and returns: */
/* y = 4xQ matrix, output #1 */
/* where Q is the number of samples. */
Which now mean the number '7' is Q samples, which is 7. why ? The output is also 7 samples, 4 * 7 = 28 == b_y1[28] .
Now, i am sure i have not explicitly stated any sample number, and neither has the genfunction code generate any explicit sample number
function [y1] = men_women_netFn(x1)
%MEN_WOMEN_NETFN neural network simulation function.
% Generated by Neural Network Toolbox function genFunction, 10-Feb-2014 05:22:00.
I am using this with opencv. I am able to compile the code and run it with opencv, but the output from the generated code function are not correct. Further any input i give, the output is exactly the same suggesting the code generated does not seem right.
Help much appreciated.

Accepted Answer

Fred Smith
Fred Smith on 12 Feb 2014
Somehow MATLAB Coder is interpreting {x1Type} as a string of 7 elements, instead of as a cell array with one argument. I don't know why this is happening, but it is consistent with the effect you are seeing.
Try moving this argument earlier in the list. Or try removing the braces. Double-check the spelling to make sure there is nothing mistyped.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by