Ask Your Question

Revision history [back]

Trivial random forest with OpenCV isn't the same as sklearn

I'm trying to get the simplest example of random forest to work. The training data is 2 points {0,0} with a label 0 and {1,1} with a label 1. The sample to predict is {2,2}. OpenCV returns 0 rather than 1. Here is the OpenCV code in C++ (main.cpp):

#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/ml/ml.hpp>

using namespace std;
using namespace cv;

int main(int argc, char const *argv[]) {
  cout << " hi \n";
  float trainingData[2][2] = { {0.0, 0.0}, {1.0, 1.0}};
  Mat training_data(2, 2, CV_32FC1, trainingData);
  float trainingClass[2] = {0.0,1.0};
  Mat training_class(2, 1, CV_32FC1, trainingClass);
  CvRTrees rtree;
  rtree.train(training_data, CV_COL_SAMPLE, training_class);
  float sampleData[2] = {2.0, 2.0};
  Mat sample_data(2, 1, CV_32FC1, sampleData);
  cout << rtree.predict(sample_data) << "  <-- predict\n";
  return 0;
}

cmake file:

cmake_minimum_required(VERSION 2.8)
project( main )
find_package( OpenCV REQUIRED )
add_executable( main main.cpp )
target_link_libraries( main ${OpenCV_LIBS} )

running:

> cmake .;make;./main
 hi 
0  <-- predict

To compare, here is a python's sklearn code (rfc.py):

from sklearn.ensemble import RandomForestClassifier
X = [[0, 0], [1, 1]]
Y = [0, 1]
clf = RandomForestClassifier(n_estimators=10)
clf = clf.fit(X, Y)
print clf.predict([[2., 2.]])

running:

> python rfc.py 
[1]

Trivial random forest with OpenCV doesn't work and isn't the same as sklearn

I'm trying to get the simplest example of random forest to work. The training data is 2 points {0,0} with a label 0 and {1,1} with a label 1. The sample to predict is {2,2}. OpenCV returns 0 rather than 1. Here is the OpenCV code in C++ (main.cpp):

#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/ml/ml.hpp>

using namespace std;
using namespace cv;

int main(int argc, char const *argv[]) {
  cout << " hi \n";
  float trainingData[2][2] = { {0.0, 0.0}, {1.0, 1.0}};
  Mat training_data(2, 2, CV_32FC1, trainingData);
  float trainingClass[2] = {0.0,1.0};
  Mat training_class(2, 1, CV_32FC1, trainingClass);
  CvRTrees rtree;
  rtree.train(training_data, CV_COL_SAMPLE, training_class);
  float sampleData[2] = {2.0, 2.0};
  Mat sample_data(2, 1, CV_32FC1, sampleData);
  cout << rtree.predict(sample_data) << "  <-- predict\n";
  return 0;
}

cmake file:

cmake_minimum_required(VERSION 2.8)
project( main )
find_package( OpenCV REQUIRED )
add_executable( main main.cpp )
target_link_libraries( main ${OpenCV_LIBS} )

running:

> cmake .;make;./main
 hi 
0  <-- predict

To compare, here is a python's sklearn code (rfc.py):

from sklearn.ensemble import RandomForestClassifier
X = [[0, 0], [1, 1]]
Y = [0, 1]
clf = RandomForestClassifier(n_estimators=10)
clf = clf.fit(X, Y)
print clf.predict([[2., 2.]])

running:

> python rfc.py 
[1]
click to hide/show revision 3
should be ROW not COL.

Trivial random forest with OpenCV doesn't work and isn't the same as sklearn

I'm trying to get the simplest example of random forest to work. The training data is 2 points {0,0} with a label 0 and {1,1} with a label 1. The sample to predict is {2,2}. OpenCV returns 0 rather than 1. Here is the OpenCV code in C++ (main.cpp):

#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/ml/ml.hpp>

using namespace std;
using namespace cv;

int main(int argc, char const *argv[]) {
  cout << " hi \n";
  float trainingData[2][2] = { {0.0, 0.0}, {1.0, 1.0}};
  Mat training_data(2, 2, CV_32FC1, trainingData);
  float trainingClass[2] = {0.0,1.0};
  Mat training_class(2, 1, CV_32FC1, trainingClass);
  CvRTrees rtree;
  rtree.train(training_data, CV_COL_SAMPLE, CV_ROW_SAMPLE, training_class);
  float sampleData[2] = {2.0, 2.0};
  Mat sample_data(2, 1, CV_32FC1, sampleData);
  cout << rtree.predict(sample_data) << "  <-- predict\n";
  return 0;
}

cmake file:

cmake_minimum_required(VERSION 2.8)
project( main )
find_package( OpenCV REQUIRED )
add_executable( main main.cpp )
target_link_libraries( main ${OpenCV_LIBS} )

running:

> cmake .;make;./main
 hi 
0  <-- predict

To compare, here is a python's sklearn code (rfc.py):

from sklearn.ensemble import RandomForestClassifier
X = [[0, 0], [1, 1]]
Y = [0, 1]
clf = RandomForestClassifier(n_estimators=10)
clf = clf.fit(X, Y)
print clf.predict([[2., 2.]])

running:

> python rfc.py 
[1]

Trivial random forest with OpenCV doesn't work and isn't the same as sklearn

I'm trying to get the simplest example of random forest to work. The training data is 2 points {0,0} with a label 0 and {1,1} with a label 1. The sample to predict is {2,2}. OpenCV returns 0 rather than 1. Here is the OpenCV code in C++ (main.cpp):

#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/ml/ml.hpp>

using namespace std;
using namespace cv;

int main(int argc, char const *argv[]) {
  cout << " hi \n";
  float trainingData[2][2] = { {0.0, 0.0}, {1.0, 1.0}};
  Mat training_data(2, 2, CV_32FC1, trainingData);
  float trainingClass[2] = {0.0,1.0};
  Mat training_class(2, 1, CV_32FC1, trainingClass);
  CvRTrees rtree;
  rtree.train(training_data, CV_ROW_SAMPLE, training_class);
  float sampleData[2] = {2.0, 2.0};
  Mat sample_data(2, 1, CV_32FC1, sampleData);
  cout << rtree.predict(sample_data) << "  <-- predict\n";
  return 0;
}

cmake file:

cmake_minimum_required(VERSION 2.8)
project( main )
find_package( OpenCV REQUIRED )
add_executable( main main.cpp )
target_link_libraries( main ${OpenCV_LIBS} )

running:

> cmake .;make;./main
 hi 
0  <-- predict

To compare, here is a python's sklearn code (rfc.py):

from sklearn.ensemble import RandomForestClassifier
X = [[0, 0], [1, 1]]
Y = [0, 1]
clf = RandomForestClassifier(n_estimators=10)
clf = clf.fit(X, Y)
print clf.predict([[2., 2.]])

running:

> python rfc.py 
[1]

Update (1)

I tried different combination of placement of data and also changed the line

Mat sample_data(2, 1, CV_32FC1, sampleData);

to

Mat sample_data(2, 1, CV_32FC1, sampleData);

I still get just 0.