Random forests CvRTrees
Hi all,
I am experimenting with the random forests class of openCV (CvRTrees) and I am encountering some troubles. In my example code, I simply create training data with two variables and twenty samples, 10 belonging to the first class and 10 belonging to the second class - so the classification task should be very easy.
The training works but when calling the get_train_error() function, the program crashes each time in CvDTreeTrainData::get_ord_var_data.
Did I discover a bug or did I - as a newbie - used the random forests in a wrong way? Did you encounter similar problems?
Thanks a lot in advance, Stefan
int numSamples = 10;
int numFeatures = 2;
cv::Mat trainingData(numSamples*2,numFeatures, CV_32FC1);
cv::Mat trainingClassification(numSamples*2,1, CV_32FC1);
for (int i=0; i<numSamples*2; i+=2)
{
trainingData.at<float>(i,0) = 1.0f;
trainingData.at<float>(i,1) = 0.0f;
trainingClassification.at<float>(i,0) = 1.0f;
trainingData.at<float>(i+1,0) = 0.0f;
trainingData.at<float>(i+1,1) = 1.0f;
trainingClassification.at<float>(i+1,0) = 0.0f;
}
cv::Mat var_type = cv::Mat(numFeatures+1 , 1, CV_8U );
var_type.setTo(cv::Scalar(CV_VAR_NUMERICAL) );
var_type.at<uchar>(numFeatures, 0) = CV_VAR_CATEGORICAL;
CvRTParams params;
CvRTrees rtree;
rtree.train(trainingData, CV_ROW_SAMPLE, trainingClassification,
cv::Mat(), cv::Mat(), var_type, cv::Mat(), params);
std::cout << rtree.get_train_error() << std::endl;
bug, i'd say.
CvRTrees::train_data gets released after train() but it's needed to make the tests in get_train_error()
looks like you are right, but coud you point me to the line, where train_data gets released? I'm desperately trying to find the line...
did not find it either, just the debugger showed, that it's invalid after leaving train()
i think, i found it: rtrees.cpp 842, Mat& _train_data gets copied to CvMat tdata = _train_data
that's a local var, which is invalid after leaving CvRTrees::train()
the member variable data->train_data sees only a pointer to that, which is invalid ( dangling ) after leaving CvRTrees::train()
I came to the same conclusion after digging through the code again, yesterday. I solved it by copying _train_data but it would be better to inrcease the refpointer. Unfortunataly that didn't seem possible or I missed something
ah, ok. did you write an issue ?
No, I didn't. I'm not registered. Could you write one? edit Meanwhile I registered. See my post below