Selaa lähdekoodia

added Converter from VVector to BoW-Histogram

Alexander Freytag 12 vuotta sitten
vanhempi
commit
dd968b8f9a

+ 144 - 0
features/simplefeatures/BoWFeatureConverter.cpp

@@ -0,0 +1,144 @@
+/** 
+* @file BoWFeatureConverter.cpp
+* @brief Convert a set of features into a Bag of visual Words histogram (either by Vector Quantization, or Hard / Soft Assignment)
+* @author Alexander Freytag
+* @date 11-06-2013 (dd-mm-yyyy)
+*/
+#include <iostream>
+#include <fstream>
+
+#include "vislearning/features/simplefeatures/BoWFeatureConverter.h"
+
+using namespace OBJREC;
+
+using namespace std;
+using namespace NICE;
+
+
+
+BoWFeatureConverter::BoWFeatureConverter( 
+    const Config *conf, 
+    const Codebook *_codebook, const std::string _section )
+    : codebook(_codebook)
+{
+  this->s_section = _section;
+  this->p_conf = conf;
+  
+  std::string normalizationMethod = conf->gS( _section, "normalizationMethod", "sum" );
+  
+  if ( normalizationMethod == "sum" )
+    this->n_normalizationMethod = NORMALIZE_SUM;
+  else if ( normalizationMethod == "binzero" )
+    this->n_normalizationMethod = NORMALIZE_BINZERO;
+  else if ( normalizationMethod == "raw" )
+    this->n_normalizationMethod = NORMALIZE_RAW;
+  else if ( normalizationMethod == "thresh" )
+    this->n_normalizationMethod = NORMALIZE_THRESH;
+  else
+  {
+    //default: L1 normalization
+    this->n_normalizationMethod = NORMALIZE_SUM;    
+  }
+  
+  std::string n_quantizationMethod = conf->gS( _section, "n_quantizationMethod", "VQ" );
+  
+  if ( normalizationMethod == "VQ" )
+    this->n_quantizationMethod = VECTOR_QUANTIZATION;
+  else if ( normalizationMethod == "VA" )
+    this->n_quantizationMethod = VECTOR_ASSIGNMENT;
+  else
+  {
+    //default: standard vector quantization
+    this->n_quantizationMethod = VECTOR_QUANTIZATION;    
+  }  
+}
+
+BoWFeatureConverter::~BoWFeatureConverter()
+{
+}
+
+void BoWFeatureConverter::calcHistogram ( const NICE::VVector & features,
+          NICE::Vector & histogram , const bool & b_resetHistogram )
+{
+  if ( b_resetHistogram )
+  {
+    histogram.resize( this->codebook->getCodebookSize() );
+    histogram.set(0);
+    
+    fprintf (stderr, "BoWFeatureConverter: number of features = %d, codebook size = %d\n", (int)features.size(), (int)codebook->getCodebookSize());    
+  }
+
+  int cluster_index = 0;
+  double weight = 0;
+  double distance = 0.0;
+  if ( this->n_quantizationMethod == VECTOR_QUANTIZATION )
+  {
+    for ( NICE::VVector::const_iterator featIt  = features.begin();
+            featIt != features.end();
+            featIt++ )
+    {
+      const NICE::Vector & x = *featIt;
+      this->codebook->vote ( x, histogram, cluster_index, weight, distance );
+      histogram[ cluster_index ] ++;
+    }    
+  }
+  else // VECTOR_ASSIGNMENT (hard or soft can be set directly in the codebook-object)
+  {
+    
+    NICE::Vector assignment (this->codebook->getCodebookSize() );;
+    for ( NICE::VVector::const_iterator featIt  = features.begin();
+            featIt != features.end();
+            featIt++ )
+    { 
+      assignment.set(0);
+      const NICE::Vector & x = *featIt;
+      this->codebook->voteVA ( x, assignment );
+      histogram += assignment;
+    }      
+  }
+
+}
+
+void BoWFeatureConverter::normalizeHistogram ( NICE::Vector & histogram )
+{
+    if ( n_normalizationMethod == NORMALIZE_RAW ) {
+    // do nothing
+    } else if ( n_normalizationMethod == NORMALIZE_BINZERO ) {
+    for ( size_t i = 0 ; i < histogram.size() ; i++ )
+      if ( histogram[i] > 0 ) histogram[i] = 1.0; 
+    } else if ( n_normalizationMethod == NORMALIZE_SUM  ) {
+    double sum = 0.0;
+    for ( size_t i = 0 ; i < histogram.size() ; i++ )
+    {
+      assert ( histogram[i] >= 0.0 );
+      sum += histogram[i];
+    }
+
+    if ( sum < 1e-5 ) {
+      fprintf (stderr, "BoWFeatureConverter::normalizeHistogram: WARNING histogram is zero !!\n");
+      return;
+    }
+
+    for ( size_t i = 0 ; i < histogram.size() ; i++ )
+      histogram[i] /= sum;
+    } else if ( n_normalizationMethod == NORMALIZE_THRESH ) {
+    const NICE::Vector & thresholds = codebook->getThresholds();
+
+    if ( thresholds.size() <= 0 ) {
+      fprintf (stderr, "BoWFeatureConverter:: This is maybe an OLD codebook ! \n");
+      exit(-1);
+    }
+    for ( size_t i = 0 ; i < histogram.size() ; i++ )
+      histogram[i] = (histogram[i] > thresholds[i]) ? 1.0 : 0.0; 
+    }
+}
+
+int BoWFeatureConverter::getNormalizationMethod () const
+{
+    return n_normalizationMethod;
+}
+
+void BoWFeatureConverter::setNormalizationMethod ( int normalizationMethod )
+{
+    n_normalizationMethod = normalizationMethod;
+}

