浏览代码

added ConvolutionFeature to feature pool

Sven Sickert 10 年之前
父节点
当前提交
4e20a3464c

+ 218 - 0
features/fpfeatures/ConvolutionFeature.cpp

@@ -0,0 +1,218 @@
+/**
+* @file ConvolutionFeature.cpp
+* @brief convolutional feature
+* @author Sven Sickert
+* @date 10/13/2008
+
+*/
+#include <iostream>
+
+#include "ConvolutionFeature.h"
+#include "vislearning/cbaselib/FeaturePool.h"
+
+using namespace OBJREC;
+
+using namespace std;
+using namespace NICE;
+
+/** simple constructor */
+ConvolutionFeature::ConvolutionFeature ( )
+{
+    window_size_x = 15;
+    window_size_y = 15;
+
+    initializeParameterVector();
+}
+
+/** default constructor */
+ConvolutionFeature::ConvolutionFeature ( const Config *conf )
+{
+  window_size_x = conf->gI ( "ConvolutionFeature", "window_size_x", 15 );
+  window_size_y = conf->gI ( "ConvolutionFeature", "window_size_y", 15 );
+
+  initializeParameterVector();
+}
+
+/** simple destructor */
+ConvolutionFeature::~ConvolutionFeature ( )
+{
+
+}
+
+
+/** (re)initialize parameter vector */
+void ConvolutionFeature::initializeParameterVector()
+{
+  if (window_size_x > 0 && window_size_y > 0)
+  {
+    beta_length = window_size_x*window_size_y;
+    beta = new NICE::Vector( beta_length, (1.0/beta_length) );
+  }
+  else
+    std::cerr << "ConvolutionFeature::initializeVector: Size of window is Zero! Could not initialize..." << std::endl;
+}
+
+/** return parameter vector */
+NICE::Vector ConvolutionFeature::getParameterVector() const
+{
+  NICE::Vector res = (*this->beta);
+  return res;
+}
+
+/** return feature vector */
+NICE::Vector ConvolutionFeature::getFeatureVector( const Example *example )
+{
+  NICE::Vector vec(window_size_x*window_size_y, 0.0);;
+
+  const NICE::MultiChannelImageT<int> & img =
+      example->ce->getIChannel( CachedExample::I_GRAYVALUES );
+
+  int xsize, ysize, x, y;
+
+  example->ce->getImageSize( xsize, ysize );
+  x = example->x;
+  y = example->y;
+
+  int halfwsx = std::floor ( window_size_x / 2 );
+  int halfwsy = std::floor ( window_size_y / 2 );
+
+  int k = 0;
+  for ( int v = -halfwsy; v <= halfwsy; v++ )
+    for ( int u = -halfwsx; u <= halfwsx; u++ )
+    {
+      if ( x+u > 0
+           && x+u < xsize
+           && y+v > 0
+           && y+v < ysize
+           && k < vec.size() )
+      {
+        vec[k] = img.get(x+u,y+v);
+      }
+      k++;
+    }
+
+  return vec;
+}
+
+/** return length of parameter vector */
+int ConvolutionFeature::getParameterLength() const
+{
+  return beta_length;
+}
+
+/** set parameter vector */
+void ConvolutionFeature::setParameterVector( const Vector & vec )
+{
+  double sum = 0.0;
+
+  if ( beta->size() == vec.size() )
+  {
+    int i = 0;
+    for ( NICE::Vector::iterator it = beta->begin();
+          it != beta->end(); ++it, i++ )
+    {
+      *it = vec[i];
+      sum += vec[i];
+    }
+  }
+  else
+    std::cerr << "ConvolutionFeature::setParameterVector: Vector sizes do not match! Could not update parameter vector..." << std::endl;
+
+  if ( beta->Sum() != 1.0 )
+    (*beta) /= sum;
+}
+
+/** return feature value */
+double ConvolutionFeature::val ( const Example *example ) const
+{
+  // is parameter vector initialized?
+  if (beta == NULL)
+    return 0.0;
+
+  const NICE::MultiChannelImageT<int> & img =
+      example->ce->getIChannel( CachedExample::I_GRAYVALUES );
+
+  int xsize, ysize, x, y;
+
+  example->ce->getImageSize( xsize, ysize );
+  x = example->x;
+  y = example->y;
+
+  int halfwsx = std::floor ( window_size_x / 2 );
+  int halfwsy = std::floor ( window_size_y / 2 );
+
+  int k = 0;
+  double val1 = 0.0;
+  for ( int v = -halfwsy; v <= halfwsy; v++ )
+    for ( int u = -halfwsx; u <= halfwsx; u++, k++ )
+    {
+      if ( x+u > 0
+           && x+u < xsize
+           && y+v > 0
+           && y+v < ysize
+           && k < beta->size() )
+      {
+        val1 += (double)img.get(x+u,y+v) * beta->operator [](k);
+      }
+    }
+
+  return std::floor(val1);
+}
+
+/** creature feature pool */
+void ConvolutionFeature::explode ( FeaturePool &featurePool, bool variableWindow ) const
+{
+  ConvolutionFeature *f = new ConvolutionFeature();
+  f->window_size_x = window_size_x;
+  f->window_size_y = window_size_y;
+  f->initializeParameterVector();
+
+  featurePool.addFeature(f);
+}
+
+/** clone current feature */
+Feature *ConvolutionFeature::clone ( ) const
+{
+  ConvolutionFeature *f = new ConvolutionFeature ();
+  f->window_size_x = window_size_x;
+  f->window_size_y = window_size_y;
+  f->beta = beta;
+  f->beta_length = beta_length;
+
+  return f;
+}
+
+Feature *ConvolutionFeature::generateFirstParameter () const
+{
+  return clone();
+}
+
+void ConvolutionFeature::restore ( istream & is, int format )
+{
+  is >> window_size_x;
+  is >> window_size_y;
+  is >> beta_length;
+
+  beta = new NICE::Vector( beta_length, 1.0 );
+  for ( NICE::Vector::iterator it = beta->begin();
+        it != beta->end(); ++it )
+    is >> *it;
+}
+
+void ConvolutionFeature::store ( ostream & os, int format ) const
+{
+  os << "ConvolutionFeature "
+  << window_size_x << " "
+  << window_size_y << " "
+  << beta_length;
+
+  for ( NICE::Vector::const_iterator it = beta->begin();
+        it != beta->end(); ++it )
+    os << ' ' << *it;
+
+}
+
+void ConvolutionFeature::clear ()
+{
+  beta->clear();
+}

