Browse Source

ConvolutionFeature: indention, copy constructor, output changes, ...

Sven Sickert 10 years ago
parent
commit
47614b091a
2 changed files with 156 additions and 130 deletions
  1. 146 126
      features/fpfeatures/ConvolutionFeature.cpp
  2. 10 4
      features/fpfeatures/ConvolutionFeature.h

+ 146 - 126
features/fpfeatures/ConvolutionFeature.cpp

@@ -12,7 +12,6 @@
 
 
 using namespace OBJREC;
 using namespace OBJREC;
 
 
-using namespace std;
 using namespace NICE;
 using namespace NICE;
 
 
 /** simple constructor */
 /** simple constructor */
@@ -24,13 +23,38 @@ ConvolutionFeature::ConvolutionFeature ( )
     initializeParameterVector();
     initializeParameterVector();
 }
 }
 
 
+/** alternative constructor */
+ConvolutionFeature::ConvolutionFeature ( const int wsize_x, const int wsize_y )
+{
+    window_size_x = wsize_x;
+    window_size_y = wsize_y;
+
+    initializeParameterVector();
+}
+
 /** default constructor */
 /** default constructor */
 ConvolutionFeature::ConvolutionFeature ( const Config *conf )
 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 );
+    window_size_x = conf->gI ( "ConvolutionFeature", "window_size_x", 15 );
+    window_size_y = conf->gI ( "ConvolutionFeature", "window_size_y", 15 );
 
 
-  initializeParameterVector();
+    initializeParameterVector();
+}
+
+/** copy constructor */
+ConvolutionFeature::ConvolutionFeature ( const ConvolutionFeature *confFeat )
+{
+    window_size_x = confFeat->window_size_x;
+    window_size_y = confFeat->window_size_y;
+    beta_length = confFeat->beta_length;
+    beta = new NICE::Vector( beta_length, 0.0 );
+
+    int i = 0;
+    for ( NICE::Vector::iterator it = confFeat->beta->begin();
+          it != confFeat->beta->end(); ++it, i++ )
+    {
+        beta[i] = *it;
+    }
 }
 }
 
 
 /** simple destructor */
 /** simple destructor */
@@ -43,182 +67,178 @@ ConvolutionFeature::~ConvolutionFeature ( )
 /** (re)initialize parameter vector */
 /** (re)initialize parameter vector */
 void ConvolutionFeature::initializeParameterVector()
 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;
+    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 */
 /** return parameter vector */
 NICE::Vector ConvolutionFeature::getParameterVector() const
 NICE::Vector ConvolutionFeature::getParameterVector() const
 {
 {
-  NICE::Vector res = (*this->beta);
-  return res;
+    NICE::Vector res = (*this->beta);
+    return res;
 }
 }
 
 
 /** return feature vector */
 /** return feature vector */
-NICE::Vector ConvolutionFeature::getFeatureVector( const Example *example )
+NICE::Vector ConvolutionFeature::getFeatureVector( const Example *example ) const
 {
 {
-  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++ )
-    {
-      int uu = u;
-      int vv = v;
-      if (x+u < 0 || x+u >= xsize) uu=-u;
-      if (y+v < 0 || y+v >= ysize) vv=-v;
-
-      if ( x+uu > 0
-           && x+uu < xsize
-           && y+vv > 0
-           && y+vv < ysize
-           && k < vec.size() )
-      {
-        vec[k] = img.get(x+uu,y+vv);
-      }
-      k++;
-    }
-
-  return vec;
+    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++ )
+        {
+            int uu = u;
+            int vv = v;
+            if (x+u < 0 || x+u >= xsize) uu=-u;
+            if (y+v < 0 || y+v >= ysize) vv=-v;
+
+            if ( x+uu > 0
+                 && x+uu < xsize
+                 && y+vv > 0
+                 && y+vv < ysize
+                 && k < vec.size() )
+            {
+                vec[k] = img.get(x+uu,y+vv);
+            }
+            k++;
+        }
+
+    return vec;
 }
 }
 
 
 /** return length of parameter vector */
 /** return length of parameter vector */
 int ConvolutionFeature::getParameterLength() const
 int ConvolutionFeature::getParameterLength() const
 {
 {
-  return beta_length;
+    return beta_length;
 }
 }
 
 
 /** set parameter vector */
 /** set parameter vector */
 void ConvolutionFeature::setParameterVector( const Vector & vec )
 void ConvolutionFeature::setParameterVector( const Vector & vec )
 {
 {
-  if ( beta->size() == vec.size() )
-  {
-    int i = 0;
-    for ( NICE::Vector::iterator it = beta->begin();
-          it != beta->end(); ++it, i++ )
+    if ( beta->size() == vec.size() )
     {
     {
-      *it = vec[i];
+        int i = 0;
+        for ( NICE::Vector::iterator it = beta->begin();
+              it != beta->end(); ++it, i++ )
+        {
+            *it = vec[i];
+        }
+        beta->normalizeL2();
     }
     }
-  }
-  else
-    std::cerr << "ConvolutionFeature::setParameterVector: Vector sizes do not match! Could not update parameter vector..." << std::endl;
+    else
+        std::cerr << "ConvolutionFeature::setParameterVector: Vector sizes do not match! Could not update parameter vector..." << std::endl;
 
 
-  if ( beta->Sum() != 1.0 )
-    beta->normalizeL2();
 }
 }
 
 
 /** return feature value */
 /** return feature value */
 double ConvolutionFeature::val ( const Example *example ) const
 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++ )
