eccv2012-15scenes.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /**
  2. * @file eccv2012-15scenes.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/cbaselib/MultiDataset.h>
  12. #include <vislearning/baselib/Globals.h>
  13. #include <gp-hik-core/FastMinKernel.h>
  14. #include <gp-hik-core/FMKGPHyperparameterOptimization.h>
  15. #include <gp-hik-core/parameterizedFunctions/PFAbsExp.h>
  16. #include <gp-hik-core/parameterizedFunctions/PFWeightedDim.h>
  17. #include <gp-hik-core/parameterizedFunctions/PFExp.h>
  18. #include <gp-hik-core/parameterizedFunctions/PFMKL.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. /**
  25. ECCV 2012 Experiment with 15 Scenes
  26. NOTE: usage of this test-function is not recommended. Use eccv2012-15scenes-fasthik instaed with a proper interface
  27. */
  28. int main (int argc, char **argv)
  29. {
  30. std::set_terminate(__gnu_cxx::__verbose_terminate_handler);
  31. Config conf ( argc, argv );
  32. string pf_s = conf.gS("main", "transform", "absexp");
  33. ParameterizedFunction *pf;
  34. double parameterLowerBound = conf.gD("GPHIKClassifier", "parameter_lower_bound", 1.0 );
  35. double parameterUpperBound = conf.gD("GPHIKClassifier", "parameter_upper_bound", 5.0 );
  36. if ( pf_s == "absexp" )
  37. pf = new PFAbsExp( 1.0, parameterLowerBound, parameterUpperBound );
  38. else if ( pf_s == "exp" )
  39. pf = new PFExp ( 1.0, parameterLowerBound, parameterUpperBound );
  40. else if ( pf_s == "weighted" )
  41. pf = new PFWeightedDim ( conf.gI("main", "dimension"), 0.0, 5.0 );
  42. else if ( pf_s == "MKL" ) {
  43. std::cerr << "use MKL feature transformation specific for VISAPP experiments" << std::endl;
  44. std::set<int> steps; steps.insert(4000); steps.insert(6000); //specific for VISAPP
  45. pf = new PFMKL( steps, parameterLowerBound, parameterUpperBound );
  46. }
  47. else
  48. fthrow(Exception, "Parameterized function type " << pf_s << " not yet implemented");
  49. cerr << "Transformation type: " << pf_s << endl;
  50. string ext = conf.gS("main", "ext", ".txt");
  51. cerr << "Using cache extension: " << ext << endl;
  52. MultiDataset md ( &conf );
  53. const ClassNames & classNamesTrain = md.getClassNames("train");
  54. // read training set
  55. vector< vector<double> > trainData;
  56. Vector y;
  57. const LabeledSet *train = md["train"];
  58. readData< vector< vector<double> >, vector<double> > ( conf, *train, trainData, y, ext );
  59. transposeVectorOfVectors ( trainData );
  60. // DEBUG
  61. #if 0
  62. Quantization q ( conf.gI("GPHIKClassifier", "num_bins") );
  63. for ( uint i = 0 ; i < trainData.size() ; i++ )
  64. for ( uint j = 0 ; j < trainData[i].size(); j++ )
  65. trainData[i][j] = q.getPrototype ( q.quantize( trainData[i][j] ) );
  66. #endif
  67. // END DEBUG
  68. cerr << "Size of the training set: " << trainData.size() << endl;
  69. double noise = 0.1;
  70. FastMinKernel *fmk = new FastMinKernel ( trainData, noise );
  71. FMKGPHyperparameterOptimization hyper ( &conf, pf, fmk );
  72. hyper.optimize ( y );
  73. // ------------------ TESTING
  74. // q'n'd memory extensive solution
  75. const LabeledSet *test = md["test"];
  76. VVector testData;
  77. Vector yTest;
  78. readData< VVector, Vector > ( conf, *test, testData, yTest, ext );
  79. // DEBUG
  80. #if 0
  81. for ( uint i = 0 ; i < testData.size() ; i++ )
  82. for ( uint j = 0 ; j < testData[i].size(); j++ )
  83. testData[i][j] = q.getPrototype ( q.quantize( testData[i][j] ) );
  84. #endif
  85. //DEBUG END
  86. Timer t;
  87. Matrix confusion ( y.Max()+1, yTest.Max() + 1, 0.0 );
  88. for ( uint i = 0 ; i < testData.size(); i++ )
  89. {
  90. const Vector & xstar = testData[i];
  91. // the following is just to be sure that we
  92. // do not count the time necessary for conversion
  93. SparseVector xstar_sparse ( xstar );
  94. uint classno_groundtruth = yTest[i];
  95. SparseVector scores;
  96. t.start();
  97. uint classno_estimated = hyper.classify ( xstar_sparse, scores );
  98. t.stop();
  99. scores.store(cerr);
  100. cerr << "[" << i << " / " << testData.size() << "] " << classno_estimated << " " << classno_groundtruth << " time: " << t.getLast() << endl;
  101. //confusion( classno_groundtruth, classno_estimated ) += 1;
  102. confusion( classno_estimated, classno_groundtruth ) += 1;
  103. }
  104. confusion.normalizeColumnsL1();
  105. cerr << confusion << endl;
  106. cerr << "average recognition rate: " << confusion.trace()/confusion.rows() << endl;
  107. return 0;
  108. }