BoWFeatureConverter.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /**
  2. * @file BoWFeatureConverter.cpp
  3. * @brief Convert a set of features into a Bag of visual Words histogram (either by Vector Quantization, or Hard / Soft Assignment)
  4. * @author Alexander Freytag
  5. * @date 11-06-2013 (dd-mm-yyyy)
  6. */
  7. #include <iostream>
  8. #include <fstream>
  9. #include "vislearning/features/simplefeatures/BoWFeatureConverter.h"
  10. using namespace OBJREC;
  11. using namespace std;
  12. using namespace NICE;
  13. BoWFeatureConverter::BoWFeatureConverter(
  14. const Config *conf,
  15. const Codebook *_codebook, const std::string _section )
  16. : codebook(_codebook)
  17. {
  18. this->s_section = _section;
  19. this->p_conf = conf;
  20. std::string normalizationMethod = conf->gS( _section, "normalizationMethod", "sum" );
  21. if ( normalizationMethod == "sum" )
  22. this->n_normalizationMethod = NORMALIZE_SUM;
  23. else if ( normalizationMethod == "binzero" )
  24. this->n_normalizationMethod = NORMALIZE_BINZERO;
  25. else if ( normalizationMethod == "raw" )
  26. this->n_normalizationMethod = NORMALIZE_RAW;
  27. else if ( normalizationMethod == "thresh" )
  28. this->n_normalizationMethod = NORMALIZE_THRESH;
  29. else
  30. {
  31. //default: L1 normalization
  32. this->n_normalizationMethod = NORMALIZE_SUM;
  33. }
  34. std::string n_quantizationMethod = conf->gS( _section, "n_quantizationMethod", "VQ" );
  35. if ( normalizationMethod == "VQ" )
  36. this->n_quantizationMethod = VECTOR_QUANTIZATION;
  37. else if ( normalizationMethod == "VA" )
  38. this->n_quantizationMethod = VECTOR_ASSIGNMENT;
  39. else
  40. {
  41. //default: standard vector quantization
  42. this->n_quantizationMethod = VECTOR_QUANTIZATION;
  43. }
  44. }
  45. BoWFeatureConverter::~BoWFeatureConverter()
  46. {
  47. }
  48. void BoWFeatureConverter::calcHistogram ( const NICE::VVector & features,
  49. NICE::Vector & histogram , const bool & b_resetHistogram )
  50. {
  51. if ( b_resetHistogram || (histogram.size() != this->codebook->getCodebookSize() ) )
  52. {
  53. histogram.resize( this->codebook->getCodebookSize() );
  54. histogram.set(0);
  55. }
  56. int cluster_index = 0;
  57. double weight = 0;
  58. double distance = 0.0;
  59. if ( this->n_quantizationMethod == VECTOR_QUANTIZATION )
  60. {
  61. for ( NICE::VVector::const_iterator featIt = features.begin();
  62. featIt != features.end();
  63. featIt++ )
  64. {
  65. const NICE::Vector & x = *featIt;
  66. this->codebook->voteVQ ( x, cluster_index, weight, distance );
  67. histogram[ cluster_index ] ++;
  68. }
  69. }
  70. else // VECTOR_ASSIGNMENT (hard or soft can be set directly in the codebook-object)
  71. {
  72. NICE::Vector assignment (this->codebook->getCodebookSize() );;
  73. for ( NICE::VVector::const_iterator featIt = features.begin();
  74. featIt != features.end();
  75. featIt++ )
  76. {
  77. assignment.set(0);
  78. const NICE::Vector & x = *featIt;
  79. this->codebook->voteVA ( x, assignment );
  80. histogram += assignment;
  81. }
  82. }
  83. }
  84. void BoWFeatureConverter::normalizeHistogram ( NICE::Vector & histogram )
  85. {
  86. if ( n_normalizationMethod == NORMALIZE_RAW ) {
  87. // do nothing
  88. } else if ( n_normalizationMethod == NORMALIZE_BINZERO ) {
  89. for ( size_t i = 0 ; i < histogram.size() ; i++ )
  90. if ( histogram[i] > 0 ) histogram[i] = 1.0;
  91. } else if ( n_normalizationMethod == NORMALIZE_SUM ) {
  92. double sum = 0.0;
  93. for ( size_t i = 0 ; i < histogram.size() ; i++ )
  94. {
  95. assert ( histogram[i] >= 0.0 );
  96. sum += histogram[i];
  97. }
  98. if ( sum < 1e-5 ) {
  99. fprintf (stderr, "BoWFeatureConverter::normalizeHistogram: WARNING histogram is zero !!\n");
  100. return;
  101. }
  102. for ( size_t i = 0 ; i < histogram.size() ; i++ )
  103. histogram[i] /= sum;
  104. } else if ( n_normalizationMethod == NORMALIZE_THRESH ) {
  105. const NICE::Vector & thresholds = codebook->getThresholds();
  106. if ( thresholds.size() <= 0 ) {
  107. fprintf (stderr, "BoWFeatureConverter:: This is maybe an OLD codebook ! \n");
  108. exit(-1);
  109. }
  110. for ( size_t i = 0 ; i < histogram.size() ; i++ )
  111. histogram[i] = (histogram[i] > thresholds[i]) ? 1.0 : 0.0;
  112. }
  113. }
  114. int BoWFeatureConverter::getNormalizationMethod () const
  115. {
  116. return n_normalizationMethod;
  117. }
  118. void BoWFeatureConverter::setNormalizationMethod ( int normalizationMethod )
  119. {
  120. n_normalizationMethod = normalizationMethod;
  121. }