opencv C++ neural network predict() function throws "Bad argument" error -


i have managed train neural network recognize numbers in image , have saved network parameters .xml file.

however, when testing network against new image code fails @ predict() stage error:

opencv error: bad argument (both input , output must floating-point matrices of same type , have same number of rows) in cvann_mlp::predict, file ........\opencv\modules\ml\src\ann_mlp.cpp, line 279.

ann_mlp.cpp line 279 is:

if( !cv_is_mat(_inputs) || !cv_is_mat(_outputs) ||     !cv_are_types_eq(_inputs,_outputs) ||     (cv_mat_type(_inputs->type) != cv_32fc1 &&     cv_mat_type(_inputs->type) != cv_64fc1) ||     _inputs->rows != _outputs->rows )     cv_error( cv_stsbadarg, "both input , output must floating-point matrices "                             "of same type , have same number of rows" ); 

i have checked input rows running code:

cv::size s = newvec.size();     int rows = s.height;     int cols = s.width;     cout << "newvec dimensions: " << rows << " x " << cols << endl; 

...and comes out expected 1 x 900 vector / matrix.

i have set input , output matrices cv_32fc1 per error dialog this:

input matrix

cv::mat newvec(1, 900, cv_32fc1);     newvec = crop_img.reshape(0, 1); //reshape / unroll image vector     cvmat n = newvec;     newvec = cv::mat(&n); 

output matrix

    cv::mat classout = cvcreatematheader(1, classes, cv_32fc1); 

and try run prediction function this:

cvann_mlp* nnetwork = new cvann_mlp; nnetwork->load("nnetwork.xml", "nnetwork");  int maxindex = 0; cv::mat classout = cvcreatematheader(1, classes, cv_32fc1);  //prediction nnetwork->predict(newvec, classout);     float value;     float maxvalue = classout.at<float>(0, 0);     (int index = 1; index<classes; index++)     {         value = classout.at<float>(0, index);         if (value>maxvalue)         {             maxvalue = value;             maxindex = index;         }     } 

any ideas? appreciated...

i suspect problem input, not output.

first it's important understand opencv deserves lot of blame this, not you. c++ api quite mediocre, , caused major confusion you.

see, in c++ when define 1x900 matrix of floats, stays matrix of floats. c++ has strong type safety.

opencv not. if assign matrix of bytes matrix of floats, latter change type (!). code initializes newvec such matrix of floats, assigns second matrix, , yet matrix. suspect crop_img still image, i.e. 8 bits. reshaping make 1x900, not floating point. that's job of .convertto.


Comments

Popular posts from this blog

cakephp - simple blog with croogo -

How to group boxplot outliers in gnuplot -

bash - Performing variable substitution in a string -