123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- /**
- * @file testFPClassifier.cpp
- * @brief main program for classifier evaluation
- * @author Erik Rodner
- * @date 2007-10-12
- */
- #include <fstream>
- #include <iostream>
- #include <core/basics/numerictools.h>
- #include <core/basics/Config.h>
- #include <core/basics/StringTools.h>
- //----------
- #include <vislearning/baselib/Preprocess.h>
- #include <vislearning/cbaselib/MultiDataset.h>
- #include <vislearning/cbaselib/ClassificationResults.h>
- #include <vislearning/cbaselib/MutualInformation.h>
- #include <vislearning/classifier/classifierbase/FeaturePoolClassifier.h>
- #include <vislearning/classifier/fpclassifier/randomforest/FPCRandomForestTransfer.h>
- #include <vislearning/classifier/classifierinterfaces/VCFeaturePool.h>
- #include <vislearning/math/cluster/GMM.h>
- //----------
- #include "gp-hik-exp/GPHIKClassifierNICE.h"
- #undef DEBUG
- using namespace OBJREC;
- using namespace NICE;
- using namespace std;
- int main ( int argc, char **argv )
- {
- fprintf ( stderr, "testClassifier: init\n" );
- std::set_terminate ( __gnu_cxx::__verbose_terminate_handler );
- Config conf ( argc, argv );
- GPHIKClassifierNICE *classifier = new GPHIKClassifierNICE ( &conf, "ClassiferGPHIK" );
- string trainfn = conf.gS ( "data", "trainfile" );
- string testfn = conf.gS ( "data", "testfile" );
- Examples trainex;
- ifstream intrain ( trainfn.c_str() );
- int parts = 0;
- while ( intrain.good() )
- {
- string line;
- getline ( intrain, line );
- vector<string> split;
- StringTools::split ( line, ' ', split );
- if ( split.size() == 0 )
- break;
- if ( parts > 0 )
- assert ( parts == ( int ) split.size() );
- parts = split.size();
- int classno = atoi ( split[0].c_str() );
- SparseVector *sv = new SparseVector();
- for ( uint i = 1; i < split.size();i++ )
- {
- vector<string> split2;
- StringTools::split ( split[i], ':', split2 );
- assert ( split2.size() == 2 );
- ( *sv ) [atoi ( split2[0].c_str() ) ] = atof ( split2[1].c_str() );
- }
- Example example;
- example.vec = NULL;
- example.svec = sv;
- trainex.push_back ( pair<int, Example> ( classno, example ) );
- }
- cout << "trainex.size(): " << trainex.size() << endl;
- Examples testex;
- ifstream intest ( testfn.c_str() );
- parts = 0;
- while ( intest.good() )
- {
- string line;
- getline ( intest, line );
- vector<string> split;
- StringTools::split ( line, ' ', split );
- if ( split.size() == 0 )
- break;
- if ( parts > 0 )
- assert ( parts == ( int ) split.size() );
- parts = split.size();
- int classno = atoi ( split[0].c_str() );
- SparseVector *sv = new SparseVector();
- for ( uint i = 1; i < split.size();i++ )
- {
- vector<string> split2;
- StringTools::split ( split[i], ':', split2 );
- assert ( split2.size() == 2 );
-
- double val = atof (split2[1].c_str());
-
- if(val != 0.0)
- ( *sv ) [atoi ( split2[0].c_str() ) ] = val;
- }
- Example example;
- example.vec = NULL;
- example.svec = sv;
- testex.push_back ( pair<int, Example> ( classno, example ) );
- }
- cout << "testex.size(): " << testex.size() << endl;
- FeaturePool fp;
- classifier->train ( fp, trainex );
- int counter = 0;
-
- for ( uint e = 0; e < testex.size(); e++ )
- {
- ClassificationResult r = classifier->classify ( testex[e].second );
-
- int bestclass = 0;
- double bestval = r.scores[0];
-
- for ( int j = 1 ; j < r.scores.size(); j++ )
- {
- if(r.scores[j] > bestval)
- {
- bestclass = j;
- bestval = r.scores[j];
- }
- }
-
- if(bestclass == testex[e].first)
- counter++;
- }
- cout << "avg: " << (double)counter/(double)testex.size() << endl;
- return 0;
- }
|