Ask Your Question

Random forests CvRTrees

asked 2012-09-24 14:29:25 -0600

etecman gravatar image

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)
{<float>(i,0) = 1.0f;<float>(i,1) = 0.0f;<float>(i,0) = 1.0f;<float>(i+1,0) = 0.0f;<float>(i+1,1) = 1.0f;<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) );<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;
edit retag flag offensive close merge delete



bug, i'd say.

CvRTrees::train_data gets released after train() but it's needed to make the tests in get_train_error()

berak gravatar imageberak ( 2013-04-18 13:16:47 -0600 )edit

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...

Sarevok gravatar imageSarevok ( 2013-04-22 03:21:56 -0600 )edit

did not find it either, just the debugger showed, that it's invalid after leaving train()

berak gravatar imageberak ( 2013-04-22 03:26:11 -0600 )edit

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()

berak gravatar imageberak ( 2013-04-22 04:05:37 -0600 )edit

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

Sarevok gravatar imageSarevok ( 2013-04-23 04:09:37 -0600 )edit

ah, ok. did you write an issue ?

berak gravatar imageberak ( 2013-04-23 04:28:46 -0600 )edit

No, I didn't. I'm not registered. Could you write one? edit Meanwhile I registered. See my post below

Sarevok gravatar imageSarevok ( 2013-04-23 05:22:57 -0600 )edit

2 answers

Sort by ยป oldest newest most voted

answered 2013-04-18 07:36:42 -0600

Sarevok gravatar image

updated 2013-04-26 03:02:10 -0600

Did you solve the issue? I'm experiencing the same Error here.

* edit *

berak already mentioned and located it. Seems to be a Bug in OpenCV. I created an issue for that:

edit flag offensive delete link more


ah, cool. thanks a lot for doing so.

berak gravatar imageberak ( 2013-04-26 03:13:32 -0600 )edit

answered 2014-01-19 17:03:30 -0600

jossyy gravatar image

Are the decision trees and random forest same method?

edit flag offensive delete link more


I found this answer .. The random tree is a tree multitude of decision trees.

jossyy gravatar imagejossyy ( 2014-01-19 19:06:25 -0600 )edit

Question Tools


Asked: 2012-09-24 14:29:25 -0600

Seen: 2,467 times

Last updated: Jan 19 '14