/** * @file eccv2012-15scenes-gphikclassifier.cpp * @brief ECCV 2012 Experiment with 15 Scenes and usage of the wrapper class * @author Alexander Freytag * @date 09/10/2015 */ // STL includes #include // NICE-core includes #include #include #include // NICE-vislearning includes #include // #include // gp-hik-core includes #include #include #include using namespace std; using namespace NICE; using namespace OBJREC; #include "datatools.h" void readNonSparseAsSparseExamples ( const NICE::Config & conf, const OBJREC::LabeledSet & ls, std::vector < const NICE::SparseVector * > & _examples, NICE::Vector & _labels, std::string extension = ".txt", const bool _verbose = false, int maxdim = -1 ) { std::string cacheroot = conf.gS("cache", "root"); _labels.resize ( ls.count() ); _examples.clear(); LOOP_ALL_S ( ls ) { EACH_S(classno, imgfn); Globals::setCurrentImgFN ( imgfn ); std::string cachefn = Globals::getCacheFilename ( cacheroot, Globals::SORT_CATEGORIES ) + extension; if ( _verbose ) { std::cerr << "fn: " << imgfn << " cachefn: " << cachefn << std::endl; } _labels[ _examples.size() ] = classno; NICE::Vector x; std::ifstream ifs ( cachefn.c_str(), std::ios::in ); if ( ! ifs.good() ) fthrow(Exception, "File not found: " << cachefn ); ifs >> x; // reduce feature dimension if necessary if (maxdim > 0 && x.size() > maxdim) { Vector xsub (maxdim); for (int i = 0; i < maxdim; i++) xsub[i] = x[i]; x = xsub; } NICE::SparseVector * xSparse = new NICE::SparseVector ( x ); _examples.push_back ( xSparse ); ifs.close(); } } /** ECCV 2012 Experiment with 15 Scenes NOTE: usage of this test-function is not recommended. Use eccv2012-15scenes-fasthik instaed with a proper interface */ int main (int argc, char **argv) { std::set_terminate(__gnu_cxx::__verbose_terminate_handler); NICE::Config conf ( argc, argv ); bool use_raw = conf.gB("main", "raw", false); NICE::GPHIKClassifier gphik ( &conf, "GPHIKClassifier" ); NICE::GPHIKRawClassifier gphik_raw ( &conf, "GPHIKRawClassifier" ); NICE::Timer t; bool b_verbose = conf.gB ( "main", "b_verbose", false ); // ======================================================================== // TRAINING STEP // ======================================================================== std::string ext = conf.gS("main", "ext", ".txt"); int maxdim = conf.gI("main", "maxdim", -1); // cerr << "Using cache extension: " << ext << endl; MultiDataset md ( &conf ); // const ClassNames & classNamesTrain = md.getClassNames("train"); // define variables for training set std::vector< const NICE::SparseVector * > examplesTrain; NICE::Vector labelsTrain; const LabeledSet * ls_train = md["train"]; // read training set readNonSparseAsSparseExamples ( conf, *ls_train, examplesTrain, labelsTrain, ext, b_verbose, maxdim ); // train GPHIK classifier std::cerr << " start training" << std::endl; t.start(); if (use_raw) gphik_raw.train ( examplesTrain, labelsTrain ); else gphik.train ( examplesTrain, labelsTrain ); t.stop(); std::cerr << " time for training: " << t.getLast() << std::endl; // ------------------ TESTING // q'n'd memory extensive solution const LabeledSet * ls_test = md["test"]; std::vector< const NICE::SparseVector * > examplesTest; NICE::Vector labelsTest; readNonSparseAsSparseExamples ( conf, *ls_test, examplesTest, labelsTest, ext, b_verbose, maxdim ); NICE::Matrix confusion ( labelsTest.Max()+1, labelsTest.Max() + 1, 0.0 ); for ( uint i = 0 ; i < examplesTest.size(); i++ ) { // const NICE::Vector & xstar = examplesTest[i]; // // the following is just to be sure that we // // do not count the time necessary for conversion // NICE::SparseVector xstar_sparse ( xstar ); // // uint classno_groundtruth = labelsTest[i]; SparseVector scores; t.start(); uint classno_estimated; if (use_raw) gphik_raw.classify ( examplesTest[i], classno_estimated, scores ); else gphik.classify ( examplesTest[i], classno_estimated, scores ); t.stop(); scores.store(cerr); cerr << "[" << i << " / " << examplesTest.size() << "] " << classno_estimated << " " << classno_groundtruth << " time: " << t.getLast() << endl; //confusion( classno_groundtruth, classno_estimated ) += 1; confusion( classno_estimated, classno_groundtruth ) += 1; } confusion.normalizeColumnsL1(); cerr << confusion << endl; cerr << "average recognition rate: " << confusion.trace()/confusion.rows() << endl; // ======================================================================== // clean up memory // ======================================================================== // release memore of feature vectors from training set for (std::vector< const NICE::SparseVector *>::const_iterator itTrainExamples = examplesTrain.begin(); itTrainExamples != examplesTrain.end(); itTrainExamples++ ) { delete *itTrainExamples; } // release memore of feature vectors from test set for (std::vector< const NICE::SparseVector *>::const_iterator itTestExamples = examplesTest.begin(); itTestExamples != examplesTest.end(); itTestExamples++ ) { delete *itTestExamples; } return 0; }