Random Forest - Vector subscript out range during training

asked 2016-02-22 06:27:56 -0600

I am trying OpenCV (3.0) random forest implementation for the first time. The forest->train always throws the "vector subscript out of range error". My code is here,

cv::ml::RTrees::Params params = cv::ml::RTrees::Params::Params();  
cv::Ptr<cv::ml::RTrees> forest = cv::ml::RTrees::create();
forest->train(trainingData, cv::ml::ROW_SAMPLE, trainingClass);

The trainingData is (CV_32F, 1,24,1800) and the trainingClass is (CV_32F, 1,1,1800). 24 is the number of features and 1800 is the total number of samples.

Even if I changed the layout to COL_SAMPLE, it throws an error like " Assertion failed <<layout =="ROW_SAMPLE" &amp;&amp;="" responses.rows="=" nsamples=""> || <layout =="COL_SAMPLE" &amp;&amp;="" responce.cols="=" nsamples="">

Where am I wrong?. Please suggest a solution.

edit retag flag offensive close merge delete


  • there are no more Params in opencv3
  • what's (CV_32F, 1,24,1800) meant to mean exactly ? please show, how you setup your data
berak gravatar imageberak ( 2016-02-22 06:54:29 -0600 )edit

Thanks Berek. This is how my data are initialized,

    trainingData = cv::Mat::zeros(totalSamples, 24, CV_32F);
trainingClass = cv::Mat::zeros(totalSamples, 1, CV_32F);

The total samples may be any value (say 1800). The values of the trainingData and trainingClass are updated using a forloop.

I_AM_BINARY gravatar imageI_AM_BINARY ( 2016-02-22 22:41:49 -0600 )edit

your code runs fine here.

berak gravatar imageberak ( 2016-02-23 00:35:48 -0600 )edit

Thanks for testing. I am still having the same issue. Could it be a dll problem?

I_AM_BINARY gravatar imageI_AM_BINARY ( 2016-02-23 02:30:01 -0600 )edit