CvDTree train fails (C++)
Hello everyone,
I have been trying to train a decision Tree for quite some time now but I always get an exception as soon as I try to train the tree. I really dont know what is going on and would appreciate any kind of help. I strongly oriented on the example found on this page (http://www.bytefish.de/blog/machine_l...) but I had to modify some things. Here is whats happening:
void decisiontree(cv::Mat& trainingData, cv::Mat& trainingClasses, cv::Mat& testData, cv::Mat& testClasses) {
CvDTree dtree;
//// this section doesnt work/////////
cv::Mat var_type(3, 1, CV_8U);
// define attributes as numerical
var_type.at<unsigned int>(0,0) = CV_VAR_NUMERICAL;
var_type.at<unsigned int>(0,1) = CV_VAR_NUMERICAL;
// define output node as numerical
var_type.at<unsigned int>(0,2) = CV_VAR_NUMERICAL;
////////////////////////////
//instead:
cv::Mat vartype(3, 1, CV_8U, CV_VAR_NUMERICAL);
dtree.train(trainingData,CV_ROW_SAMPLE, trainingClasses, cv::Mat(), cv::Mat(), var_type, cv::Mat(), CvDTreeParams());//this call fails error message below
cv::Mat predicted(testClasses.rows, 1, CV_32F);
for (int i = 0; i < testData.rows; i++) {
const cv::Mat sample = testData.row(i);
CvDTreeNode* prediction = dtree.predict(sample);
predicted.at<float> (i, 0) = prediction->value;
}
cout << "Accuracy_{TREE} = " << evaluate(predicted, testClasses) << endl;
plot_binary(testData, predicted, "Predictions tree");
}
int main() {
cv::Mat trainingData(numTrainingPoints, 2, CV_32FC1);
cv::Mat testData(numTestPoints, 2, CV_32FC1);
cv::randu(trainingData,0,1);
cv::randu(testData,0,1);
cv::Mat trainingClasses = labelData(trainingData, eq);
cv::Mat testClasses = labelData(testData, eq);
plot_binary(trainingData, trainingClasses, "Training Data");
plot_binary(testData, testClasses, "Test Data");
//These algorithms are working finde but are uncommented for the sake of simplicity
//svm(trainingData, trainingClasses, testData, testClasses);
//mlp(trainingData, trainingClasses, testData, testClasses);
//knn(trainingData, trainingClasses, testData, testClasses, 3);
//bayes(trainingData, trainingClasses, testData, testClasses);
decisiontree(trainingData, trainingClasses, testData, testClasses);
cv::waitKey();
return 0;
}
First-chance exception at 0x000007FEFD65B3DD in xxx.exe: Microsoft C++ exception: cv::Exception at memory location 0x000000000012E510.
Falls ein Handler für diese Ausnahme vorhanden ist, kann das Programm möglicherweise weiterhin sicher ausgeführt werden.(If a handler exists for this excepition the programm can probably be exectued safely)
But if I continue I get: Unhandled exception at at 0x000007FEFD65B3DD in xxx.exe: Microsoft C++ exception: cv::Exception at memory location 0x000000000012E510.
Falls ein Handler für diese Ausnahme vorhanden ist, kann das Programm möglicherweise weiterhin sicher ausgeführt werden.
If I continue again I get the first exception and after a while I finally get:
Unbehandelte Ausnahme bei 0x000007FED279B207 (opencv_ml2410d.dll) in xxx.exe: 0xC0000005: Zugriffsverletzung beim Lesen an Position 0x0000000000000030
What is going on with the dll. Could there be any problem with that? I really dont kow how to fix this and would appreciate any help
Thank you
it's not your fault, the code from the blog is unfortunately buggy there:
it must be
var_type.at<unsigned char>(0,0);
etc. notunsigned int