eccv2012-15scenes-gphikclassifier.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /**
  2. * @file eccv2012-15scenes-gphikclassifier.cpp
  3. * @brief ECCV 2012 Experiment with 15 Scenes and usage of the wrapper class
  4. * @author Alexander Freytag
  5. * @date 09/10/2015
  6. */
  7. // STL includes
  8. #include <vector>
  9. // NICE-core includes
  10. #include <core/basics/vectorio.h>
  11. #include <core/basics/Timer.h>
  12. #include <core/basics/Config.h>
  13. // NICE-vislearning includes
  14. #include <vislearning/baselib/Globals.h>
  15. //
  16. #include <vislearning/cbaselib/MultiDataset.h>
  17. // gp-hik-core includes
  18. #include <gp-hik-core/GPHIKClassifier.h>
  19. #include <gp-hik-core/tools.h>
  20. using namespace std;
  21. using namespace NICE;
  22. using namespace OBJREC;
  23. #include "datatools.h"
  24. void readNonSparseAsSparseExamples ( const NICE::Config & conf,
  25. const OBJREC::LabeledSet & ls,
  26. std::vector < const NICE::SparseVector * > & _examples,
  27. NICE::Vector & _labels,
  28. std::string extension = ".txt",
  29. const bool _verbose = false
  30. )
  31. {
  32. std::string cacheroot = conf.gS("cache", "root");
  33. _labels.resize ( ls.count() );
  34. _examples.clear();
  35. LOOP_ALL_S ( ls )
  36. {
  37. EACH_S(classno, imgfn);
  38. Globals::setCurrentImgFN ( imgfn );
  39. std::string cachefn = Globals::getCacheFilename ( cacheroot, Globals::SORT_CATEGORIES ) + extension;
  40. if ( _verbose )
  41. {
  42. std::cerr << "fn: " << imgfn << " cachefn: " << cachefn << std::endl;
  43. }
  44. _labels[ _examples.size() ] = classno;
  45. NICE::Vector x;
  46. std::ifstream ifs ( cachefn.c_str(), std::ios::in );
  47. if ( ! ifs.good() )
  48. fthrow(Exception, "File not found: " << cachefn );
  49. ifs >> x;
  50. NICE::SparseVector * xSparse = new NICE::SparseVector ( x );
  51. _examples.push_back ( xSparse );
  52. ifs.close();
  53. }
  54. }
  55. /**
  56. ECCV 2012 Experiment with 15 Scenes
  57. NOTE: usage of this test-function is not recommended. Use eccv2012-15scenes-fasthik instaed with a proper interface
  58. */
  59. int main (int argc, char **argv)
  60. {
  61. std::set_terminate(__gnu_cxx::__verbose_terminate_handler);
  62. NICE::Config conf ( argc, argv );
  63. NICE::GPHIKClassifier gphik ( &conf, "GPHIKClassifier" );
  64. NICE::Timer t;
  65. bool b_verbose = conf.gB ( "main", "b_verbose", false );
  66. // ========================================================================
  67. // TRAINING STEP
  68. // ========================================================================
  69. std::string ext = conf.gS("main", "ext", ".txt");
  70. // cerr << "Using cache extension: " << ext << endl;
  71. MultiDataset md ( &conf );
  72. // const ClassNames & classNamesTrain = md.getClassNames("train");
  73. // define variables for training set
  74. std::vector< const NICE::SparseVector * > examplesTrain;
  75. NICE::Vector labelsTrain;
  76. const LabeledSet * ls_train = md["train"];
  77. // read training set
  78. readNonSparseAsSparseExamples ( conf,
  79. *ls_train,
  80. examplesTrain,
  81. labelsTrain,
  82. ext,
  83. b_verbose
  84. );
  85. // train GPHIK classifier
  86. std::cerr << " start training" << std::endl;
  87. t.start();
  88. gphik.train ( examplesTrain, labelsTrain );
  89. t.stop();
  90. std::cerr << " time for training: " << t.getLast() << std::endl;
  91. // ------------------ TESTING
  92. // q'n'd memory extensive solution
  93. const LabeledSet * ls_test = md["test"];
  94. std::vector< const NICE::SparseVector * > examplesTest;
  95. NICE::Vector labelsTest;
  96. readNonSparseAsSparseExamples ( conf,
  97. *ls_test,
  98. examplesTest,
  99. labelsTest,
  100. ext,
  101. b_verbose
  102. );
  103. NICE::Matrix confusion ( labelsTest.Max()+1, labelsTest.Max() + 1, 0.0 );
  104. for ( uint i = 0 ; i < examplesTest.size(); i++ )
  105. {
  106. // const NICE::Vector & xstar = examplesTest[i];
  107. // // the following is just to be sure that we
  108. // // do not count the time necessary for conversion
  109. // NICE::SparseVector xstar_sparse ( xstar );
  110. //
  111. //
  112. uint classno_groundtruth = labelsTest[i];
  113. SparseVector scores;
  114. t.start();
  115. uint classno_estimated;
  116. gphik.classify ( examplesTest[i], classno_estimated, scores );
  117. t.stop();
  118. scores.store(cerr);
  119. cerr << "[" << i << " / " << examplesTest.size() << "] " << classno_estimated << " " << classno_groundtruth << " time: " << t.getLast() << endl;
  120. //confusion( classno_groundtruth, classno_estimated ) += 1;
  121. confusion( classno_estimated, classno_groundtruth ) += 1;
  122. }
  123. confusion.normalizeColumnsL1();
  124. cerr << confusion << endl;
  125. cerr << "average recognition rate: " << confusion.trace()/confusion.rows() << endl;
  126. // ========================================================================
  127. // clean up memory
  128. // ========================================================================
  129. // release memore of feature vectors from training set
  130. for (std::vector< const NICE::SparseVector *>::const_iterator itTrainExamples = examplesTrain.begin(); itTrainExamples != examplesTrain.end(); itTrainExamples++ )
  131. {
  132. delete *itTrainExamples;
  133. }
  134. // release memore of feature vectors from test set
  135. for (std::vector< const NICE::SparseVector *>::const_iterator itTestExamples = examplesTest.begin(); itTestExamples != examplesTest.end(); itTestExamples++ )
  136. {
  137. delete *itTestExamples;
  138. }
  139. return 0;
  140. }