eccv2012-15scenes-fasthik.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /**
  2. * @file eccv2011-15scenes-fasthik.cpp
  3. * @brief ECCV 2012 Experiment with 15 Scenes
  4. * @author Erik Rodner
  5. * @date 01/17/2012
  6. */
  7. #include <vector>
  8. #include <core/basics/vectorio.h>
  9. #include <core/basics/Timer.h>
  10. #include <core/basics/Config.h>
  11. #include <vislearning/classifier/fpclassifier/gphik/FPCGPHIK.h>
  12. #include <vislearning/cbaselib/MultiDataset.h>
  13. #include <vislearning/baselib/Globals.h>
  14. #include <gp-hik-core/FastMinKernel.h>
  15. #include <gp-hik-core/FMKGPHyperparameterOptimization.h>
  16. #include <gp-hik-core/parameterizedFunctions/PFAbsExp.h>
  17. #include <gp-hik-core/parameterizedFunctions/PFWeightedDim.h>
  18. #include <gp-hik-core/parameterizedFunctions/PFExp.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. // this function is very much adapted to Wus files
  25. void readSparseExamples ( const string & fn, Examples & examples )
  26. {
  27. cerr << "Reading " << fn << endl;
  28. ifstream ifs ( fn.c_str(), ios::in );
  29. if ( ! ifs.good() ) {
  30. fthrow(Exception, "Unable to read " << fn );
  31. }
  32. while ( !ifs.eof() )
  33. {
  34. int classno;
  35. if ( !(ifs >> classno) ) break;
  36. classno--;
  37. SparseVector *v = new SparseVector;
  38. v->restore ( ifs, SparseVector::FORMAT_INDEX_LINE );
  39. v->setDim( 10000 );
  40. // evil HACK FIXME
  41. v->multiply(1.0 / (128.0*1000.0));
  42. Example example;
  43. example.svec = v;
  44. cerr << "read example of size " << v->size() << " belonging to class " << classno << endl;
  45. cerr << "min: " << v->min() << " max:" << v->max() << endl;
  46. examples.push_back ( pair<int, Example> ( classno, example ) );
  47. }
  48. ifs.close();
  49. }
  50. /**
  51. ECCV 2012 Experiment with 15 Scenes
  52. */
  53. int main (int argc, char **argv)
  54. {
  55. std::set_terminate(__gnu_cxx::__verbose_terminate_handler);
  56. Config conf ( argc, argv );
  57. FPCGPHIK classifier ( &conf, "GPHIKClassifier" );
  58. string trainFilename = conf.gS("main", "train");
  59. Examples trainExamples;
  60. readSparseExamples ( trainFilename, trainExamples );
  61. FeaturePool fp; // ignored anyway
  62. classifier.train ( fp, trainExamples );
  63. cerr << "Reading test data file" << endl;
  64. string testFilename = conf.gS("main", "test");
  65. Examples testExamples;
  66. readSparseExamples ( testFilename, testExamples );
  67. Timer t;
  68. Matrix confusion ( 15, 15, 0.0 );
  69. int index = 1;
  70. for ( Examples::iterator example_i = testExamples.begin(); example_i != testExamples.end(); example_i++,index++ )
  71. {
  72. uint classno_groundtruth = example_i->first;
  73. t.start();
  74. ClassificationResult r = classifier.classify ( example_i->second );
  75. uint classno_estimated = r.classno;
  76. t.stop();
  77. r.scores.store(cerr);
  78. cerr << "[" << index << " / " << testExamples.size() << "] " << classno_estimated << " " << classno_groundtruth << " time: " << t.getLast() << endl;
  79. //confusion( classno_groundtruth, classno_estimated ) += 1;
  80. confusion( classno_estimated, classno_groundtruth ) += 1;
  81. }
  82. confusion.normalizeColumnsL1();
  83. cerr << confusion << endl;
  84. cerr << "average recognition rate: " << confusion.trace()/confusion.rows() << endl;
  85. return 0;
  86. }