NormalBayesClassifier Predict Errors

asked 2015-06-22 07:22:16 -0500

blDraX gravatar image

I'm trying to get a NormalBayesClassifier running and by now have the impression that I'm using something about this class fundamentally wrong.

So far, this is the (complete) code:

#include <opencv2/ml/ml.hpp>

int main()
{
    cv::Ptr<cv::ml::NormalBayesClassifier> bayes = cv::ml::NormalBayesClassifier::create();
    cv::Mat_<float> trainFeatures(2, 2);
    cv::Mat_<int> trainClasses(2, 1);

    trainFeatures.at<float>(0, 0) = 0;
    trainFeatures.at<float>(0, 1) = 0;
    trainFeatures.at<float>(1, 0) = 1;
    trainFeatures.at<float>(1, 1) = 1;
    trainClasses.at<int>(0, 0) = 0;
    trainClasses.at<int>(1, 0) = 1;

    bayes->train(trainFeatures, cv::ml::ROW_SAMPLE, trainClasses);

    cv::Mat_<float> test(2, 2);

    test.at<float>(0, 0) = 0;
    test.at<float>(0, 1) = 0;
    test.at<float>(1, 0) = 0;
    test.at<float>(1, 1) = 0;

    bayes->predict(test);
}

If I try do run this, the program crashes (in Debug mode) at the predict statement with the following exception:

Run-Time Check Failure #2 - Stack around the variable 'value' was corrupted.

Ouch.

If I only test a single data case, I don't get a crash but still weird exceptions (sorry for the German, but you should get the point):

// ...same as above...
cv::Mat_<float> test(1, 2);

test.at<float>(0, 0) = 0;
test.at<float>(0, 1) = 0;

bayes->predict(test);

Results in:

Ausnahme (erste Chance) bei 0x000007FEFCD6B3DD in Components.exe: Microsoft C++-Ausnahme: cv::Exception bei Speicherort 0x000000000013D990.
Ausnahme (erste Chance) bei 0x000007FEFCD6B3DD in Components.exe: Microsoft C++-Ausnahme: cv::Exception bei Speicherort 0x000000000013F460.

Ultimately I want to do something like this:

// ...same as above...
cv::Mat_<float> test(2, 2);
cv::Mat result;
cv::Mat resultP;

test.at<float>(0, 0) = 0;
test.at<float>(0, 1) = 0;
test.at<float>(1, 0) = 0;
test.at<float>(1, 1) = 0;

bayes->predictProb(test, result, resultP);

Needless to say this also doesn't work. OpenCV prints an error for that:

OpenCV Error: Null pointer (When the number of input samples is >1, the output vector of results must be passed) in cv::ml::NormalBayesClassifierImpl::predictProb, file C:\builds\master_PackSlave-win64-vc12-shared\opencv\modules\ml\src\nbayes.cpp, line 318

Can anyone see what I'm doing wrong? I use an SVM Classifier and an KNN Classifier in exactly the same way. They work like charms. I'm using OpenCV3 Gold Release by the way (but had the same errors in RC1).

edit retag flag offensive close merge delete

Comments

i can reproduce it, so it's probably not your fault.

the 2nd one is rather easy, it obviously should be: if( samples.rows > 1 && ! _results.needed() ) in nbayes.cpp, L316

(this also removes the buffer overrun)

berak gravatar imageberak ( 2015-06-22 07:44:41 -0500 )edit

please make an official issue here , also, if you could prepare a pull request with the fix already, you'd be our hero ;)

berak gravatar imageberak ( 2015-06-22 08:03:38 -0500 )edit