+ 101 - 0
features/simplefeatures/BoWFeatureConverter.h

@@ -0,0 +1,101 @@
+/** 
+* @file BoWFeatureConverter.h
+* @brief Convert a set of features into a Bag of visual Words histogram (either by Vector Quantization, or Hard / Soft Assignment)
+* @author Alexander Freytag
+* @date 11-06-2013 (dd-mm-yyyy)
+*/
+#ifndef BOWFEATURECONVERTERINCLUDE
+#define BOWFEATURECONVERTERINCLUDE
+
+#include "core/vector/VectorT.h"
+#include "core/vector/VVector.h"
+#include "core/vector/MatrixT.h"
+
+#include "core/basics/Config.h"
+
+#include "Codebook.h"
+
+
+namespace OBJREC {
+
+
+  /**
+   * @class BoWFeatureConverter
+   * @brief Convert a set of features into a Bag of visual Words histogram (either by Vector Quantization, or Hard / Soft Assignment)
+   * @author Alexander Freytag
+   * @date 11-06-2013 (dd-mm-yyyy)
+  */   
+  class BoWFeatureConverter 
+  {
+
+    protected:
+      //! normalization method used (enum type)
+      int n_normalizationMethod;
+      
+      //! quantization method used (enum type)
+      int n_quantizationMethod;
+
+      //! pointer to our codebook
+      const Codebook *codebook;
+      
+      //! the section name if we want to read something from the config file lateron
+      std::string s_section;
+      
+      //! the config file to specify parameter settings
+      const NICE::Config * p_conf;
+
+
+
+    public:
+      //! enum type used for normalization method
+      enum {
+        NORMALIZE_RAW = 0,
+        NORMALIZE_BINZERO,
+        NORMALIZE_SUM,
+        NORMALIZE_THRESH
+      };
+      
+      //! enum type used to specify feature -> clusters (vector quant. , or vector assignment (either hard or soft) )
+      enum {
+        VECTOR_QUANTIZATION = 0,
+        VECTOR_ASSIGNMENT
+      };
+         
+
+      /**
+      * @brief standard constructor 
+      *
+      * @param conf pointer to a Config object with some parameter settings (currently not used)
+      * @param codebook pointer to the codebook (keep the pointer!)
+      */
+      BoWFeatureConverter( const NICE::Config *conf,
+          const Codebook *codebook, const std::string _section = "BoWFeatureConverter" );
+          
+      /** simple destructor */
+      virtual ~BoWFeatureConverter();
+      
+      void calcHistogram ( const NICE::VVector & features,
+              NICE::Vector & histogram, const bool & b_resetHistogram = true );
+
+      void normalizeHistogram ( NICE::Vector & histogram );
+
+      /**
+      * @brief set the type of the normalization method (see the enum of the class)
+      *
+      * @param normalizationMethod see enum type
+      */
+      void setNormalizationMethod ( int normalizationMethod );
+
+      /**
+      * @brief get the currently used normalization method
+      *
+      * @return see enum type
+      */
+      int getNormalizationMethod () const;
+      
+  };
+
+
+} // namespace
+
+#endif