testFeatureLearning.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /**
  2. * @file testFeatureLearning.cpp
  3. * @brief test the feature learning routines to incrementally increase / adapt the codebook currently used
  4. * @author Alexander Freytag
  5. * @date 11-04-2013
  6. */
  7. #include <iostream>
  8. #include <limits>
  9. #include <core/basics/Config.h>
  10. #include <core/basics/ResourceStatistics.h>
  11. #include <core/image/Convert.h>
  12. #include <core/vector/VectorT.h>
  13. #include <vislearning/baselib/Globals.h>
  14. #include <vislearning/baselib/ICETools.h>
  15. #include <vislearning/cbaselib/MultiDataset.h>
  16. #include <vislearning/cbaselib/Example.h>
  17. #include "vislearning/featureLearning/FeatureLearningGeneric.h"
  18. #include "vislearning/featureLearning/FeatureLearningClusterBased.h"
  19. using namespace std;
  20. using namespace NICE;
  21. using namespace OBJREC;
  22. /**
  23. test feature learning routines
  24. */
  25. int main( int argc, char **argv )
  26. {
  27. std::set_terminate( __gnu_cxx::__verbose_terminate_handler );
  28. Config * conf = new Config ( argc, argv );
  29. bool showTrainingImages= conf->gB( "featureLearning", "showTrainingImages", false );
  30. bool showTestImages= conf->gB( "featureLearning", "showTestImages", false );
  31. ResourceStatistics rs;
  32. std::string resultdir;
  33. resultdir = conf->gS( "featureLearning", "resultdir", "/tmp/");
  34. //**********************************************
  35. //
  36. // READ INITIAL TRAINING SET TO COMPUTE
  37. // AN INITIAL CODEBOOK
  38. //
  39. //**********************************************
  40. std::cerr << " READ INITIAL TRAINING SET TO COMPUTE AN INITIAL CODEBOOK" << std::endl;
  41. MultiDataset md( conf );
  42. const LabeledSet *trainFiles = md["train"];
  43. //**********************************************
  44. //
  45. // SET UP THE FEATURE LEARNING ALGO
  46. //
  47. //**********************************************
  48. OBJREC::FeatureLearningGeneric * featureLearning;
  49. featureLearning = new OBJREC::FeatureLearningClusterBased( conf, &md );
  50. //print computed cluster centers -- this is NOT recommended :)
  51. // prototypes.store(std::cerr);
  52. //evaluate how well the training images are covered with our initial codebook
  53. //that is, compute these nice "novelty maps" per feature
  54. //NOTE we skip this currently
  55. // LOOP_ALL_S( *trainFiles )
  56. // {
  57. // EACH_INFO( classno, info );
  58. // std::string filename = info.img();
  59. //
  60. // featureLearning->evaluateCurrentCodebook( filename , true /* beforeComputingNewFeatures */);
  61. // }
  62. //**********************************************
  63. //
  64. // FOR-LOOP OVER UNSEEN IMAGES
  65. //
  66. // EXTRACT FEATURES, CLUSTER THEM, TAKE
  67. // MOST "VALUABLE" CLUSTERS AS NEW
  68. // REPRESENTATIVES IN AN INCREASED CODEBOK
  69. //
  70. //**********************************************
  71. const LabeledSet *testFiles = md["test"];
  72. std::cerr << "start looping over all files" << std::endl;
  73. int imageCnt ( 0 );
  74. LOOP_ALL_S( *testFiles )
  75. {
  76. EACH_INFO( classno, info );
  77. std::string filename = info.img();
  78. NICE::ColorImage orig( filename );
  79. // showImage( orig, "Input" );
  80. NICE::FloatImage noveltyImageBefore;
  81. noveltyImageBefore = featureLearning->evaluateCurrentCodebook( filename , true /* beforeComputingNewFeatures */ );
  82. //**********************************************
  83. //
  84. // IS THIS IMAGE NOVEL?
  85. //
  86. // IF NOT, GO TO THE NEXT ONE
  87. //
  88. //**********************************************
  89. //TODO currently hard coded, and only stupid averaging of feature novelties for whole image, and hard thresholding!
  90. double meanNovelty ( 0.0 );
  91. for ( uint y = 0 ; y < ( uint ) noveltyImageBefore.height() ; y++ )
  92. {
  93. for ( uint x = 0 ; x < ( uint ) noveltyImageBefore.width(); x++ )
  94. {
  95. meanNovelty += noveltyImageBefore(x,y);
  96. }
  97. }
  98. int imageSize ( noveltyImageBefore.height() * noveltyImageBefore.width() );
  99. meanNovelty /= imageSize;
  100. double stupidThreshold ( 0.0022 );
  101. std::cerr << " NOVELTY SCORE FOR CURRENT IMAGE: " << meanNovelty << " -- threshold: " << stupidThreshold << std::endl;
  102. if ( meanNovelty < stupidThreshold )
  103. {
  104. std::cerr << " --- NOT NOVEL --- " << std::endl;
  105. continue;
  106. }
  107. else
  108. {
  109. std::cerr << " --- NOVEL --- " << std::endl;
  110. }
  111. featureLearning->learnNewFeatures( filename );
  112. NICE::FloatImage noveltyImageAfter;
  113. noveltyImageAfter = featureLearning->evaluateCurrentCodebook( filename , false /* beforeComputingNewFeatures */ );
  114. NICE::FloatImage noveltyImageDifference ( noveltyImageAfter.width(), noveltyImageAfter.height());
  115. for ( uint y = 0 ; y < ( uint ) noveltyImageAfter.height() ; y++ )
  116. {
  117. for ( uint x = 0 ; x < ( uint ) noveltyImageAfter.width(); x++ )
  118. {
  119. noveltyImageDifference(x,y) = fabs ( noveltyImageBefore(x,y) - noveltyImageAfter(x,y) );
  120. }
  121. }
  122. NICE::ColorImage noveltyImageDifferenceRGB (noveltyImageAfter.width(), noveltyImageAfter.height() );
  123. imageToPseudoColor( noveltyImageDifference, noveltyImageDifferenceRGB );
  124. std::vector< std::string > list2;
  125. StringTools::split ( filename, '/', list2 );
  126. std::string destination ( resultdir + NICE::intToString(imageCnt) + "_" + list2.back() + "_4_differenceOfNovelties.ppm");
  127. noveltyImageDifferenceRGB.writePPM( destination );
  128. imageCnt++;
  129. } //Loop over all test images
  130. return 0;
  131. }