2010-11-26 18:59:40 +01:00
|
|
|
#include "opencv2/ml/ml.hpp"
|
2011-04-05 17:13:10 +02:00
|
|
|
#include "opencv2/core/core_c.h"
|
2010-05-11 19:44:00 +02:00
|
|
|
#include <stdio.h>
|
2011-04-05 17:13:10 +02:00
|
|
|
#include <map>
|
2010-12-03 03:49:09 +01:00
|
|
|
|
2012-06-15 15:04:17 +02:00
|
|
|
static void help()
|
2010-12-03 03:49:09 +01:00
|
|
|
{
|
2012-06-15 15:04:17 +02:00
|
|
|
printf(
|
|
|
|
"\nThis sample demonstrates how to use different decision trees and forests including boosting and random trees:\n"
|
|
|
|
"CvDTree dtree;\n"
|
|
|
|
"CvBoost boost;\n"
|
|
|
|
"CvRTrees rtrees;\n"
|
|
|
|
"CvERTrees ertrees;\n"
|
|
|
|
"CvGBTrees gbtrees;\n"
|
|
|
|
"Call:\n\t./tree_engine [-r <response_column>] [-c] <csv filename>\n"
|
2011-06-09 14:01:47 +02:00
|
|
|
"where -r <response_column> specified the 0-based index of the response (0 by default)\n"
|
|
|
|
"-c specifies that the response is categorical (it's ordered by default) and\n"
|
|
|
|
"<csv filename> is the name of training data file in comma-separated value format\n\n");
|
2010-12-03 03:49:09 +01:00
|
|
|
}
|
2011-04-05 17:13:10 +02:00
|
|
|
|
|
|
|
|
2012-06-15 15:04:17 +02:00
|
|
|
static int count_classes(CvMLData& data)
|
2011-04-05 17:13:10 +02:00
|
|
|
{
|
|
|
|
cv::Mat r(data.get_responses());
|
|
|
|
std::map<int, int> rmap;
|
|
|
|
int i, n = (int)r.total();
|
|
|
|
for( i = 0; i < n; i++ )
|
|
|
|
{
|
|
|
|
float val = r.at<float>(i);
|
|
|
|
int ival = cvRound(val);
|
|
|
|
if( ival != val )
|
|
|
|
return -1;
|
2012-06-15 15:04:17 +02:00
|
|
|
rmap[ival] = 1;
|
2011-04-05 17:13:10 +02:00
|
|
|
}
|
2012-03-16 22:21:04 +01:00
|
|
|
return (int)rmap.size();
|
2011-04-05 17:13:10 +02:00
|
|
|
}
|
|
|
|
|
2012-06-15 15:04:17 +02:00
|
|
|
static void print_result(float train_err, float test_err, const CvMat* _var_imp)
|
2010-05-11 19:44:00 +02:00
|
|
|
{
|
|
|
|
printf( "train error %f\n", train_err );
|
|
|
|
printf( "test error %f\n\n", test_err );
|
2012-06-15 15:04:17 +02:00
|
|
|
|
2011-04-05 17:13:10 +02:00
|
|
|
if (_var_imp)
|
2010-05-11 19:44:00 +02:00
|
|
|
{
|
2011-04-05 17:13:10 +02:00
|
|
|
cv::Mat var_imp(_var_imp), sorted_idx;
|
|
|
|
cv::sortIdx(var_imp, sorted_idx, CV_SORT_EVERY_ROW + CV_SORT_DESCENDING);
|
2012-06-15 15:04:17 +02:00
|
|
|
|
2011-04-05 17:13:10 +02:00
|
|
|
printf( "variable importance:\n" );
|
|
|
|
int i, n = (int)var_imp.total();
|
|
|
|
int type = var_imp.type();
|
|
|
|
CV_Assert(type == CV_32F || type == CV_64F);
|
2012-06-15 15:04:17 +02:00
|
|
|
|
2011-04-05 17:13:10 +02:00
|
|
|
for( i = 0; i < n; i++)
|
2010-05-11 19:44:00 +02:00
|
|
|
{
|
2011-04-05 17:13:10 +02:00
|
|
|
int k = sorted_idx.at<int>(i);
|
|
|
|
printf( "%d\t%f\n", k, type == CV_32F ? var_imp.at<float>(k) : var_imp.at<double>(k));
|
2010-05-11 19:44:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
2011-06-09 14:01:47 +02:00
|
|
|
int main(int argc, char** argv)
|
2010-05-11 19:44:00 +02:00
|
|
|
{
|
2011-06-09 14:01:47 +02:00
|
|
|
if(argc < 2)
|
2011-04-05 17:13:10 +02:00
|
|
|
{
|
|
|
|
help();
|
2011-06-09 14:01:47 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
const char* filename = 0;
|
|
|
|
int response_idx = 0;
|
|
|
|
bool categorical_response = false;
|
2012-06-15 15:04:17 +02:00
|
|
|
|
2011-06-09 14:01:47 +02:00
|
|
|
for(int i = 1; i < argc; i++)
|
|
|
|
{
|
|
|
|
if(strcmp(argv[i], "-r") == 0)
|
|
|
|
sscanf(argv[++i], "%d", &response_idx);
|
|
|
|
else if(strcmp(argv[i], "-c") == 0)
|
|
|
|
categorical_response = true;
|
|
|
|
else if(argv[i][0] != '-' )
|
|
|
|
filename = argv[i];
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf("Error. Invalid option %s\n", argv[i]);
|
|
|
|
help();
|
|
|
|
return -1;
|
|
|
|
}
|
2011-04-05 17:13:10 +02:00
|
|
|
}
|
2012-06-15 15:04:17 +02:00
|
|
|
|
2011-06-09 14:01:47 +02:00
|
|
|
printf("\nReading in %s...\n\n",filename);
|
2010-05-11 19:44:00 +02:00
|
|
|
CvDTree dtree;
|
|
|
|
CvBoost boost;
|
|
|
|
CvRTrees rtrees;
|
|
|
|
CvERTrees ertrees;
|
2012-06-15 15:04:17 +02:00
|
|
|
CvGBTrees gbtrees;
|
2010-05-11 19:44:00 +02:00
|
|
|
|
|
|
|
CvMLData data;
|
|
|
|
|
2012-06-15 15:04:17 +02:00
|
|
|
|
2011-04-05 17:13:10 +02:00
|
|
|
CvTrainTestSplit spl( 0.5f );
|
2012-06-15 15:04:17 +02:00
|
|
|
|
2011-06-09 14:01:47 +02:00
|
|
|
if ( data.read_csv( filename ) == 0)
|
2010-05-11 19:44:00 +02:00
|
|
|
{
|
2011-04-05 17:13:10 +02:00
|
|
|
data.set_response_idx( response_idx );
|
|
|
|
if(categorical_response)
|
|
|
|
data.change_var_type( response_idx, CV_VAR_CATEGORICAL );
|
2010-05-11 19:44:00 +02:00
|
|
|
data.set_train_test_split( &spl );
|
2012-06-15 15:04:17 +02:00
|
|
|
|
2010-05-11 19:44:00 +02:00
|
|
|
printf("======DTREE=====\n");
|
|
|
|
dtree.train( &data, CvDTreeParams( 10, 2, 0, false, 16, 0, false, false, 0 ));
|
|
|
|
print_result( dtree.calc_error( &data, CV_TRAIN_ERROR), dtree.calc_error( &data, CV_TEST_ERROR ), dtree.get_var_importance() );
|
|
|
|
|
2011-04-05 17:13:10 +02:00
|
|
|
if( categorical_response && count_classes(data) == 2 )
|
|
|
|
{
|
2010-05-11 19:44:00 +02:00
|
|
|
printf("======BOOST=====\n");
|
|
|
|
boost.train( &data, CvBoostParams(CvBoost::DISCRETE, 100, 0.95, 2, false, 0));
|
2011-02-28 03:24:11 +01:00
|
|
|
print_result( boost.calc_error( &data, CV_TRAIN_ERROR ), boost.calc_error( &data, CV_TEST_ERROR ), 0 ); //doesn't compute importance
|
2011-04-05 17:13:10 +02:00
|
|
|
}
|
2010-05-11 19:44:00 +02:00
|
|
|
|
|
|
|
printf("======RTREES=====\n");
|
|
|
|
rtrees.train( &data, CvRTParams( 10, 2, 0, false, 16, 0, true, 0, 100, 0, CV_TERMCRIT_ITER ));
|
|
|
|
print_result( rtrees.calc_error( &data, CV_TRAIN_ERROR), rtrees.calc_error( &data, CV_TEST_ERROR ), rtrees.get_var_importance() );
|
|
|
|
|
|
|
|
printf("======ERTREES=====\n");
|
2012-04-12 16:05:55 +02:00
|
|
|
ertrees.train( &data, CvRTParams( 18, 2, 0, false, 16, 0, true, 0, 100, 0, CV_TERMCRIT_ITER ));
|
2010-05-11 19:44:00 +02:00
|
|
|
print_result( ertrees.calc_error( &data, CV_TRAIN_ERROR), ertrees.calc_error( &data, CV_TEST_ERROR ), ertrees.get_var_importance() );
|
2010-10-17 21:18:42 +02:00
|
|
|
|
2011-04-30 20:04:33 +02:00
|
|
|
printf("======GBTREES=====\n");
|
2012-06-15 15:04:17 +02:00
|
|
|
if (categorical_response)
|
|
|
|
gbtrees.train( &data, CvGBTreesParams(CvGBTrees::DEVIANCE_LOSS, 100, 0.1f, 0.8f, 5, false));
|
|
|
|
else
|
|
|
|
gbtrees.train( &data, CvGBTreesParams(CvGBTrees::SQUARED_LOSS, 100, 0.1f, 0.8f, 5, false));
|
2011-04-30 20:04:33 +02:00
|
|
|
print_result( gbtrees.calc_error( &data, CV_TRAIN_ERROR), gbtrees.calc_error( &data, CV_TEST_ERROR ), 0 ); //doesn't compute importance
|
2010-05-11 19:44:00 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
printf("File can not be read");
|
|
|
|
|
|
|
|
return 0;
|
2010-07-08 13:24:32 +02:00
|
|
|
}
|