eccv2012-15scenes.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. std::cerr << " number of examples: " << trainData.size() << std::endl;
  43. transposeVectorOfVectors ( trainData );
  44. std::cerr << " number of dimensions: " << trainData.size() << std::endl;
  45. // DEBUG
  46. #if 0
  47. Quantization q ( conf.gI("GPHIKClassifier", "num_bins") );
  48. for ( uint i = 0 ; i < trainData.size() ; i++ )
  49. for ( uint j = 0 ; j < trainData[i].size(); j++ )
  50. trainData[i][j] = q.getPrototype ( q.quantize( trainData[i][j] ) );
  51. #endif
  52. // END DEBUG
  53. cerr << "Size of the training set: " << trainData.size() << endl;
  54. double noise = 0.1;
  55. Timer t;
  56. std::cerr << " create FastMinKernel object with data" << std::endl;
  57. t.start();
  58. FastMinKernel *fmk = new FastMinKernel ( trainData, noise );
  59. t.stop();
  60. std::cerr << " time for setting up FMK object: " << t.getLast() << std::endl;
  61. bool b_debug = conf.gB( "FMKGPHyperparameterOptimization", "debug", false);
  62. std::cerr << " debug: " << b_debug << std::endl;
  63. if ( b_debug )
  64. {
  65. fmk->store ( std::cerr );
  66. }
  67. std::cerr << " Initialize FMKGPHyperparameterOptimization object " << std::endl;
  68. t.start();
  69. FMKGPHyperparameterOptimization hyper ( &conf, fmk, "FMKGPHyperparameterOptimization" );
  70. t.stop();
  71. std::cerr << " time for setting up FMKGP object: " << t.getLast() << std::endl;
  72. std::cerr << " start optimization" << std::endl;
  73. t.start();
  74. hyper.optimize ( y );
  75. t.stop();
  76. std::cerr << " time for optimization: " << t.getLast() << std::endl;
  77. // ------------------ TESTING
  78. // q'n'd memory extensive solution
  79. const LabeledSet *test = md["test"];
  80. VVector testData;
  81. Vector yTest;
  82. readData< VVector, Vector > ( conf, *test, testData, yTest, ext );
  83. // DEBUG
  84. #if 0
  85. for ( uint i = 0 ; i < testData.size() ; i++ )
  86. for ( uint j = 0 ; j < testData[i].size(); j++ )
  87. testData[i][j] = q.getPrototype ( q.quantize( testData[i][j] ) );
  88. #endif
  89. //DEBUG END
  90. Matrix confusion ( y.Max()+1, yTest.Max() + 1, 0.0 );
  91. for ( uint i = 0 ; i < testData.size(); i++ )
  92. {
  93. const Vector & xstar = testData[i];
  94. // the following is just to be sure that we
  95. // do not count the time necessary for conversion
  96. SparseVector xstar_sparse ( xstar );
  97. uint classno_groundtruth = yTest[i];
  98. SparseVector scores;
  99. t.start();
  100. uint classno_estimated = hyper.classify ( xstar_sparse, scores );
  101. t.stop();
  102. scores.store(cerr);
  103. cerr << "[" << i << " / " << testData.size() << "] " << classno_estimated << " " << classno_groundtruth << " time: " << t.getLast() << endl;
  104. //confusion( classno_groundtruth, classno_estimated ) += 1;
  105. confusion( classno_estimated, classno_groundtruth ) += 1;
  106. }
  107. confusion.normalizeColumnsL1();
  108. cerr << confusion << endl;
  109. cerr << "average recognition rate: " << confusion.trace()/confusion.rows() << endl;
  110. return 0;
  111. }