testImageNetBinary.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /**
  2. * @file testImageNetBinary.cpp
  3. * @brief perform ImageNet tests with binary tasks for OCC
  4. * @author Alexander Lütz
  5. * @date 23-05-2012 (dd-mm-yyyy)
  6. */
  7. #include "core/basics/Config.h"
  8. #include "vislearning/cbaselib/ClassificationResults.h"
  9. #include "vislearning/baselib/ProgressBar.h"
  10. #include "core/matlabAccess/MatFileIO.h"
  11. #include "vislearning/matlabAccessHighLevel/ImageNetData.h"
  12. #include "vislearning/classifier/kernelclassifier/KCGPOneClass.h"
  13. #include "vislearning/classifier/kernelclassifier/KCGPApproxOneClass.h"
  14. #include "vislearning/math/kernels/KernelData.h"
  15. #include "vislearning/math/kernels/Kernel.h"
  16. #include "vislearning/math/kernels/KernelRBF.h"
  17. #include "vislearning/math/kernels/KernelExp.h"
  18. // #include "fast-hik/tools.h"
  19. using namespace std;
  20. using namespace NICE;
  21. using namespace OBJREC;
  22. /**
  23. test the basic functionality of fast-hik hyperparameter optimization
  24. */
  25. int main (int argc, char **argv)
  26. {
  27. std::set_terminate(__gnu_cxx::__verbose_terminate_handler);
  28. Config conf ( argc, argv );
  29. string resultsfile = conf.gS("main", "results", "results.txt" );
  30. int positiveClass = conf.gI("main", "positive_class");
  31. std::cerr << "Positive class is " << positiveClass << std::endl;
  32. sparse_t data;
  33. NICE::Vector y;
  34. std::cerr << "Reading ImageNet data ..." << std::endl;
  35. bool imageNetLocal = conf.gB("main", "imageNetLocal" , false);
  36. string imageNetPath;
  37. if (imageNetLocal)
  38. imageNetPath = "/users2/rodner/data/imagenet/devkit-1.0/";
  39. else
  40. imageNetPath = "/home/dbv/bilder/imagenet/devkit-1.0/";
  41. ImageNetData imageNet ( imageNetPath + "demo/" );
  42. // imageNet.getBatchData ( data, y, "train", "training" );
  43. LabeledSetVector train;
  44. imageNet.loadDataAsLabeledSetVector( train );
  45. //set up the kernel function
  46. double rbf_sigma = conf.gD("main", "rbf_sigma", -2.0 );
  47. KernelRBF kernelFunction ( rbf_sigma, 0.0 );
  48. //KernelExp kernelFunction ( rbf_sigma, 0.0, 0.0 );
  49. //set up our OC-classifier
  50. string classifierName = conf.gS("main", "classifier", "KCGPApproxOneClass");
  51. KernelClassifier *classifier;
  52. if(strcmp("KCGPApproxOneClass",classifierName.c_str())==0)
  53. {
  54. classifier = new KCGPApproxOneClass ( &conf, &kernelFunction );
  55. }
  56. else if (strcmp("KCGPOneClass",classifierName.c_str())==0) {
  57. classifier = new KCGPOneClass ( &conf, &kernelFunction );
  58. }
  59. else{ //default
  60. classifier = new KCGPApproxOneClass ( &conf, &kernelFunction );
  61. }
  62. //and perform the training
  63. classifier->teach( train );
  64. // uint n = y.size();
  65. //
  66. // set<int> positives;
  67. // set<int> negatives;
  68. //
  69. // map< int, set<int> > mysets;
  70. // for ( uint i = 0 ; i < n; i++ )
  71. // mysets[ y[i] ].insert ( i );
  72. //
  73. // if ( mysets[ positiveClass ].size() == 0 )
  74. // fthrow(Exception, "Class " << positiveClass << " is not available.");
  75. //
  76. // // add our positive examples
  77. // for ( set<int>::const_iterator i = mysets[positiveClass].begin(); i != mysets[positiveClass].end(); i++ )
  78. // positives.insert ( *i );
  79. //
  80. // int Nneg = conf.gI("main", "nneg", 1 );
  81. // for ( map<int, set<int> >::const_iterator k = mysets.begin(); k != mysets.end(); k++ )
  82. // {
  83. // int classno = k->first;
  84. // if ( classno == positiveClass )
  85. // continue;
  86. // const set<int> & s = k->second;
  87. // uint ind = 0;
  88. // for ( set<int>::const_iterator i = s.begin(); (i != s.end() && ind < Nneg); i++,ind++ )
  89. // negatives.insert ( *i );
  90. // }
  91. // std::cerr << "Number of positive examples: " << positives.size() << std::endl;
  92. // std::cerr << "Number of negative examples: " << negatives.size() << std::endl;
  93. // ------------------------------ TESTING ------------------------------
  94. std::cerr << "Reading ImageNet test data files (takes some seconds)..." << std::endl;
  95. imageNet.preloadData ( "val", "testing" );
  96. imageNet.loadExternalLabels ( imageNetPath + "data/ILSVRC2010_validation_ground_truth.txt" );
  97. ClassificationResults results;
  98. std::cerr << "Classification step ... with " << imageNet.getNumPreloadedExamples() << " examples" << std::endl;
  99. ProgressBar pb;
  100. for ( uint i = 0 ; i < (uint)imageNet.getNumPreloadedExamples(); i++ )
  101. {
  102. pb.update ( imageNet.getNumPreloadedExamples() );
  103. const SparseVector & svec = imageNet.getPreloadedExample ( i );
  104. NICE::Vector vec;
  105. svec.convertToVectorT( vec );
  106. // classification step
  107. ClassificationResult r = classifier->classify ( vec );
  108. // set ground truth label
  109. r.classno_groundtruth = (((int)imageNet.getPreloadedLabel ( i )) == positiveClass) ? 1 : 0;
  110. results.push_back ( r );
  111. }
  112. std::cerr << "Writing results to " << resultsfile << std::endl;
  113. results.writeWEKA ( resultsfile, 0 );
  114. double perfvalue = results.getBinaryClassPerformance( ClassificationResults::PERF_AUC );
  115. std::cerr << "Performance: " << perfvalue << std::endl;
  116. //don't waste memory
  117. delete classifier;
  118. return 0;
  119. }