|
@@ -0,0 +1,198 @@
|
|
|
+/**
|
|
|
+* @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/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
|
|
|
+ )
|
|
|
+{
|
|
|
+ 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;
|
|
|
+ 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 );
|
|
|
+
|
|
|
+ NICE::GPHIKClassifier gphik ( &conf, "GPHIKClassifier" );
|
|
|
+
|
|
|
+ NICE::Timer t;
|
|
|
+
|
|
|
+ bool b_verbose = conf.gB ( "main", "b_verbose", false );
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ // ========================================================================
|
|
|
+ // TRAINING STEP
|
|
|
+ // ========================================================================
|
|
|
+
|
|
|
+
|
|
|
+ std::string ext = conf.gS("main", "ext", ".txt");
|
|
|
+// 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
|
|
|
+ );
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ // train GPHIK classifier
|
|
|
+ std::cerr << " start training" << std::endl;
|
|
|
+ t.start();
|
|
|
+
|
|
|
+ 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
|
|
|
+ );
|
|
|
+
|
|
|
+
|
|
|
+ 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;
|
|
|
+ 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;
|
|
|
+}
|