eccv2012-15scenes.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /**
  2. * @file eccv2012-15scenes.cpp
  3. * @brief ECCV 2012 Experiment with 15 Scenes
  4. * @author Erik Rodner, Alexander Freytag
  5. * @date 01/17/2012
  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/FastMinKernel.h>
  19. #include <gp-hik-core/FMKGPHyperparameterOptimization.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. /**
  26. ECCV 2012 Experiment with 15 Scenes
  27. NOTE: usage of this test-function is not recommended. Use eccv2012-15scenes-fasthik instaed with a proper interface
  28. */
  29. int main (int argc, char **argv)
  30. {
  31. std::set_terminate(__gnu_cxx::__verbose_terminate_handler);
  32. Config conf ( argc, argv );
  33. string ext = conf.gS("main", "ext", ".txt");
  34. cerr << "Using cache extension: " << ext << endl;
  35. MultiDataset md ( &conf );
  36. const ClassNames & classNamesTrain = md.getClassNames("train");
  37. // read training set
  38. vector< vector<double> > trainData;
  39. NICE::Vector y;
  40. const LabeledSet *train = md["train"];
  41. readData< vector< vector<double> >, vector<double> > ( conf, *train, trainData, y, ext );
  42. transposeVectorOfVectors ( trainData );
  43. // DEBUG
  44. #if 0
  45. Quantization q ( conf.gI("GPHIKClassifier", "num_bins") );
  46. for ( uint i = 0 ; i < trainData.size() ; i++ )
  47. for ( uint j = 0 ; j < trainData[i].size(); j++ )
  48. trainData[i][j] = q.getPrototype ( q.quantize( trainData[i][j] ) );
  49. #endif
  50. // END DEBUG
  51. cerr << "Size of the training set: " << trainData.size() << endl;
  52. double noise = 0.1;
  53. Timer t;
  54. std::cerr << " create FastMinKernel object with data" << std::endl;
  55. t.start();
  56. FastMinKernel *fmk = new FastMinKernel ( trainData, noise );
  57. t.stop();
  58. std::cerr << " time for setting up FMK object: " << t.getLast() << std::endl;
  59. std::cerr << " Initialize FMKGPHyperparameterOptimization object " << std::endl;
  60. t.start();
  61. FMKGPHyperparameterOptimization hyper ( &conf, fmk, "FMKGPHyperparameterOptimization" );
  62. t.stop();
  63. std::cerr << " time for setting up FMKGP object: " << t.getLast() << std::endl;
  64. std::cerr << " start optimization" << std::endl;
  65. t.start();
  66. hyper.optimize ( y );
  67. t.stop();
  68. std::cerr << " time for optimization: " << t.getLast() << std::endl;
  69. // ------------------ TESTING
  70. // q'n'd memory extensive solution
  71. const LabeledSet *test = md["test"];
  72. VVector testData;
  73. Vector yTest;
  74. readData< VVector, Vector > ( conf, *test, testData, yTest, ext );
  75. // DEBUG
  76. #if 0
  77. for ( uint i = 0 ; i < testData.size() ; i++ )
  78. for ( uint j = 0 ; j < testData[i].size(); j++ )
  79. testData[i][j] = q.getPrototype ( q.quantize( testData[i][j] ) );
  80. #endif
  81. //DEBUG END
  82. Matrix confusion ( y.Max()+1, yTest.Max() + 1, 0.0 );
  83. for ( uint i = 0 ; i < testData.size(); i++ )
  84. {
  85. const Vector & xstar = testData[i];
  86. // the following is just to be sure that we
  87. // do not count the time necessary for conversion
  88. SparseVector xstar_sparse ( xstar );
  89. uint classno_groundtruth = yTest[i];
  90. SparseVector scores;
  91. t.start();
  92. uint classno_estimated = hyper.classify ( xstar_sparse, scores );
  93. t.stop();
  94. scores.store(cerr);
  95. cerr << "[" << i << " / " << testData.size() << "] " << classno_estimated << " " << classno_groundtruth << " time: " << t.getLast() << endl;
  96. //confusion( classno_groundtruth, classno_estimated ) += 1;
  97. confusion( classno_estimated, classno_groundtruth ) += 1;
  98. }
  99. confusion.normalizeColumnsL1();
  100. cerr << confusion << endl;
  101. cerr << "average recognition rate: " << confusion.trace()/confusion.rows() << endl;
  102. return 0;
  103. }