123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- /**
- * @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 <vector>
- // NICE-core includes
- #include <core/basics/vectorio.h>
- #include <core/basics/Timer.h>
- #include <core/basics/Config.h>
- // NICE-vislearning includes
- #include <vislearning/baselib/Globals.h>
- //
- #include <vislearning/cbaselib/MultiDataset.h>
- // gp-hik-core includes
- #include <gp-hik-core/GPHIKClassifier.h>
- #include <gp-hik-core/GPHIKRawClassifier.h>
- #include <gp-hik-core/tools.h>
- 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;
- }
|