+ 136 - 0
features/fpfeatures/ConvolutionFeature.h

@@ -0,0 +1,136 @@
+/**
+* @file ConvolutionFeature.h
+* @brief convolutional feature
+* @author Sven Sickert
+* @date 10/13/2008
+
+*/
+#ifndef ConvolutionFeatureINCLUDE
+#define ConvolutionFeatureINCLUDE
+
+#include "core/vector/VectorT.h"
+#include "core/vector/MatrixT.h"
+
+#include "core/basics/Config.h"
+#include "vislearning/cbaselib/Feature.h"
+
+
+namespace OBJREC{
+
+/** convolutional feature */
+class ConvolutionFeature : public Feature
+{
+
+  protected:
+
+  /////////////////////////
+  /////////////////////////
+  // PROTECTED VARIABLES //
+  /////////////////////////
+  /////////////////////////
+
+    /** feature parameter */
+    int window_size_x;
+    int window_size_y;
+    int beta_length;
+
+    NICE::Vector *beta;
+
+  public:
+
+    ///////////////////// ///////////////////// /////////////////////
+    //                   CONSTRUCTORS / DESTRUCTORS
+    ///////////////////// ///////////////////// /////////////////////
+
+    /** simple constructor */
+    ConvolutionFeature ( );
+
+    /** default constructor */
+    ConvolutionFeature ( const NICE::Config *conf );
+
+    /** simple destructor */
+    virtual ~ConvolutionFeature ( );
+
+
+    ///////////////////// ///////////////////// /////////////////////
+    //                      FEATURE STUFF
+    ///////////////////// ///////////////////// /////////////////////
+
+    /**
+     * @brief (re)initialize parameter vector
+     */
+    void initializeParameterVector();
+
+    /**
+     * @brief return parameter vector
+     * @return parameter vector
+     */
+    NICE::Vector getParameterVector () const;
+
+    /**
+     * @brief return feature vector
+     * @param example current example
+     * @return feature vector
+     */
+    NICE::Vector getFeatureVector ( const Example *example );
+
+    /**
+     * @brief return length of parameter vector
+     * @return length of vector
+     */
+    int getParameterLength () const;
+
+    /**
+     * @brief set parameter vector
+     * @param vec new parameter vector
+     */
+    void setParameterVector ( const NICE::Vector &vec );
+
+    /**
+     * @brief return feature value for given example
+     * @param example given Example
+     * @return double feature value
+     */
+    double val ( const Example *example ) const;
+
+    /**
+     * @brief create feature pool with convolutional features
+     * @param featurePool to be filled
+     * @param variableWindow
+     */
+    void explode ( FeaturePool &featurePool, bool variableWindow = true ) const;
+
+    /**
+     * @brief clone current feature
+     * @return clone of current feature
+     */
+    Feature *clone () const;
+
+    Feature *generateFirstParameter () const;
+
+    ///////////////////// INTERFACE PERSISTENT /////////////////////
+    // interface specific methods for store and restore
+    ///////////////////// INTERFACE PERSISTENT /////////////////////
+
+    /**
+     * @brief Load convolution feature object from external file (stream)
+     */
+    virtual void restore ( std::istream & is, int format = 0 );
+
+    /**
+     * @brief Save convolution feature object to external file (stream)
+     */
+    virtual void store( std::ostream & os, int format = 0 ) const;
+
+    /**
+     * @brief Clear convolution feature object
+     */
+    virtual void clear ();
+
+};
+
+
+
+} //namespace
+
+#endif

+ 5 - 0
features/fpfeatures/createFeatures.cpp

@@ -6,6 +6,7 @@
 #include "EOHFeature.h"
 #include "SparseVectorFeature.h"
 #include "ColorHistogramFeature.h"
+#include "ConvolutionFeature.h"
 
 #include "createFeatures.h"
 
@@ -43,6 +44,10 @@ Feature *OBJREC::createFeatureFromTag ( const Config *conf, const std::string &
 	    ||  ( tag == "SparseVectorFeature" ) ){
 	return new SparseVectorFeature ( 4711 ); // bogus dimension value only needed for explode
 
+    } else if ( ( tag == "CONVOLUTIONFEATURE")
+        || ( tag == "ConvolutionFeature" ) ) {
+    return new ConvolutionFeature ( conf );
+
     } else {
 	return NULL;
     }