NoveltyDetectorCodebookLevel.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. #include "NoveltyDetectorCodebookLevel.h"
  2. //STL
  3. #include <iostream>
  4. //core
  5. #include <core/vector/VectorT.h>
  6. //vislearning
  7. #include <vislearning/features/localfeatures/LFonHSG.h>
  8. #include <vislearning/features/localfeatures/LFColorSande.h>
  9. #include <vislearning/features/localfeatures/LocalFeatureColorWeijer.h>
  10. #include <vislearning/features/localfeatures/LFReadCache.h>
  11. #include <vislearning/features/localfeatures/LFWriteCache.h>
  12. using namespace std;
  13. using namespace NICE;
  14. using namespace OBJREC;
  15. //**********************************************
  16. //
  17. // PROTECTED METHODS
  18. //
  19. //**********************************************
  20. void NoveltyDetectorCodebookLevel::setFeatureExtractor( const bool & _setForTraining )
  21. {
  22. //be careful with previously allocated memory
  23. if (this->featureExtractor != NULL)
  24. delete featureExtractor;
  25. //feature stuff
  26. // which OpponentSIFT implementation to use {NICE, VANDESANDE}
  27. std::string opSiftImpl;
  28. opSiftImpl = this->conf->gS ( "Descriptor", "implementation", "VANDESANDE" );
  29. // read features?
  30. bool readfeat;
  31. readfeat = this->conf->gB ( "Descriptor", "read", true );
  32. // write features?
  33. bool writefeat;
  34. writefeat = this->conf->gB ( "Descriptor", "write", true );
  35. // Welche Opponentsift Implementierung soll genutzt werden ?
  36. LocalFeatureRepresentation *cSIFT = NULL;
  37. LocalFeatureRepresentation *writeFeats = NULL;
  38. LocalFeatureRepresentation *readFeats = NULL;
  39. this->featureExtractor = NULL;
  40. if ( opSiftImpl == "NICE" )
  41. {
  42. if ( _setForTraining )
  43. cSIFT = new OBJREC::LFonHSG ( this->conf, "HSGtrain" );
  44. else
  45. cSIFT = new OBJREC::LFonHSG ( this->conf, "HSGtest" );
  46. }
  47. else if ( opSiftImpl == "VANDESANDE" )
  48. {
  49. if ( _setForTraining )
  50. cSIFT = new OBJREC::LFColorSande ( this->conf, "LFColorSandeTrain" );
  51. else
  52. cSIFT = new OBJREC::LFColorSande ( this->conf, "LFColorSandeTest" );
  53. }
  54. else
  55. {
  56. fthrow ( Exception, "feattype: %s not yet supported" << opSiftImpl );
  57. }
  58. this->featureExtractor = cSIFT;
  59. if ( writefeat )
  60. {
  61. // write the features to a file, if there isn't any to read
  62. writeFeats = new LFWriteCache ( this->conf, cSIFT );
  63. this->featureExtractor = writeFeats;
  64. }
  65. if ( readfeat )
  66. {
  67. // read the features from a file
  68. if ( writefeat )
  69. {
  70. readFeats = new LFReadCache ( this->conf, writeFeats, -1 );
  71. }
  72. else
  73. {
  74. readFeats = new LFReadCache ( this->conf, cSIFT, -1 );
  75. }
  76. this->featureExtractor = readFeats;
  77. }
  78. //only set feature stuff to NULL, deletion of the underlying object is done in the destructor
  79. if ( cSIFT != NULL )
  80. cSIFT = NULL;
  81. if ( writeFeats != NULL )
  82. writeFeats = NULL;
  83. if ( readFeats != NULL )
  84. readFeats = NULL ;
  85. }
  86. bool NoveltyDetectorCodebookLevel::loadInitialCodebook ( )
  87. {
  88. if ( b_loadInitialCodebook )
  89. {
  90. std::cerr << " INITIAL CODEBOOK ALREADY COMPUTED - RE-USE IT" << std::endl;
  91. std::cerr << " // WARNING - WE DO NOT VERIFY WHETHER THIS IS THE CORRECT CODEBOOK FOR THIS TRAINING SET!!!!" << std::endl;
  92. prototypes->clear();
  93. try
  94. {
  95. prototypes->read(cacheInitialCodebook);
  96. }
  97. catch (...)
  98. {
  99. std::cerr << "Error while loading initial codebook" << std::endl;
  100. return false;
  101. }
  102. return true;
  103. }
  104. else
  105. return false;
  106. }
  107. bool NoveltyDetectorCodebookLevel::writeInitialCodebook ( )
  108. {
  109. if ( b_saveInitialCodebook )
  110. {
  111. std::cerr << " SAVE INITIAL CODEBOOK " << std::endl;
  112. try
  113. {
  114. prototypes->write( cacheInitialCodebook );
  115. }
  116. catch (...)
  117. {
  118. std::cerr << "Error while saving initial codebook" << std::endl;
  119. return false;
  120. }
  121. return true;
  122. }
  123. else
  124. return false;
  125. }
  126. //**********************************************
  127. //
  128. // PUBLIC METHODS
  129. //
  130. //**********************************************
  131. NoveltyDetectorCodebookLevel::NoveltyDetectorCodebookLevel ( const Config *_conf,
  132. const MultiDataset *_md, const std::string & _section )
  133. : NoveltyDetector ( _conf, _section )
  134. {
  135. // define the distance function to be used
  136. std::string distFunctionString = conf->gS(section, "distFunction", "euclidian");
  137. //**********************************************
  138. //
  139. // SET UP VARIABLES AND METHODS
  140. // - FEATURE TYPE
  141. // - DISTANCE FUNCTION
  142. // - ...
  143. //
  144. //**********************************************
  145. if (distFunctionString.compare("euclidian") == 0)
  146. {
  147. distFunction = new NICE::EuclidianDistance<double>();
  148. }
  149. else
  150. {
  151. std::cerr << "Unknown vector distance selected, use euclidian instead" << std::endl;
  152. distFunction = new NICE::EuclidianDistance<double>();
  153. }
  154. //feature extraction for comparison against codebook
  155. this->featureExtractor = NULL;
  156. //feature extraction for unseen images
  157. this->setFeatureExtractor( false /* set for training */ );
  158. this->prototypes = NULL;
  159. }
  160. NoveltyDetectorCodebookLevel::~NoveltyDetectorCodebookLevel()
  161. {
  162. // clean-up
  163. if ( distFunction != NULL )
  164. delete distFunction;
  165. if ( featureExtractor != NULL )
  166. delete featureExtractor;
  167. if ( ( ! externalCodebook ) && (prototypes != NULL) )
  168. delete prototypes;
  169. }
  170. void NoveltyDetectorCodebookLevel::setCodebook( NICE::VVector * _prototypes)
  171. {
  172. std::cerr << "aim to set codebook" << std::endl;
  173. // if we had no codebook so far, we simply store the pointer and remember, that we use the external one
  174. // such that we do not have to delete it lateron
  175. if ( this->prototypes == NULL)
  176. {
  177. std::cerr << "previous codebook is null -- set it as external codebook" << std::endl;
  178. this->prototypes = _prototypes;
  179. this->externalCodebook = true;
  180. }
  181. else
  182. {
  183. std::cerr << "previous codebook is not null - do we want to treat the new one as external?" << std::endl;
  184. //in case of an internal codebook, we need to copy the whole thing
  185. if ( ! externalCodebook )
  186. {
  187. *(this->prototypes) = *_prototypes;
  188. std::cerr << "new codebook treated as internal codebook" << std::endl;
  189. }
  190. //if we use a pointer to an external codebook, we simply renew the pointer
  191. else
  192. {
  193. this->prototypes = _prototypes;
  194. std::cerr << "new codebook treated as external codebook " << std::endl;
  195. }
  196. }
  197. }