testImageNetMedian.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /**
  2. * @file testImageNetMedian.cpp
  3. * @brief test our median prototype idea
  4. * @author Erik Rodner
  5. * @date 01/04/2012
  6. */
  7. #include <core/basics/Config.h>
  8. #include <core/matlabAccess/MatFileIO.h>
  9. //----------
  10. #include <vislearning/matlabAccessHighLevel/ImageNetData.h>
  11. //----------
  12. #include <gp-hik-core/FMKGPHyperparameterOptimization.h>
  13. #include <gp-hik-core/parameterizedFunctions/PFAbsExp.h>
  14. #include <gp-hik-core/tools.h>
  15. using namespace std;
  16. using namespace NICE;
  17. /**
  18. test the basic functionality of fast-hik hyperparameter optimization
  19. */
  20. int main (int argc, char **argv)
  21. {
  22. std::set_terminate(__gnu_cxx::__verbose_terminate_handler);
  23. Config conf ( argc, argv );
  24. // std::string root = "/home/dbv/bilder/imagenet/devkit-1.0/demo/";
  25. std::string root = "/users2/rodner/data/imagenet/devkit-1.0/demo/";
  26. ImageNetData imageNet ( root );
  27. cerr << "Reading ImageNet data files (takes some seconds)..." << endl;
  28. NICE::Vector y;
  29. sparse_t data;
  30. imageNet.getBatchData ( data, y, "train", "training" );
  31. uint n = y.size();
  32. map<int, int> examples;
  33. for ( uint i = 0 ; i < n; i++ )
  34. examples.insert( pair<int, int> (i, i) );
  35. y.resize(n);
  36. cerr << "Sorting features ..." << endl;
  37. FeatureMatrix fm ( data, examples );
  38. // count the number of examples of each class
  39. Vector elementCounts (y.Max()+1, 0.0);
  40. for ( uint i = 0 ; i < n; i++ )
  41. elementCounts[ y[i] ]++;
  42. // calculate the median for each class
  43. map<int, SparseVector> medians;
  44. for ( int dim = 0 ; dim < fm.get_d(); dim++ )
  45. {
  46. cerr << "Calculating the median values for dimension " << dim << endl;
  47. SparseVector classMedians;
  48. fm.getFeatureValues(dim).getClassMedians ( classMedians, y, elementCounts );
  49. for ( SparseVector::const_iterator i = classMedians.begin(); i != classMedians.end(); i++ )
  50. {
  51. int classno = i->first;
  52. double medianValue = i->second;
  53. medians[classno].insert ( pair<int, double> ( dim, medianValue ) );
  54. }
  55. }
  56. // ------------------------------ TESTING ------------------------------
  57. cerr << "Reading ImageNet test data files (takes some seconds)..." << endl;
  58. imageNet.preloadData ( "val", "testing" );
  59. for ( uint i = 0 ; i < imageNet.getNumPreloadedExamples(); i++ )
  60. {
  61. const SparseVector & svec = imageNet.getPreloadedExample ( i );
  62. set< pair<double, int> > scores;
  63. // calculate the distance to each of the classes !
  64. for ( map<int, SparseVector>::const_iterator k = medians.begin(); k != medians.end(); k++ )
  65. {
  66. const SparseVector & prototype = k->second;
  67. int classno = k->first;
  68. //double kval = prototype.minimumKernel ( svec );
  69. double kval = prototype.innerProduct ( svec );
  70. scores.insert ( pair<double, int> ( -1.0 * kval, classno ) );
  71. }
  72. cerr << "# groundtruth example: " << imageNet.getPreloadedLabel(i) << " " << i << " / " << imageNet.getNumPreloadedExamples() << endl;
  73. uint nBestClasses = std::min( 5, (int)scores.size() );
  74. uint kk = 0;
  75. for ( set< pair<double, int> >::const_iterator k = scores.begin(); k != scores.end() && kk < nBestClasses; k++, kk++ )
  76. cerr << k->second << " ";
  77. cerr << endl;
  78. }
  79. return 0;
  80. }