opencv / opencv

Open Source Computer Vision Library
https://opencv.org
Apache License 2.0
77.03k stars 55.67k forks source link

Checking the required input sample size for `cv::ml::StatModel::predict()` for a StatModel loaded from a file #12974

Closed adishavit closed 3 years ago

adishavit commented 5 years ago

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 that matches the one expected by the loaded model!

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.

adishavit commented 5 years ago

So, apparently the right function is getVarCount(). However, it is strange that predict() does not fail when there is a mismatch.

Specifically, my example, I trained ml::Boost to classify vectors of size 5547 but it doesn't fail when called mistakenly with smaller samples like 1323. Silent success is horrible.

I added a safety line: e.g. assert(classifier->getVarCount() == sample.cols);

danielenricocahall commented 3 years ago

@alalek this can be closed, as my PR addressed this issue.