2

I am doing some machine learning in OpenCV and i'm using Decision Trees. I am currently using OpenCV 3.0.0-rc1. Whenever i attempt to train Decision Trees with my training data and labels, i get either

terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc

or

Segmentation fault

Depending on what i put into setMaxDepth(); if the number is larger than 22, it's bad_alloc, else it's seg fault. Here's my source code:

//import data
Mat trainData=imread("/home/jetson/Documents/CB/ml/td.jpg",CV_LOAD_IMAGE_GRAYSCALE);
Mat labels=imread("/home/jetson/Documents/CB/ml/lab.jpg",CV_LOAD_IMAGE_GRAYSCALE);
//convert to the right type
trainData.convertTo(trainData,CV_32FC1);
labels.convertTo(labels,CV_32SC1);
transpose(trainData,trainData);
Ptr<ml::TrainData> tData = ml::TrainData::create(trainData, ml::ROW_SAMPLE, labels);

cout <<"Training data ready\n";

Ptr<ml::DTrees> dec_trees = ml::DTrees::create();
//params
dec_trees->setMaxDepth(1);
dec_trees->setMinSampleCount(10);
dec_trees->setRegressionAccuracy(0.01f);
dec_trees->setUseSurrogates(false);
dec_trees->setMaxCategories(2);
dec_trees->setCVFolds(10);
dec_trees->setUse1SERule(true);
dec_trees->setTruncatePrunedTree(true);
dec_trees->setPriors(Mat());

cout <<"Params set\n";
dec_trees->train(tData);
cout <<"Done!\n";`

In addition to this, when i try to train a SVM model with the same data, using the same steps (below) it works just fine.

Ptr<ml::SVM> svm = ml::SVM::create();
//params
svm->setType(ml::SVM::C_SVC);
svm->setKernel(ml::SVM::POLY);
svm->setGamma(3);
svm->setDegree(0.1);

cout <<"Params set\n";
svm->train(tData);
cout <<"Done!\n";

I need to point out that the error occurs when i try to train the model. I'm using the default parameters for decision trees, as suggested on the OpenCV documentation page. Does anybody know what's wrong here and how to go about fixing my problem?

Thanks in advance.

EDIT: I upgraded OpenCV to version 3.0.0 and the issues stay the same

KinbodoP
  • 21
  • 5
  • Here I guess u are doing classification. I am new to Opencv. Can u tell what we will will put in the place of the setMaxCategories in case we are doing regression –  Oct 22 '15 at 13:47

0 Answers0