Error when using Decision Trees in OpenCV 3.0.0-rc1

扶醉桌前 提交于 2021-02-19 06:01:15

问题


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

来源:https://stackoverflow.com/questions/31808495/error-when-using-decision-trees-in-opencv-3-0-0-rc1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!