testFeatureLearning.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. bool showResults = conf->gB( "featureLearning", "showResults", false );
  39. ResourceStatistics rs;
  40. std::string resultdir;
  41. resultdir = conf->gS( "featureLearning", "resultdir", "/tmp/");
  42. NICE::ImageT<int> noveltyScale ( 20, 100 );
  43. for (int j = 0; j < 100; j++)
  44. {
  45. for (int i = 0; i < 20; i++)
  46. noveltyScale(i,j) = 99-j;
  47. }
  48. NICE::ColorImage noveltyScaleRGB (noveltyScale.width(), noveltyScale.height() );
  49. imageToPseudoColor( noveltyScale, noveltyScaleRGB );
  50. std::string destinationNoveltyScale ( resultdir + "_" + "_noveltyScale.ppm");
  51. noveltyScaleRGB.writePPM( destinationNoveltyScale );
  52. //**********************************************
  53. //
  54. // READ INITIAL TRAINING SET TO COMPUTE
  55. // AN INITIAL CODEBOOK
  56. //
  57. //**********************************************
  58. std::cerr << " READ INITIAL TRAINING SET TO COMPUTE AN INITIAL CODEBOOK" << std::endl;
  59. MultiDataset md( conf );
  60. const LabeledSet *trainFiles = md["train"];
  61. //**********************************************
  62. //
  63. // SET UP THE FEATURE LEARNING ALGO
  64. //
  65. //**********************************************
  66. OBJREC::FeatureLearningGeneric * featureLearning;
  67. std::string featureLearningMethod = conf->gS( "featureLearning", "featureLearningMethod", "clusterBased" );
  68. if (featureLearningMethod.compare("clusterBased") == 0)
  69. {
  70. featureLearning = new OBJREC::FeatureLearningClusterBased( conf, &md );
  71. }
  72. else if (featureLearningMethod.compare("regionBased") == 0)
  73. {
  74. featureLearning = new OBJREC::FeatureLearningRegionBased( conf, &md );
  75. }
  76. else
  77. {
  78. std::cerr << "Unknown feature learning algorithm selected, use cluster based instead" << std::endl;
  79. featureLearning = new OBJREC::FeatureLearningClusterBased( conf, &md );
  80. }
  81. //**********************************************
  82. //
  83. // SET UP THE NOVELTY DECTION ALGO
  84. //
  85. //**********************************************
  86. OBJREC::NDCodebookLevelImagePooling * novDetector;
  87. novDetector = new OBJREC::NDCodebookLevelImagePooling( conf, &md, "featureLearning" );
  88. //evaluate how well the training images are covered with our initial codebook
  89. //that is, compute these nice "novelty maps" per feature
  90. //NOTE we skip this currently
  91. LOOP_ALL_S( *trainFiles )
  92. {
  93. EACH_INFO( classno, info );
  94. std::string filename = info.img();
  95. //
  96. // featureLearning->evaluateCurrentCodebook( filename , true /* beforeComputingNewFeatures */);
  97. NICE::ImageT< int > imgClusterAssignments;
  98. imgClusterAssignments = featureLearning->evaluateCurrentCodebookByAssignments(filename , false /* beforeComputingNewFeatures */, false /* _binaryShowLatestPrototype*/ );
  99. std::cerr << "now do image To pseude color and show the initial cluster assignments" << std::endl;
  100. NICE::ColorImage imgClusterAssignmentsRGB (imgClusterAssignments.width(), imgClusterAssignments.height() );
  101. imageToPseudoColor( imgClusterAssignments, imgClusterAssignmentsRGB );
  102. if ( showResults )
  103. showImage(imgClusterAssignmentsRGB, "cluster Assignments" ) ;
  104. else
  105. {
  106. std::vector< std::string > list2;
  107. StringTools::split ( filename, '/', list2 );
  108. std::string destination ( resultdir + NICE::intToString(0) + "_" + list2.back() + "_00_initialClusterAssignments.ppm");
  109. imgClusterAssignmentsRGB.writePPM( destination );
  110. }
  111. }
  112. //**********************************************
  113. //
  114. // EVALUATE INITIAL CLUSTER
  115. //
  116. // COMPUTE A NICE CONFUSION MATRIX
  117. // FOR OUR INITIAL CODEBOOK
  118. //
  119. //**********************************************
  120. NICE::Matrix confusionMatInitial;
  121. featureLearning->evaluateCurrentCodebookByConfusionMatrix( confusionMatInitial );
  122. std::cerr << "initial Confusion matrix: " << std::endl << confusionMatInitial << std::endl;
  123. //set the initially computed codebook to the novelty detection mechanism
  124. //TODO this should be done, but currently we do not care about
  125. // novDetector->setCodebook( featureLearning->getCurrentCodebook() );
  126. //**********************************************
  127. //
  128. // FOR-LOOP OVER UNSEEN IMAGES
  129. //
  130. // EXTRACT FEATURES, CLUSTER THEM, TAKE
  131. // MOST "VALUABLE" CLUSTERS AS NEW
  132. // REPRESENTATIVES IN AN INCREASED CODEBOK
  133. //
  134. //**********************************************
  135. const LabeledSet *testFiles = md["test"];
  136. std::cerr << "start looping over all files" << std::endl;
  137. int imageCnt ( 0 );
  138. LOOP_ALL_S( *testFiles )
  139. {
  140. EACH_INFO( classno, info );
  141. std::string filename = info.img();
  142. NICE::ColorImage orig( filename );
  143. NICE::ImageT< int > imgClusterAssignments;
  144. imgClusterAssignments = featureLearning->evaluateCurrentCodebookByAssignments(filename , false /* beforeComputingNewFeatures */, false /* _binaryShowLatestPrototype*/ );
  145. NICE::ColorImage imgClusterAssignmentsRGB (imgClusterAssignments.width(), imgClusterAssignments.height() );
  146. imageToPseudoColor( imgClusterAssignments, imgClusterAssignmentsRGB );
  147. NICE::FloatImage noveltyImageBefore;
  148. noveltyImageBefore = featureLearning->evaluateCurrentCodebookByDistance( filename , true /* beforeComputingNewFeatures */ );
  149. NICE::ColorImage noveltyImageBeforeRGB (noveltyImageBefore.width(), noveltyImageBefore.height() );
  150. imageToPseudoColor( noveltyImageBefore, noveltyImageBeforeRGB );
  151. if ( showResults )
  152. showImage(imgClusterAssignmentsRGB, "cluster Assignments" ) ;
  153. else
  154. {
  155. std::vector< std::string > list2;
  156. StringTools::split ( filename, '/', list2 );
  157. std::string destination ( resultdir + NICE::intToString(0) + "_" + list2.back() + "_00_initialClusterAssignments.ppm");
  158. imgClusterAssignmentsRGB.writePPM( destination );
  159. std::string destinationNovelty ( resultdir + NICE::intToString(0) + "_" + list2.back() + "_01_initialClusterDistances.ppm");
  160. noveltyImageBeforeRGB.writePPM( destinationNovelty );
  161. }
  162. //**********************************************
  163. //
  164. // IS THIS IMAGE NOVEL?
  165. //
  166. // IF NOT, GO TO THE NEXT ONE
  167. //
  168. //**********************************************
  169. bool b_isImageNovel ( novDetector->evaluateNoveltyOfImage( noveltyImageBefore ) );
  170. if ( ! b_isImageNovel )
  171. {
  172. std::cerr << " --- NOT NOVEL --- " << std::endl << std::endl;
  173. continue;
  174. }
  175. else
  176. {
  177. std::cerr << " --- NOVEL --- " << std::endl;
  178. }
  179. while ( b_isImageNovel )
  180. {
  181. //**********************************************
  182. //
  183. // LEARN NEW FEATURE FOR A NOVEL IMAGE
  184. //
  185. //**********************************************
  186. featureLearning->learnNewFeatures( filename );
  187. //and update the codebook pointer within our novelty detection algorithm
  188. //TODO this should be done, but currently we do not care about
  189. // novDetector->setCodebook( featureLearning->getCurrentCodebook() );
  190. //**********************************************
  191. //
  192. // EVALUATE HOW WELL THE CURRENT IMAGE
  193. // CAN BE EXPLAINED AFTER
  194. // COMPUTING A NEW FEATURE
  195. //
  196. // SHOULD WE REPEAT THIS UNTIL THE IMAGE
  197. // IS NOT CLASSIFIED AS "NOVEL" ANYMORE?
  198. //
  199. //**********************************************
  200. NICE::FloatImage noveltyImageAfter;
  201. noveltyImageAfter = featureLearning->evaluateCurrentCodebookByDistance( filename , false /* beforeComputingNewFeatures */ );
  202. NICE::FloatImage noveltyImageDifference ( noveltyImageAfter.width(), noveltyImageAfter.height());
  203. for ( uint y = 0 ; y < ( uint ) noveltyImageAfter.height() ; y++ )
  204. {
  205. for ( uint x = 0 ; x < ( uint ) noveltyImageAfter.width(); x++ )
  206. {
  207. noveltyImageDifference(x,y) = fabs ( noveltyImageBefore(x,y) - noveltyImageAfter(x,y) );
  208. }
  209. }
  210. std::cerr << "min diff: " << noveltyImageDifference.min() << " and max diff: " << noveltyImageDifference.max() << std::endl;
  211. NICE::ColorImage noveltyImageDifferenceRGB (noveltyImageAfter.width(), noveltyImageAfter.height() );
  212. imageToPseudoColor( noveltyImageDifference, noveltyImageDifferenceRGB );
  213. std::vector< std::string > list2;
  214. StringTools::split ( filename, '/', list2 );
  215. if ( showResults )
  216. {
  217. showImage( noveltyImageDifferenceRGB, "Difference of novelty images" );
  218. }
  219. else
  220. {
  221. std::string destination ( resultdir + NICE::intToString(imageCnt) + "_" + list2.back() + "_02_initialLoopClusterAssignments.ppm");
  222. imgClusterAssignmentsRGB.writePPM( destination );
  223. std::string destinationNoveltyDifference ( resultdir + NICE::intToString(imageCnt) + "_" + list2.back() + "_03_noveltyDifferences.ppm");
  224. noveltyImageDifferenceRGB.writePPM( destinationNoveltyDifference );
  225. }
  226. NICE::ImageT< int > imgClusterAssignmentsBinary;
  227. imgClusterAssignmentsBinary = featureLearning->evaluateCurrentCodebookByAssignments(filename , true, true /* _binaryShowLatestPrototype*/ );
  228. NICE::ImageT< int > imgClusterAssignments;
  229. imgClusterAssignments = featureLearning->evaluateCurrentCodebookByAssignments(filename , true, false /* _binaryShowLatestPrototype*/ );
  230. NICE::ColorImage imgClusterAssignmentsBinaryRGB (imgClusterAssignmentsBinary.width(), imgClusterAssignmentsBinary.height() );
  231. imageToPseudoColor( imgClusterAssignmentsBinary, imgClusterAssignmentsBinaryRGB );
  232. NICE::ColorImage imgClusterAssignmentsRGB (imgClusterAssignments.width(), imgClusterAssignments.height() );
  233. imageToPseudoColor( imgClusterAssignments, imgClusterAssignmentsRGB );
  234. if ( showResults )
  235. {
  236. showImage(imgClusterAssignmentsBinaryRGB, "cluster Assignments Binary (latest prototype)" ) ;
  237. showImage(imgClusterAssignmentsRGB, "cluster Assignments" ) ;
  238. }
  239. else
  240. {
  241. std::string destination ( resultdir + NICE::intToString(imageCnt) + "_" + list2.back() + "_5_clusterAssignments.ppm");
  242. std::string destinationBinary ( resultdir + NICE::intToString(imageCnt) + "_" + list2.back() + "_6_clusterAssignmentsBinary.ppm");
  243. imgClusterAssignmentsRGB.writePPM( destination );
  244. imgClusterAssignmentsBinaryRGB.writePPM( destinationBinary );
  245. }
  246. //check, whether the image will still be seen as novel or not
  247. // i.e., are we able to "explain" the image with the lately computed codebook entry?
  248. b_isImageNovel = novDetector->evaluateNoveltyOfImage( noveltyImageAfter ) ;
  249. if ( ! b_isImageNovel )
  250. {
  251. std::cerr << " --- NOT NOVEL ANYMORE --- " << std::endl << std::endl;
  252. }
  253. else
  254. {
  255. std::cerr << " --- STILL NOVEL --- " << std::endl;
  256. noveltyImageBefore = noveltyImageAfter;
  257. }
  258. //artifial break for the moment
  259. break;
  260. }
  261. imageCnt++;
  262. } //Loop over all test images
  263. //don't waste memory
  264. std::cerr << "don't waste memory - cleaning up" << std::endl;
  265. // if (trainFiles != NULL)
  266. // delete trainFiles;
  267. if (featureLearning != NULL)
  268. delete featureLearning;
  269. if (novDetector != NULL)
  270. delete novDetector;
  271. if (conf != NULL)
  272. delete conf;
  273. return 0;
  274. }