testFeatureLearning.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. //STL
  8. #include <iostream>
  9. #include <limits>
  10. //core
  11. #include <core/basics/Config.h>
  12. #include <core/basics/ResourceStatistics.h>
  13. #include <core/image/Convert.h>
  14. #include <core/vector/VectorT.h>
  15. //vislearning
  16. #include <vislearning/baselib/Globals.h>
  17. #include <vislearning/baselib/ICETools.h>
  18. #include <vislearning/cbaselib/MultiDataset.h>
  19. #include <vislearning/cbaselib/Example.h>
  20. //
  21. #include "vislearning/featureLearning/FeatureLearningGeneric.h"
  22. #include "vislearning/featureLearning/FeatureLearningClusterBased.h"
  23. #include "vislearning/featureLearning/FeatureLearningRegionBased.h"
  24. //
  25. #include "vislearning/noveltyDetection/NDCodebookLevelImagePooling.h"
  26. using namespace std;
  27. using namespace NICE;
  28. using namespace OBJREC;
  29. /**
  30. test feature learning routines
  31. */
  32. int main( int argc, char **argv )
  33. {
  34. std::set_terminate( __gnu_cxx::__verbose_terminate_handler );
  35. Config * conf = new Config ( argc, argv );
  36. bool showTrainingImages= conf->gB( "featureLearning", "showTrainingImages", false );
  37. bool showTestImages= conf->gB( "featureLearning", "showTestImages", false );
  38. ResourceStatistics rs;
  39. std::string resultdir;
  40. resultdir = conf->gS( "featureLearning", "resultdir", "/tmp/");
  41. //**********************************************
  42. //
  43. // READ INITIAL TRAINING SET TO COMPUTE
  44. // AN INITIAL CODEBOOK
  45. //
  46. //**********************************************
  47. std::cerr << " READ INITIAL TRAINING SET TO COMPUTE AN INITIAL CODEBOOK" << std::endl;
  48. MultiDataset md( conf );
  49. const LabeledSet *trainFiles = md["train"];
  50. //**********************************************
  51. //
  52. // SET UP THE FEATURE LEARNING ALGO
  53. //
  54. //**********************************************
  55. OBJREC::FeatureLearningGeneric * featureLearning;
  56. std::string featureLearningMethod = conf->gS( "featureLearning", "featureLearningMethod", "clusterBased" );
  57. if (featureLearningMethod.compare("clusterBased") == 0)
  58. {
  59. featureLearning = new OBJREC::FeatureLearningClusterBased( conf, &md );
  60. }
  61. else if (featureLearningMethod.compare("regionBased") == 0)
  62. {
  63. featureLearning = new OBJREC::FeatureLearningRegionBased( conf, &md );
  64. }
  65. else
  66. {
  67. std::cerr << "Unknown feature learning algorithm selected, use cluster based instead" << std::endl;
  68. featureLearning = new OBJREC::FeatureLearningClusterBased( conf, &md );
  69. }
  70. //**********************************************
  71. //
  72. // SET UP THE NOVELTY DECTION ALGO
  73. //
  74. //**********************************************
  75. OBJREC::NDCodebookLevelImagePooling * novDetector;
  76. novDetector = new OBJREC::NDCodebookLevelImagePooling( conf, &md, "featureLearning" );
  77. //evaluate how well the training images are covered with our initial codebook
  78. //that is, compute these nice "novelty maps" per feature
  79. //NOTE we skip this currently
  80. LOOP_ALL_S( *trainFiles )
  81. {
  82. EACH_INFO( classno, info );
  83. std::string filename = info.img();
  84. //
  85. // featureLearning->evaluateCurrentCodebook( filename , true /* beforeComputingNewFeatures */);
  86. NICE::ImageT< int > imgClusterAssignments;
  87. imgClusterAssignments = featureLearning->evaluateCurrentCodebookByAssignments(filename , false /* beforeComputingNewFeatures */, false /* _binaryShowLatestPrototype*/ );
  88. std::cerr << "now do image To pseude color and show the initial cluster assignments" << std::endl;
  89. NICE::ColorImage imgClusterAssignmentsRGB (imgClusterAssignments.width(), imgClusterAssignments.height() );
  90. imageToPseudoColor( imgClusterAssignments, imgClusterAssignmentsRGB );
  91. showImage(imgClusterAssignmentsRGB, "cluster Assignments" ) ;
  92. }
  93. //**********************************************
  94. //
  95. // EVALUATE INITIAL CLUSTER
  96. //
  97. // COMPUTE A NICE CONFUSION MATRIX
  98. // FOR OUR INITIAL CODEBOOK
  99. //
  100. //**********************************************
  101. NICE::Matrix confusionMatInitial;
  102. featureLearning->evaluateCurrentCodebookByConfusionMatrix( confusionMatInitial );
  103. std::cerr << "initial Confusion matrix: " << std::endl << confusionMatInitial << std::endl;
  104. //set the initially computed codebook to the novelty detection mechanism
  105. //TODO this should be done, but currently we do not care about
  106. // novDetector->setCodebook( featureLearning->getCurrentCodebook() );
  107. //**********************************************
  108. //
  109. // FOR-LOOP OVER UNSEEN IMAGES
  110. //
  111. // EXTRACT FEATURES, CLUSTER THEM, TAKE
  112. // MOST "VALUABLE" CLUSTERS AS NEW
  113. // REPRESENTATIVES IN AN INCREASED CODEBOK
  114. //
  115. //**********************************************
  116. const LabeledSet *testFiles = md["test"];
  117. std::cerr << "start looping over all files" << std::endl;
  118. int imageCnt ( 0 );
  119. LOOP_ALL_S( *testFiles )
  120. {
  121. EACH_INFO( classno, info );
  122. std::string filename = info.img();
  123. NICE::ColorImage orig( filename );
  124. NICE::FloatImage noveltyImageBefore;
  125. noveltyImageBefore = featureLearning->evaluateCurrentCodebookByDistance( filename , true /* beforeComputingNewFeatures */ );
  126. //**********************************************
  127. //
  128. // IS THIS IMAGE NOVEL?
  129. //
  130. // IF NOT, GO TO THE NEXT ONE
  131. //
  132. //**********************************************
  133. bool b_isImageNovel ( novDetector->evaluateNoveltyOfImage( noveltyImageBefore ) );
  134. if ( ! b_isImageNovel )
  135. {
  136. std::cerr << " --- NOT NOVEL --- " << std::endl << std::endl;
  137. continue;
  138. }
  139. else
  140. {
  141. std::cerr << " --- NOVEL --- " << std::endl;
  142. }
  143. //**********************************************
  144. //
  145. // LEARN NEW FEATURE FOR A NOVEL IMAGE
  146. //
  147. //**********************************************
  148. featureLearning->learnNewFeatures( filename );
  149. //and update the codebook pointer within our novelty detection algorithm
  150. //TODO this should be done, but currently we do not care about
  151. // novDetector->setCodebook( featureLearning->getCurrentCodebook() );
  152. //**********************************************
  153. //
  154. // EVALUATE HOW WELL THE CURRENT IMAGE
  155. // CAN BE EXPLAINED AFTER
  156. // COMPUTING A NEW FEATURE
  157. //
  158. // SHOULD WE REPEAT THIS UNTIL THE IMAGE
  159. // IS NOT CLASSIFIED AS "NOVEL" ANYMORE?
  160. //
  161. //**********************************************
  162. NICE::FloatImage noveltyImageAfter;
  163. noveltyImageAfter = featureLearning->evaluateCurrentCodebookByDistance( filename , false /* beforeComputingNewFeatures */ );
  164. NICE::FloatImage noveltyImageDifference ( noveltyImageAfter.width(), noveltyImageAfter.height());
  165. for ( uint y = 0 ; y < ( uint ) noveltyImageAfter.height() ; y++ )
  166. {
  167. for ( uint x = 0 ; x < ( uint ) noveltyImageAfter.width(); x++ )
  168. {
  169. noveltyImageDifference(x,y) = fabs ( noveltyImageBefore(x,y) - noveltyImageAfter(x,y) );
  170. }
  171. }
  172. std::cerr << "min diff: " << noveltyImageDifference.min() << " and max diff: " << noveltyImageDifference.max() << std::endl;
  173. NICE::ColorImage noveltyImageDifferenceRGB (noveltyImageAfter.width(), noveltyImageAfter.height() );
  174. imageToPseudoColor( noveltyImageDifference, noveltyImageDifferenceRGB );
  175. std::vector< std::string > list2;
  176. StringTools::split ( filename, '/', list2 );
  177. std::string destination ( resultdir + NICE::intToString(imageCnt) + "_" + list2.back() + "_4_differenceOfNovelties.ppm");
  178. noveltyImageDifferenceRGB.writePPM( destination );
  179. NICE::ImageT< int > imgClusterAssignmentsBinary;
  180. imgClusterAssignmentsBinary = featureLearning->evaluateCurrentCodebookByAssignments(filename , true, true /* _binaryShowLatestPrototype*/ );
  181. NICE::ImageT< int > imgClusterAssignments;
  182. imgClusterAssignments = featureLearning->evaluateCurrentCodebookByAssignments(filename , true, false /* _binaryShowLatestPrototype*/ );
  183. NICE::ColorImage imgClusterAssignmentsBinaryRGB (imgClusterAssignmentsBinary.width(), imgClusterAssignmentsBinary.height() );
  184. imageToPseudoColor( imgClusterAssignmentsBinary, imgClusterAssignmentsBinaryRGB );
  185. NICE::ColorImage imgClusterAssignmentsRGB (imgClusterAssignments.width(), imgClusterAssignments.height() );
  186. imageToPseudoColor( imgClusterAssignments, imgClusterAssignmentsRGB );
  187. if ( false )
  188. {
  189. showImage(imgClusterAssignmentsBinaryRGB, "cluster Assignments Binary (latest prototype)" ) ;
  190. showImage(imgClusterAssignmentsRGB, "cluster Assignments" ) ;
  191. }
  192. else
  193. {
  194. std::string destination ( resultdir + NICE::intToString(imageCnt) + "_" + list2.back() + "_5_clusterAssignments.ppm");
  195. std::string destinationBinary ( resultdir + NICE::intToString(imageCnt) + "_" + list2.back() + "_6_clusterAssignmentsBinary.ppm");
  196. imgClusterAssignmentsRGB.writePPM( destination );
  197. imgClusterAssignmentsBinaryRGB.writePPM( destinationBinary );
  198. }
  199. imageCnt++;
  200. } //Loop over all test images
  201. //don't waste memory
  202. std::cerr << "don't waste memory - cleaning up" << std::endl;
  203. // if (trainFiles != NULL)
  204. // delete trainFiles;
  205. if (featureLearning != NULL)
  206. delete featureLearning;
  207. if (novDetector != NULL)
  208. delete novDetector;
  209. if (conf != NULL)
  210. delete conf;
  211. return 0;
  212. }