DTree classifier crashes without error message (Bug #4480)
Description
When try to use the DTrees in OpenCV 3.0 the "train" method crashes without give an error message.
I tried many configurations, but here is a very simple example.
cv::Mat sampleMat = cv::Mat(10,2, CV_32FC1);
cv::Mat labelMat = cv::Mat(10,1, CV_32SC1);
float* sampleData = (float*)sampleMat.data;
int* labelData = (int*)labelMat.data;
(*sampleData++) = 1.f; (*sampleData++) = 1.f; (*labelData++) = 1;
(*sampleData++) = 1.f; (*sampleData++) = 3.f; (*labelData++) = 1;
(*sampleData++) = 2.f; (*sampleData++) = 8.f; (*labelData++) = 1;
(*sampleData++) = 1.f; (*sampleData++) = 23.f; (*labelData++) = 1;
(*sampleData++) = 2.f; (*sampleData++) = 546.f; (*labelData++) = 1;
(*sampleData++) = 4.f; (*sampleData++) = 1.f; (*labelData++) = 2;
(*sampleData++) = 3.f; (*sampleData++) = 3.f; (*labelData++) = 2;
(*sampleData++) = 4.f; (*sampleData++) = 8.f; (*labelData++) = 2;
(*sampleData++) = 4.f; (*sampleData++) = 23.f; (*labelData++) = 2;
(*sampleData++) = 5.f; (*sampleData++) = 546.f; (*labelData++) = 2;
cv::Ptr<cv::ml::TrainData> trainData = cv::ml::TrainData::create(sampleMat, cv::ml::ROW_SAMPLE, labelMat);
cv::Ptr<cv::ml::DTrees> dtree = cv::ml::DTrees::create();
dtree->train(trainData);
History
Updated by Tom Krause over 9 years ago
i found a message in the terminal, maybe this helps a little bit:
"terminate called after throwing an instance of 'std::length_error'
what(): vector::reserve"
Updated by be rak over 9 years ago
i can reproduce it, if we don't set maxdepth, the default value of MAX_INT is used, and it tries to allocate absurd sizes here:
int DTreesImpl::addTree(const vector<int>& sidx ) { size_t n = (params.getMaxDepth() > 0 ? (1 << params.getMaxDepth()) : 1024) + w->wnodes.size(); w->wnodes.reserve(n); w->wsplits.reserve(n); w->wsubsets.reserve(n*w->maxSubsetSize); ...
Updated by Tom Krause over 9 years ago
When setting the maxDepth. Then the program crashes without an error message.
So it seems there is another bug.
Updated by be rak over 9 years ago
yes, true. if CVFolds is left at the default value 10 (or !=0 in general), it crashes in DTreesImpl::calcValue, line 517:
for( i = 0; i < n; i++ ) { int si = _sidx[i]; j = w->cv_labels[si]; k = w->cat_responses[si]; cv_cls_count[j*m + k] += w->sample_weights[si]; }
w->cv_labels is not initialized / empty.
the 2.4 branch assigned the cv_labels: https://github.com/Itseez/opencv/blob/2.4/modules/ml/src/tree.cpp#L2749 , this part seems missing.
Updated by Maksim Shabunin over 9 years ago
Issue has been transferred to GitHub: https://github.com/Itseez/opencv/issues/5070