2010-11-27 01:59:40 +08:00
|
|
|
#include "opencv2/ml/ml.hpp"
|
2014-08-03 05:41:09 +08:00
|
|
|
#include "opencv2/core/core.hpp"
|
2013-03-29 01:01:12 +08:00
|
|
|
#include "opencv2/core/utility.hpp"
|
2010-05-12 01:44:00 +08:00
|
|
|
#include <stdio.h>
|
2014-08-03 05:41:09 +08:00
|
|
|
#include <string>
|
2011-04-05 23:13:10 +08:00
|
|
|
#include <map>
|
2010-12-03 10:49:09 +08:00
|
|
|
|
2014-08-03 05:41:09 +08:00
|
|
|
using namespace cv;
|
|
|
|
using namespace cv::ml;
|
|
|
|
|
2012-06-08 01:21:29 +08:00
|
|
|
static void help()
|
2010-12-03 10:49:09 +08:00
|
|
|
{
|
2012-06-08 01:21:29 +08:00
|
|
|
printf(
|
2014-08-03 05:41:09 +08:00
|
|
|
"\nThis sample demonstrates how to use different decision trees and forests including boosting and random trees.\n"
|
|
|
|
"Usage:\n\t./tree_engine [-r <response_column>] [-ts type_spec] <csv filename>\n"
|
2011-06-09 20:01:47 +08:00
|
|
|
"where -r <response_column> specified the 0-based index of the response (0 by default)\n"
|
2014-08-03 05:41:09 +08:00
|
|
|
"-ts specifies the var type spec in the form ord[n1,n2-n3,n4-n5,...]cat[m1-m2,m3,m4-m5,...]\n"
|
2011-06-09 20:01:47 +08:00
|
|
|
"<csv filename> is the name of training data file in comma-separated value format\n\n");
|
2010-12-03 10:49:09 +08:00
|
|
|
}
|
2011-04-05 23:13:10 +08:00
|
|
|
|
2014-08-03 05:41:09 +08:00
|
|
|
static void train_and_print_errs(Ptr<StatModel> model, const Ptr<TrainData>& data)
|
2011-04-05 23:13:10 +08:00
|
|
|
{
|
2014-08-03 05:41:09 +08:00
|
|
|
bool ok = model->train(data);
|
|
|
|
if( !ok )
|
2011-04-05 23:13:10 +08:00
|
|
|
{
|
2014-08-03 05:41:09 +08:00
|
|
|
printf("Training failed\n");
|
2011-04-05 23:13:10 +08:00
|
|
|
}
|
2014-08-03 05:41:09 +08:00
|
|
|
else
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-08-03 05:41:09 +08:00
|
|
|
printf( "train error: %f\n", model->calcError(data, false, noArray()) );
|
|
|
|
printf( "test error: %f\n\n", model->calcError(data, true, noArray()) );
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-09 20:01:47 +08:00
|
|
|
int main(int argc, char** argv)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2011-06-09 20:01:47 +08:00
|
|
|
if(argc < 2)
|
2011-04-05 23:13:10 +08:00
|
|
|
{
|
|
|
|
help();
|
2011-06-09 20:01:47 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
const char* filename = 0;
|
|
|
|
int response_idx = 0;
|
2014-08-03 05:41:09 +08:00
|
|
|
std::string typespec;
|
2012-06-08 01:21:29 +08:00
|
|
|
|
2011-06-09 20:01:47 +08:00
|
|
|
for(int i = 1; i < argc; i++)
|
|
|
|
{
|
|
|
|
if(strcmp(argv[i], "-r") == 0)
|
|
|
|
sscanf(argv[++i], "%d", &response_idx);
|
2014-08-03 05:41:09 +08:00
|
|
|
else if(strcmp(argv[i], "-ts") == 0)
|
|
|
|
typespec = argv[++i];
|
2011-06-09 20:01:47 +08:00
|
|
|
else if(argv[i][0] != '-' )
|
|
|
|
filename = argv[i];
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf("Error. Invalid option %s\n", argv[i]);
|
|
|
|
help();
|
|
|
|
return -1;
|
|
|
|
}
|
2011-04-05 23:13:10 +08:00
|
|
|
}
|
2012-06-08 01:21:29 +08:00
|
|
|
|
2011-06-09 20:01:47 +08:00
|
|
|
printf("\nReading in %s...\n\n",filename);
|
2014-08-03 05:41:09 +08:00
|
|
|
const double train_test_split_ratio = 0.5;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-08-03 05:41:09 +08:00
|
|
|
Ptr<TrainData> data = TrainData::loadFromCSV(filename, 0, response_idx, response_idx+1, typespec);
|
2012-06-08 01:21:29 +08:00
|
|
|
|
2014-08-03 05:41:09 +08:00
|
|
|
if( data.empty() )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-08-03 05:41:09 +08:00
|
|
|
printf("ERROR: File %s can not be read\n", filename);
|
|
|
|
return 0;
|
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-08-03 05:41:09 +08:00
|
|
|
data->setTrainTestSplitRatio(train_test_split_ratio);
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-08-03 05:41:09 +08:00
|
|
|
printf("======DTREE=====\n");
|
2015-02-11 18:24:14 +08:00
|
|
|
Ptr<DTrees> dtree = DTrees::create();
|
|
|
|
dtree->setMaxDepth(10);
|
|
|
|
dtree->setMinSampleCount(2);
|
|
|
|
dtree->setRegressionAccuracy(0);
|
|
|
|
dtree->setUseSurrogates(false);
|
|
|
|
dtree->setMaxCategories(16);
|
|
|
|
dtree->setCVFolds(0);
|
|
|
|
dtree->setUse1SERule(false);
|
|
|
|
dtree->setTruncatePrunedTree(false);
|
|
|
|
dtree->setPriors(Mat());
|
2014-08-03 05:41:09 +08:00
|
|
|
train_and_print_errs(dtree, data);
|
2010-10-18 03:18:42 +08:00
|
|
|
|
2014-08-03 05:41:09 +08:00
|
|
|
if( (int)data->getClassLabels().total() <= 2 ) // regression or 2-class classification problem
|
|
|
|
{
|
|
|
|
printf("======BOOST=====\n");
|
2015-02-11 18:24:14 +08:00
|
|
|
Ptr<Boost> boost = Boost::create();
|
|
|
|
boost->setBoostType(Boost::GENTLE);
|
|
|
|
boost->setWeakCount(100);
|
|
|
|
boost->setWeightTrimRate(0.95);
|
|
|
|
boost->setMaxDepth(2);
|
|
|
|
boost->setUseSurrogates(false);
|
|
|
|
boost->setPriors(Mat());
|
2014-08-03 05:41:09 +08:00
|
|
|
train_and_print_errs(boost, data);
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
2014-08-03 05:41:09 +08:00
|
|
|
|
|
|
|
printf("======RTREES=====\n");
|
2015-02-11 18:24:14 +08:00
|
|
|
Ptr<RTrees> rtrees = RTrees::create();
|
|
|
|
rtrees->setMaxDepth(10);
|
|
|
|
rtrees->setMinSampleCount(2);
|
|
|
|
rtrees->setRegressionAccuracy(0);
|
|
|
|
rtrees->setUseSurrogates(false);
|
|
|
|
rtrees->setMaxCategories(16);
|
|
|
|
rtrees->setPriors(Mat());
|
|
|
|
rtrees->setCalculateVarImportance(false);
|
|
|
|
rtrees->setActiveVarCount(0);
|
|
|
|
rtrees->setTermCriteria(TermCriteria(TermCriteria::MAX_ITER, 100, 0));
|
2014-08-03 05:41:09 +08:00
|
|
|
train_and_print_errs(rtrees, data);
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
return 0;
|
2010-07-08 19:24:32 +08:00
|
|
|
}
|