Codebook.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /**
  2. * @file Codebook.cpp
  3. * @brief feature codebook
  4. * @author Erik Rodner, Alexander Freytag
  5. * @date 05-06-2013 (dd-mm-yyyy ) (original: 02/15/2008)
  6. */
  7. #include <iostream>
  8. #include "vislearning/features/simplefeatures/Codebook.h"
  9. using namespace OBJREC;
  10. using namespace std;
  11. using namespace NICE;
  12. Codebook::Codebook ( )
  13. {
  14. this->i_noOfNearestClustersToConsidere = 1;
  15. }
  16. Codebook::Codebook ( const int & _noOfNearestClustersToConsidere )
  17. {
  18. this->i_noOfNearestClustersToConsidere = _noOfNearestClustersToConsidere;
  19. }
  20. Codebook::Codebook( NICE::Config * _conf, const std::string & _section)
  21. {
  22. this->p_conf = _conf;
  23. this->s_section = _section;
  24. this->i_noOfNearestClustersToConsidere = this->p_conf->gI( _section, "noOfNearestClustersToConsidere", 1);
  25. }
  26. Codebook::~Codebook ( )
  27. {
  28. //NOTE the config does not need to be deleted, it is just a pointer to an external data structure
  29. }
  30. void Codebook::voteVQ (const NICE::Vector &feature, NICE::Vector &histogram, int &codebookEntry, double &weight, double &distance) const
  31. {
  32. this->voteVQ ( feature, codebookEntry, weight, distance );
  33. //VQ, but we directly increase the corresponding entry without setting histogram to zero before (useful if we work on multiple images)
  34. histogram[codebookEntry] += weight;
  35. }
  36. void Codebook::voteVQ ( const NICE::Vector & feature, NICE::SparseVector & histogram, int &codebookEntry, double &weight, double &distance ) const
  37. {
  38. this->voteVQ ( feature, codebookEntry, weight, distance );
  39. //is this cluster already non-zero in the histogram?
  40. NICE::SparseVector::iterator entryIt = histogram.find( codebookEntry ) ;
  41. if ( entryIt == histogram.end() )
  42. {
  43. //entry does not exist
  44. histogram.insert ( histogram.begin(), pair<int, double> ( codebookEntry, weight ) );
  45. }
  46. else
  47. {
  48. //entry already exists, so we increase the weight
  49. entryIt->second += weight;
  50. }
  51. }
  52. bool Codebook::allowsMultipleVoting () const
  53. {
  54. if ( this->i_noOfNearestClustersToConsidere > 1 )
  55. return true;
  56. else
  57. return false;
  58. }
  59. void Codebook::reinit ( int numCodebookEntries )
  60. {
  61. thresholds.resize ( numCodebookEntries );
  62. thresholds.set(0.0);
  63. informativeMeasure.resize ( numCodebookEntries );
  64. informativeMeasure.set(0.0);
  65. classnos.resize ( numCodebookEntries );
  66. classnos.resize(0);
  67. }
  68. void Codebook::clear ()
  69. {
  70. thresholds.clear();
  71. informativeMeasure.clear();
  72. classnos.clear();
  73. }
  74. void Codebook::restore ( istream & is, int format )
  75. {
  76. if (is.good())
  77. {
  78. std::string tmp;
  79. is >> tmp; //class name
  80. if ( ! this->isStartTag( tmp, "Codebook" ) )
  81. {
  82. std::cerr << " WARNING - attempt to restore Codebook, but start flag " << tmp << " does not match! Aborting... " << std::endl;
  83. throw;
  84. }
  85. bool b_endOfBlock = false;
  86. while ( !b_endOfBlock )
  87. {
  88. is >> tmp; // start of block
  89. if ( this->isEndTag( tmp, "Codebook" ) )
  90. {
  91. b_endOfBlock = true;
  92. continue;
  93. }
  94. tmp = this->removeStartTag ( tmp );
  95. if ( tmp.compare("thresholds") == 0 )
  96. {
  97. is >> thresholds;
  98. is >> tmp; // end of block
  99. tmp = this->removeEndTag ( tmp );
  100. }
  101. else if ( tmp.compare("informativeMeasure") == 0 )
  102. {
  103. is >> informativeMeasure;
  104. is >> tmp; // end of block
  105. tmp = this->removeEndTag ( tmp );
  106. }
  107. else if ( tmp.compare("classnos") == 0 )
  108. {
  109. is >> classnos;
  110. is >> tmp; // end of block
  111. tmp = this->removeEndTag ( tmp );
  112. }
  113. }
  114. }
  115. // is >> classnos;
  116. //TODO use a flag for compatibility with old systems?
  117. /* try
  118. {
  119. is >> i_noOfNearestClustersToConsidere;
  120. is >> s_section;
  121. this->p_conf->restore( is, format );
  122. }
  123. catch( ... )
  124. {
  125. std::cerr << "something went wrong while Loading the codebook - use default values instead" << std::endl;
  126. //TODO use suitable default values
  127. } */
  128. }
  129. void Codebook::store ( ostream & os, int format ) const
  130. {
  131. if (os.good())
  132. {
  133. // show starting point
  134. os << this->createStartTag( "Codebook" ) << std::endl;
  135. os << this->createStartTag( "thresholds" ) << std::endl;
  136. os << this->thresholds << endl;
  137. os << this->createEndTag( "thresholds" ) << std::endl;
  138. os << this->createStartTag( "informativeMeasure" ) << std::endl;
  139. os << this->informativeMeasure << endl;
  140. os << this->createEndTag( "informativeMeasure" ) << std::endl;
  141. os << this->createStartTag( "classnos" ) << std::endl;
  142. os << this->classnos << endl;
  143. os << this->createEndTag( "classnos" ) << std::endl;
  144. // done
  145. os << this->createEndTag( "Codebook" ) << std::endl;
  146. }
  147. // os << this->i_noOfNearestClustersToConsidere << endl;
  148. // os << this->s_section << endl;
  149. //
  150. // this->p_conf->store( os, format );
  151. }
  152. void Codebook::copy ( const Codebook *codebook )
  153. {
  154. this->reinit ( codebook->thresholds.size() );
  155. thresholds = codebook->thresholds;
  156. informativeMeasure = codebook->informativeMeasure;
  157. classnos = codebook->classnos;
  158. this->p_conf = codebook->p_conf;
  159. this->setNoOfNearestClustersToConsidere ( codebook->getNoOfNearestClustersToConsidere() );
  160. }
  161. void Codebook::setNoOfNearestClustersToConsidere ( const int & _noOfNearestClustersToConsidere )
  162. {
  163. this->i_noOfNearestClustersToConsidere = _noOfNearestClustersToConsidere;
  164. }
  165. int Codebook::getNoOfNearestClustersToConsidere ( ) const
  166. {
  167. return this->i_noOfNearestClustersToConsidere;
  168. }
  169. void Codebook::setHardAssignment( const bool & _hardAssignment )
  170. {
  171. this->b_hardAssignment = _hardAssignment;
  172. }
  173. bool Codebook::getHardAssignment ( ) const
  174. {
  175. return this->b_hardAssignment;
  176. }