FeatureLearningPrototypes.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  1. #include "FeatureLearningPrototypes.h"
  2. //STL
  3. #include <iostream>
  4. //core
  5. #include <core/image/FilterT.h>
  6. #include <core/image/CircleT.h>
  7. #include <core/image/Convert.h>
  8. #include <core/vector/VectorT.h>
  9. //vislearning
  10. #include <vislearning/features/localfeatures/LFonHSG.h>
  11. #include <vislearning/features/localfeatures/LFColorSande.h>
  12. #include <vislearning/features/localfeatures/LFColorWeijer.h>
  13. #include <vislearning/features/localfeatures/LFReadCache.h>
  14. #include <vislearning/features/localfeatures/LFWriteCache.h>
  15. //
  16. #include <vislearning/math/cluster/KMeans.h>
  17. #include <vislearning/math/cluster/GMM.h>
  18. using namespace std;
  19. using namespace NICE;
  20. using namespace OBJREC;
  21. //**********************************************
  22. //
  23. // PROTECTED METHODS
  24. //
  25. //**********************************************
  26. void FeatureLearningPrototypes::setClusterAlgo( const std::string & _clusterAlgoString)
  27. {
  28. //be careful with previously allocated memory
  29. if (this->clusterAlgo != NULL)
  30. delete clusterAlgo;
  31. if (_clusterAlgoString.compare("kmeans") == 0)
  32. {
  33. this->clusterAlgo = new OBJREC::KMeans(this->initialNumberOfClusters);
  34. }
  35. else if (_clusterAlgoString.compare("GMM") == 0)
  36. {
  37. this->clusterAlgo = new OBJREC::GMM(this->conf, this->initialNumberOfClusters);
  38. }
  39. else
  40. {
  41. std::cerr << "Unknown cluster algorithm selected, use k-means instead" << std::endl;
  42. this->clusterAlgo = new OBJREC::KMeans(this->initialNumberOfClusters);
  43. }
  44. }
  45. void FeatureLearningPrototypes::setFeatureExtractor( const bool & _setForTraining )
  46. {
  47. //be careful with previously allocated memory
  48. if (this->featureExtractor != NULL)
  49. delete featureExtractor;
  50. //feature stuff
  51. // which OpponentSIFT implementation to use {NICE, VANDESANDE}
  52. std::string opSiftImpl;
  53. opSiftImpl = this->conf->gS ( "Descriptor", "implementation", "VANDESANDE" );
  54. // read features?
  55. bool readfeat;
  56. readfeat = this->conf->gB ( "Descriptor", "read", true );
  57. // write features?
  58. bool writefeat;
  59. writefeat = this->conf->gB ( "Descriptor", "write", true );
  60. // Welche Opponentsift Implementierung soll genutzt werden ?
  61. LocalFeatureRepresentation *cSIFT = NULL;
  62. LocalFeatureRepresentation *writeFeats = NULL;
  63. LocalFeatureRepresentation *readFeats = NULL;
  64. this->featureExtractor = NULL;
  65. if ( opSiftImpl == "NICE" )
  66. {
  67. if ( _setForTraining )
  68. cSIFT = new OBJREC::LFonHSG ( this->conf, "HSGtrain" );
  69. else
  70. cSIFT = new OBJREC::LFonHSG ( this->conf, "HSGtest" );
  71. }
  72. else if ( opSiftImpl == "VANDESANDE" )
  73. {
  74. if ( _setForTraining )
  75. cSIFT = new OBJREC::LFColorSande ( this->conf, "LFColorSandeTrain" );
  76. else
  77. cSIFT = new OBJREC::LFColorSande ( this->conf, "LFColorSandeTest" );
  78. }
  79. else
  80. {
  81. fthrow ( Exception, "feattype: %s not yet supported" << opSiftImpl );
  82. }
  83. this->featureExtractor = cSIFT;
  84. if ( writefeat )
  85. {
  86. // write the features to a file, if there isn't any to read
  87. writeFeats = new LFWriteCache ( this->conf, cSIFT );
  88. this->featureExtractor = writeFeats;
  89. }
  90. if ( readfeat )
  91. {
  92. // read the features from a file
  93. if ( writefeat )
  94. {
  95. readFeats = new LFReadCache ( this->conf, writeFeats, -1 );
  96. }
  97. else
  98. {
  99. readFeats = new LFReadCache ( this->conf, cSIFT, -1 );
  100. }
  101. this->featureExtractor = readFeats;
  102. }
  103. //only set feature stuff to NULL, deletion of the underlying object is done in the destructor
  104. if ( cSIFT != NULL )
  105. cSIFT = NULL;
  106. if ( writeFeats != NULL )
  107. writeFeats = NULL;
  108. if ( readFeats != NULL )
  109. readFeats = NULL ;
  110. }
  111. void FeatureLearningPrototypes::extractFeaturesFromTrainingImages( const OBJREC::MultiDataset *_md, NICE::VVector & examplesTraining )
  112. {
  113. examplesTraining.clear();
  114. int numberOfTrainImage ( 0 );
  115. const LabeledSet *trainFiles = (*_md)["train"];
  116. //run over all training images
  117. LOOP_ALL_S( *trainFiles )
  118. {
  119. EACH_INFO( classno, info );
  120. std::string filename = info.img();
  121. NICE::ColorImage img( filename );
  122. if ( b_showTrainingImages )
  123. {
  124. showImage( img, "Input" );
  125. }
  126. //variables to store feature informatio
  127. NICE::VVector features;
  128. NICE::VVector cfeatures;
  129. NICE::VVector positions;
  130. //compute features
  131. Globals::setCurrentImgFN ( filename );
  132. if (featureExtractor == NULL)
  133. std::cerr << "feature Extractor is NULL" << std::endl;
  134. else
  135. featureExtractor->extractFeatures ( img, features, positions );
  136. //store feature information in larger data structure
  137. for ( NICE::VVector::iterator i = features.begin();
  138. i != features.end();
  139. i++)
  140. {
  141. //normalization :)
  142. i->normalizeL1();
  143. examplesTraining.push_back(*i);
  144. }
  145. //don't waste memory
  146. features.clear();
  147. positions.clear();
  148. numberOfTrainImage++;
  149. }//Loop over all training images
  150. }
  151. void FeatureLearningPrototypes::train ( const OBJREC::MultiDataset *_md )
  152. {
  153. bool loadSuccess = this->loadInitialCodebook();
  154. if ( !loadSuccess )
  155. {
  156. //**********************************************
  157. //
  158. // EXTRACT FEATURES FROM TRAINING IMAGES
  159. //
  160. //**********************************************
  161. std::cerr << " EXTRACT FEATURES FROM TRAINING IMAGES" << std::endl;
  162. NICE::VVector examplesTraining;
  163. this->extractFeaturesFromTrainingImages( _md, examplesTraining );
  164. //**********************************************
  165. //
  166. // CLUSTER FEATURES FROM TRAINING IMAGES
  167. //
  168. // THIS GIVES US AN INITIAL CODEBOOK
  169. //
  170. //**********************************************
  171. std::cerr << " CLUSTER FEATURES FROM TRAINING IMAGES" << std::endl;
  172. //go, go, go...
  173. prototypes.clear();
  174. std::vector< double > weights;
  175. std::vector< int > assignment;
  176. clusterAlgo->cluster ( examplesTraining, prototypes, weights, assignment);
  177. weights.clear();
  178. assignment.clear();
  179. //normalization
  180. for (NICE::VVector::iterator protoIt = prototypes.begin(); protoIt != prototypes.end(); protoIt++)
  181. {
  182. protoIt->normalizeL1();
  183. }
  184. }
  185. this->writeInitialCodebook();
  186. }
  187. bool FeatureLearningPrototypes::loadInitialCodebook ( )
  188. {
  189. if ( b_loadInitialCodebook )
  190. {
  191. std::cerr << " INITIAL CODEBOOK ALREADY COMPUTED - RE-USE IT" << std::endl;
  192. std::cerr << " // WARNING - WE DO NOT VERIFY WHETHER THIS IS THE CORRECT CODEBOOK FOR THIS TRAINING SET!!!!" << std::endl;
  193. prototypes.clear();
  194. try
  195. {
  196. prototypes.read(cacheInitialCodebook);
  197. }
  198. catch (...)
  199. {
  200. std::cerr << "Error while loading initial codebook" << std::endl;
  201. return false;
  202. }
  203. return true;
  204. }
  205. else
  206. return false;
  207. }
  208. bool FeatureLearningPrototypes::writeInitialCodebook ( )
  209. {
  210. if ( b_saveInitialCodebook )
  211. {
  212. std::cerr << " SAVE INITIAL CODEBOOK " << std::endl;
  213. try
  214. {
  215. prototypes.write( cacheInitialCodebook );
  216. }
  217. catch (...)
  218. {
  219. std::cerr << "Error while saving initial codebook" << std::endl;
  220. return false;
  221. }
  222. return true;
  223. }
  224. else
  225. return false;
  226. }
  227. void FeatureLearningPrototypes::evaluateCurrentCodebookForGivenFeatures( const NICE::VVector & _features,
  228. const NICE::VVector & _positions,
  229. NICE::FloatImage & _noveltyImageGaussFiltered,
  230. NICE::FloatImage * _noveltyImage )
  231. {
  232. bool wasNoveltyImageGiven ( true );
  233. if ( _noveltyImage == NULL )
  234. {
  235. _noveltyImage = new FloatImage ( _noveltyImageGaussFiltered.width(), _noveltyImageGaussFiltered.height() );
  236. wasNoveltyImageGiven = false;
  237. }
  238. _noveltyImageGaussFiltered.set( 0.0 );
  239. _noveltyImage->set( 0.0 );
  240. NICE::VVector::const_iterator posIt = _positions.begin();
  241. for ( NICE::VVector::const_iterator i = _features.begin();
  242. i != _features.end();
  243. i++, posIt++)
  244. {
  245. //loop over codebook representatives
  246. double minDist ( std::numeric_limits<double>::max() );
  247. for (NICE::VVector::const_iterator it = prototypes.begin(); it != prototypes.end(); it++)
  248. {
  249. //compute distance
  250. double tmpDist ( this->distFunction->calculate(*i,*it) );
  251. if (tmpDist < minDist)
  252. minDist = tmpDist;
  253. }
  254. //take minimum distance and store in in a float image
  255. (*_noveltyImage) ( (*posIt)[0], (*posIt)[1] ) = minDist;
  256. }
  257. //gauss-filtering for nicer visualization
  258. float sigma ( 3.0 );
  259. FilterT<float, float, float> filter;
  260. filter.filterGaussSigmaApproximate ( *_noveltyImage, sigma, & _noveltyImageGaussFiltered );
  261. if ( ! wasNoveltyImageGiven )
  262. delete _noveltyImage;
  263. }
  264. //**********************************************
  265. //
  266. // PUBLIC METHODS
  267. //
  268. //**********************************************
  269. FeatureLearningPrototypes::FeatureLearningPrototypes ( const Config *_conf,
  270. const MultiDataset *_md, const std::string & _section )
  271. : FeatureLearningGeneric ( _conf, _section )
  272. {
  273. // define the initial number of clusters our codebook shall contain
  274. initialNumberOfClusters = conf->gI(section, "initialNumberOfClusters", 10);
  275. // define the clustering algorithm to be used
  276. std::string clusterAlgoString = conf->gS(section, "clusterAlgo", "kmeans");
  277. // define the distance function to be used
  278. std::string distFunctionString = conf->gS(section, "distFunction", "euclidian");
  279. //**********************************************
  280. //
  281. // SET UP VARIABLES AND METHODS
  282. // - FEATURE TYPE
  283. // - CLUSTERING ALGO
  284. // - DISTANCE FUNCTION
  285. // - ...
  286. //
  287. //**********************************************
  288. std::cerr << " SET UP VARIABLES AND METHODS " << std::endl;
  289. //feature extraction for initial codebook
  290. this->featureExtractor = NULL;
  291. this->setFeatureExtractor( true /* set for training */ );
  292. //clustering algorithm
  293. this->clusterAlgo = NULL;
  294. this->setClusterAlgo( clusterAlgoString );
  295. if (distFunctionString.compare("euclidian") == 0)
  296. {
  297. distFunction = new NICE::EuclidianDistance<double>();
  298. }
  299. else
  300. {
  301. std::cerr << "Unknown vector distance selected, use euclidian instead" << std::endl;
  302. distFunction = new NICE::EuclidianDistance<double>();
  303. }
  304. //run the training to initially compute a codebook and stuff like that
  305. this->train( _md );
  306. //so far, we have not seen any new image
  307. this->newImageCounter = 0;
  308. //TODO stupid
  309. this->maxValForVisualization = conf->gD( section, "stupidMaxValForVisualization", 0.005 ) ;
  310. //feature extraction for unseen images
  311. this->setFeatureExtractor( false /* set for training */ );
  312. }
  313. FeatureLearningPrototypes::~FeatureLearningPrototypes()
  314. {
  315. // clean-up
  316. if ( clusterAlgo != NULL )
  317. delete clusterAlgo;
  318. if ( distFunction != NULL )
  319. delete distFunction;
  320. if ( featureExtractor != NULL )
  321. delete featureExtractor;
  322. }
  323. NICE::FloatImage FeatureLearningPrototypes::evaluateCurrentCodebookByDistance ( const std::string & _filename , const bool & beforeComputingNewFeatures )
  324. {
  325. std::cerr << " VISUALIZATION ----- maxValForVisualization: " << maxValForVisualization << std::endl;
  326. NICE::ColorImage img( _filename );
  327. if ( b_showTrainingImages )
  328. {
  329. showImage( img, "Input" );
  330. }
  331. int xsize ( img.width() );
  332. int ysize ( img.height() );
  333. //variables to store feature information
  334. NICE::VVector features;
  335. NICE::VVector cfeatures;
  336. NICE::VVector positions;
  337. //compute features
  338. Globals::setCurrentImgFN ( _filename );
  339. featureExtractor->extractFeatures ( img, features, positions );
  340. //normalization
  341. for ( NICE::VVector::iterator i = features.begin();
  342. i != features.end();
  343. i++)
  344. {
  345. //normalization :)
  346. i->normalizeL1();
  347. }
  348. FloatImage noveltyImage ( xsize, ysize );
  349. FloatImage noveltyImageGaussFiltered ( xsize, ysize );
  350. this->evaluateCurrentCodebookForGivenFeatures( features, positions, noveltyImageGaussFiltered, &noveltyImage );
  351. double maxDist ( noveltyImage.max() );
  352. double maxFiltered ( noveltyImageGaussFiltered.max() );
  353. std::cerr << "maximum distance of Training images: " << maxDist << std::endl;
  354. std::cerr << "maximum distance of Training images after filtering: " << maxFiltered << std::endl;
  355. if ( beforeComputingNewFeatures )
  356. this->oldMaxDist = maxFiltered;
  357. //convert float to RGB
  358. NICE::ColorImage noveltyImageRGB ( xsize, ysize );
  359. // ICETools::convertToRGB ( noveltyImageGaussFiltered, noveltyImageRGB );
  360. if ( beforeComputingNewFeatures )
  361. {
  362. imageToPseudoColorWithRangeSpecification( noveltyImageGaussFiltered, noveltyImageRGB, 0 /* min */, maxValForVisualization /* maxFiltered*/ /* max */ );
  363. std::cerr << "set max value to: " << noveltyImageGaussFiltered.max() << std::endl;
  364. }
  365. else
  366. {
  367. imageToPseudoColorWithRangeSpecification( noveltyImageGaussFiltered, noveltyImageRGB, 0 /* min */, maxValForVisualization /*this->oldMaxDist*/ /* max */ );
  368. std::cerr << "set max value to: " << this->oldMaxDist << std::endl;
  369. }
  370. if ( b_showResults )
  371. showImage(noveltyImageRGB, "Novelty Image");
  372. else
  373. {
  374. std::vector< std::string > list2;
  375. StringTools::split ( _filename, '/', list2 );
  376. std::string destination ( s_resultdir + NICE::intToString(this->newImageCounter -1 ) + "_" + list2.back() + "_3_updatedNoveltyMap.ppm");
  377. if ( beforeComputingNewFeatures )
  378. destination = s_resultdir + NICE::intToString(this->newImageCounter) + "_" + list2.back() + "_0_initialNoveltyMap.ppm";
  379. noveltyImageRGB.writePPM( destination );
  380. }
  381. // now look where the closest features for the current cluster indices are
  382. int tmpProtCnt ( 0 );
  383. for (NICE::VVector::const_iterator protIt = prototypes.begin(); protIt != prototypes.end(); protIt++, tmpProtCnt++)
  384. {
  385. double distToNewCluster ( std::numeric_limits<double>::max() );
  386. int indexOfMostSimFeat( 0 );
  387. double tmpDist;
  388. int tmpCnt ( 0 );
  389. for ( NICE::VVector::iterator i = features.begin();
  390. i != features.end();
  391. i++, tmpCnt++)
  392. {
  393. tmpDist = this->distFunction->calculate( *i, *protIt );
  394. if ( tmpDist < distToNewCluster )
  395. {
  396. distToNewCluster = tmpDist;
  397. indexOfMostSimFeat = tmpCnt;
  398. }
  399. }
  400. int posX ( ( positions[indexOfMostSimFeat] ) [0] );
  401. int posY ( ( positions[indexOfMostSimFeat] ) [1] );
  402. NICE::Circle circ ( Coord( posX, posY), 2*(tmpProtCnt+1) /* radius*/, Color(200,0,255 ) );
  403. img.draw(circ);
  404. }
  405. if ( b_showResults )
  406. showImage(img, "Current image and most similar features for current cluster");
  407. else
  408. {
  409. std::vector< std::string > list2;
  410. StringTools::split ( _filename, '/', list2 );
  411. std::string destination ( s_resultdir + NICE::intToString(this->newImageCounter-1) + "_" + list2.back() + "_3_updatedCurrentCluster.ppm");
  412. if ( beforeComputingNewFeatures )
  413. destination = s_resultdir + NICE::intToString(this->newImageCounter) + "_" + list2.back() + "_0_initialCurrentCluster.ppm";
  414. img.writePPM( destination );
  415. }
  416. return noveltyImageGaussFiltered;
  417. }
  418. NICE::ImageT< int > FeatureLearningPrototypes::evaluateCurrentCodebookByAssignments(const std::string& _filename, const bool& beforeComputingNewFeatures, const bool & _binaryShowLatestPrototype)
  419. {
  420. std::cerr << "evaluateCurrentCodebookByAssignments" << std::endl;
  421. NICE::ColorImage img( _filename );
  422. int xsize ( img.width() );
  423. int ysize ( img.height() );
  424. //variables to store feature information
  425. NICE::VVector features;
  426. NICE::VVector cfeatures;
  427. NICE::VVector positions;
  428. //compute features
  429. Globals::setCurrentImgFN ( _filename );
  430. featureExtractor->extractFeatures ( img, features, positions );
  431. //normalization
  432. for ( NICE::VVector::iterator i = features.begin();
  433. i != features.end();
  434. i++)
  435. {
  436. //normalization :)
  437. i->normalizeL1();
  438. }
  439. std::cerr << "normalization done - now look for nearest clusters for every extracted feature" << std::endl;
  440. NICE::ImageT< int > clusterImage ( xsize, ysize );
  441. clusterImage.set ( 0 );
  442. NICE::VVector::const_iterator posIt = positions.begin();
  443. for ( NICE::VVector::const_iterator i = features.begin();
  444. i != features.end();
  445. i++, posIt++)
  446. {
  447. //loop over codebook representatives
  448. double minDist ( std::numeric_limits<double>::max() );
  449. int indexOfNearestCluster ( 0 );
  450. int clusterCounter ( 0 );
  451. for (NICE::VVector::const_iterator it = this->prototypes.begin(); it != this->prototypes.end(); it++, clusterCounter++)
  452. {
  453. //compute distance
  454. double tmpDist ( this->distFunction->calculate(*i,*it) );
  455. if (tmpDist < minDist)
  456. {
  457. minDist = tmpDist;
  458. indexOfNearestCluster = clusterCounter;
  459. }
  460. }
  461. //take minimum distance and store in in a float image
  462. //TODO hard coded!!!
  463. int noProtoTypes ( this->prototypes.size() -1 );
  464. for ( int tmpY = (*posIt)[1] - 1; tmpY < (*posIt)[1] + 1; tmpY++)
  465. {
  466. for ( int tmpX = (*posIt)[0] - 1; tmpX < (*posIt)[0] + 1; tmpX++)
  467. {
  468. if ( _binaryShowLatestPrototype )
  469. {
  470. //just a binary image - 1 if newest prototype is nearest - 0 if not
  471. if ( indexOfNearestCluster == noProtoTypes)
  472. clusterImage ( tmpX, tmpY ) = 1;
  473. else
  474. clusterImage ( tmpX, tmpY ) = 0;
  475. }
  476. else
  477. //as many different values as current prototypes available
  478. clusterImage ( tmpX, tmpY ) = indexOfNearestCluster;
  479. }
  480. }
  481. // clusterImage ( (*posIt)[0], (*posIt)[1] ) = indexOfNearestCluster;
  482. }
  483. //show how many clusters we have
  484. if ( !_binaryShowLatestPrototype )
  485. {
  486. int tmpCnt ( 0 );
  487. for (NICE::VVector::const_iterator protoIt = prototypes.begin(); protoIt != prototypes.end(); protoIt++, tmpCnt++)
  488. {
  489. for ( int tmpY = 1 + 2 - 2; tmpY < (2 + 2); tmpY++)
  490. {
  491. for ( int tmpX = 1 + 5*tmpCnt - 2; tmpX < (1 + 5*tmpCnt + 2); tmpX++)
  492. {
  493. //Take care, this might go "over" the image
  494. clusterImage ( tmpX, tmpY ) = (Ipp8u) tmpCnt;
  495. }
  496. }
  497. }
  498. }
  499. std::cerr << " evaluateCurrentCodebookByAssignments done" << std::endl;
  500. return clusterImage;
  501. }