eccv2012-15scenes-gphikclassifier.cpp 6.4 KB

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