Ask Your Question

Unhandled exception while machine learning using NormalBayesClassifier

asked 2012-12-08 11:34:49 -0500

ipunished gravatar image


Im trying to implement the bag of words approach using opencv. After making the dictionary I am using the NormalBayesClassifier to train and predict the system.

The code I am using is below:

int _tmain(int argc, _TCHAR* argv[])

Ptr<FeatureDetector> features = FeatureDetector::create("SIFT");
Ptr<DescriptorExtractor> descriptor = DescriptorExtractor::create("SIFT");
Ptr<DescriptorMatcher> matcher = DescriptorMatcher::create("FlannBased");

//defining terms for bowkmeans trainer
TermCriteria tc(CV_TERMCRIT_ITER+CV_TERMCRIT_EPS, 10, 0.001);
int dictionarySize = 100;
int retries = 1;
int flags = KMEANS_PP_CENTERS;
BOWKMeansTrainer bowTrainer(dictionarySize, tc, retries, flags);

BOWImgDescriptorExtractor bowDE(descriptor, matcher);

//**creating dictionary**//

Mat features1, features2;
Mat img = imread("c:\\1.jpg", 0);
Mat img2 = imread("c:\\2.jpg", 0);
vector<KeyPoint> keypoints, keypoints2;
features->detect(img, keypoints);
descriptor->compute(img, keypoints, features1);
descriptor->compute(img2, keypoints2, features2);

Mat dictionary = bowTrainer.cluster();

//**dictionary made**//

//**now training the classifier**//

Mat trainme(0, dictionarySize, CV_32FC1); 
Mat labels(0, 1, CV_32FC1); //1d matrix with 32fc1 is requirement of normalbayesclassifier class

Mat bowDescriptor, bowDescriptor2;
bowDE.compute(img, keypoints, bowDescriptor);
float label = 1.0;
bowDE.compute(img2, keypoints2, bowDescriptor2);

NormalBayesClassifier classifier;
classifier.train(trainme, labels);

//**classifier trained**//

//**now trying to predict using the same trained classifier, it should return 1.0**//

Mat tryme(0, dictionarySize, CV_32FC1);
Mat tryDescriptor;
Mat img3 = imread("2.jpg", 0);
vector<KeyPoint> keypoints3;
features->detect(img3, keypoints3);
bowDE.compute(img3, keypoints3, tryDescriptor);


return 0;

I have prepared the trainme matrix as per the documentation as in each sample in each row. But the problem is that it gives an unhandled exception at this line: classifier.train(trainme, labels);

I tried to find out the cause but could not narrow it down. Any guidance would be greatly appreciated.

Thank you

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2012-12-10 15:51:23 -0500

ipunished gravatar image

I managed to figure it out, the problem lay here: float label = 1.0; as all the images being trained cannot have the same label. The system must be able to distinguish between the images given, thus its best to arrange the images in groups and give the groups the float values.

edit flag offensive delete link more

Question Tools


Asked: 2012-12-08 11:34:49 -0500

Seen: 746 times

Last updated: Dec 10 '12