-    {
-      int uu = u;
-      int vv = v;
-      if (x+u < 0 || x+u >= xsize) uu=-u;
-      if (y+v < 0 || y+v >= ysize) vv=-v;
-      if ( x+uu > 0
-           && x+uu < xsize
-           && y+vv > 0
-           && y+vv < ysize
-           && k < beta->size() )
-      {
-        val1 += (double)img.get(x+uu,y+vv) * beta->operator [](k);
-      }
-    }
-
-  return std::floor(val1);
+    // 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++ )
+        {
+            int uu = u;
+            int vv = v;
+            if (x+u < 0 || x+u >= xsize) uu=-u;
+            if (y+v < 0 || y+v >= ysize) vv=-v;
+            if ( x+uu > 0
+                 && x+uu < xsize
+                 && y+vv > 0
+                 && y+vv < ysize
+                 && k < beta->size() )
+            {
+                val1 += (double)img.get(x+uu,y+vv) * beta->operator [](k);
+            }
+        }
+
+    return std::floor(val1);
 }
 }
 
 
 /** creature feature pool */
 /** creature feature pool */
 void ConvolutionFeature::explode ( FeaturePool &featurePool, bool variableWindow ) const
 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();
+    ConvolutionFeature *f = new ConvolutionFeature();
+    f->window_size_x = window_size_x;
+    f->window_size_y = window_size_y;
+    f->initializeParameterVector();
 
 
-  featurePool.addFeature(f);
+    featurePool.addFeature(f);
 }
 }
 
 
 /** clone current feature */
 /** clone current feature */
 Feature *ConvolutionFeature::clone ( ) const
 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;
+    ConvolutionFeature *f =
+            new ConvolutionFeature ( *this );
 
 
-  return f;
+    return f;
 }
 }
 
 
 Feature *ConvolutionFeature::generateFirstParameter () const
 Feature *ConvolutionFeature::generateFirstParameter () const
 {
 {
-  return clone();
+    return clone();
 }
 }
 
 
-void ConvolutionFeature::restore ( istream & is, int format )
+void ConvolutionFeature::restore ( std::istream & is, int format )
 {
 {
-  is >> window_size_x;
-  is >> window_size_y;
-  is >> beta_length;
+    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;
+    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
+void ConvolutionFeature::store ( std::ostream & os, int format ) const
 {
 {
-  os << "ConvolutionFeature "
-  << window_size_x << " "
-  << window_size_y << " "
-  << beta_length;
+    os << "ConvolutionFeature "
+       << window_size_x << " "
+       << window_size_y << " "
+       << beta_length;
 
 
-  for ( NICE::Vector::const_iterator it = beta->begin();
-        it != beta->end(); ++it )
-    os << ' ' << *it;
+    for ( NICE::Vector::const_iterator it = beta->begin();
+          it != beta->end(); ++it )
+        os << ' ' << *it;
 
 
 }
 }
 
 
 void ConvolutionFeature::clear ()
 void ConvolutionFeature::clear ()
 {
 {
-  beta->clear();
+    beta->clear();
 }
 }

+ 10 - 4
features/fpfeatures/ConvolutionFeature.h

@@ -45,9 +45,15 @@ class ConvolutionFeature : public Feature
     /** simple constructor */
     /** simple constructor */
     ConvolutionFeature ( );
     ConvolutionFeature ( );
 
 
+    /** alternative constructor */
+    ConvolutionFeature ( const int wsize_x, const int wsize_y );
+
     /** default constructor */
     /** default constructor */
     ConvolutionFeature ( const NICE::Config *conf );
     ConvolutionFeature ( const NICE::Config *conf );
 
 
+    /** copy constructor */
+    ConvolutionFeature ( const ConvolutionFeature *convFeat );
+
     /** simple destructor */
     /** simple destructor */
     virtual ~ConvolutionFeature ( );
     virtual ~ConvolutionFeature ( );
 
 
@@ -72,7 +78,7 @@ class ConvolutionFeature : public Feature
      * @param example current example
      * @param example current example
      * @return feature vector
      * @return feature vector
      */
      */
-    NICE::Vector getFeatureVector ( const Example *example );
+    NICE::Vector getFeatureVector ( const Example *example ) const;
 
 
     /**
     /**
      * @brief return length of parameter vector
      * @brief return length of parameter vector
@@ -115,17 +121,17 @@ class ConvolutionFeature : public Feature
     /**
     /**
      * @brief Load convolution feature object from external file (stream)
      * @brief Load convolution feature object from external file (stream)
      */
      */
-    virtual void restore ( std::istream & is, int format = 0 );
+    void restore ( std::istream & is, int format = 0 );
 
 
     /**
     /**
      * @brief Save convolution feature object to external file (stream)
      * @brief Save convolution feature object to external file (stream)
      */
      */
-    virtual void store( std::ostream & os, int format = 0 ) const;
+    void store( std::ostream & os, int format = 0 ) const;
 
 
     /**
     /**
      * @brief Clear convolution feature object
      * @brief Clear convolution feature object
      */
      */
-    virtual void clear ();
+    void clear ();
 
 
 };
 };