Ask Your Question
0

Checking the Sample Size Input for `cv::ml::StatModel::predict()` Loaded from File?

asked 2018-10-28 04:58:06 -0600

Adi gravatar image

updated 2018-10-28 05:21:35 -0600

I'm loading an ML trained model from a file.
I noticed that when calling predict() with an input sample of the wrong size, the function does not (necessarily) fail! Apparently there is no internal checking that the input is of the right size!

I want to verify that when I'm calling predict(), my input sample is of the correct size. How can I do that? I cannot seem to find such a method to provide this info.

OpenCV 3.4. Some "sample" code:

cv::Ptr<cv::ml::StatModel> classifier = cv::Algorithm::load(...); 
// ...
cv::Mat sample32f;  // fill sample32f
//...
classifier->predict(sample32f.reshape(1,1)); // How to verify the correct expected size???
edit retag flag offensive close merge delete

Comments

some code to reproduce it would be nice here ..

(StatModel is an abstract interface, we'd have to look at the actual implementation, like SVM, DTree, or such)

also: opencv version ?

berak gravatar imageberak ( 2018-10-28 05:15:41 -0600 )edit

See above tho this is a question about API, so the code is rather simple/obvious...

Adi gravatar imageAdi ( 2018-10-28 05:22:09 -0600 )edit
1

Yes, StatModel is an abstract interface, but all derived classes expect a model of some known size.

Adi gravatar imageAdi ( 2018-10-28 05:23:29 -0600 )edit

but which class has the missing size check ? (it probably needs to be reported)

berak gravatar imageberak ( 2018-10-28 05:33:32 -0600 )edit

I misunderstood the meaning of getVarCount(). I thought it was related to the number of examples in the training phase: "Returns the number of variables in training samples." That is apparently the right answer. Thanks!

Adi gravatar imageAdi ( 2018-10-28 05:36:27 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2018-10-28 05:31:32 -0600

berak gravatar image

you can retrieve the size of your train samples with getVarCount() like this:

Mat td(20,13,CV_32F);
Mat tl(20,1,CV_32S);

Ptr<ml::StatModel> mdl = ml::SVM::create();
mdl->train(td,0,tl);

cout << mdl->getVarCount() << endl;
// result: 13

and if you found a missing size check somewhere, it should go to : https://github.com/opencv/opencv/issu...

edit flag offensive delete link more

Comments

1

We can continue the discussions here: https://github.com/opencv/opencv/issu...

Adi gravatar imageAdi ( 2018-10-28 05:45:18 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-10-28 04:58:06 -0600

Seen: 183 times

Last updated: Oct 28 '18