Browse Source

ConvolutionalFeature: added ability to use spatial priors

Sven Sickert 10 years ago
parent
commit
f19fdc82fd

+ 31 - 1
features/fpfeatures/ConvolutionFeature.cpp

@@ -25,6 +25,7 @@ ConvolutionFeature::ConvolutionFeature ( )
     window_size_x = 15;
     window_size_y = 15;
     isColor = false;
+    useSpatialPriors = false;
 
     initializeParameterVector();
 }
@@ -38,6 +39,7 @@ ConvolutionFeature::ConvolutionFeature (
     window_size_x = wsize_x;
     window_size_y = wsize_y;
     isColor = color;
+    useSpatialPriors = false;
 
     initializeParameterVector();
 }
@@ -49,6 +51,7 @@ ConvolutionFeature::ConvolutionFeature ( const Config *conf )
     window_size_x = conf->gI ( section, "window_size_x", 15 );
     window_size_y = conf->gI ( section, "window_size_y", 15 );
     isColor = conf->gB ( section, "is_color", false );
+    useSpatialPriors = conf->gB ( section, "use_spatial_priors", false );
 
     initializeParameterVector();
 }
@@ -60,6 +63,7 @@ ConvolutionFeature::ConvolutionFeature ( const ConvolutionFeature *confFeat )
     window_size_y = confFeat->window_size_y;
     betaLength = confFeat->betaLength;
     isColor = confFeat->isColor;
+    useSpatialPriors = confFeat->useSpatialPriors;
     numChannels = confFeat->numChannels;
     beta = new NICE::Vector( betaLength, 0.0 );
 
@@ -90,6 +94,8 @@ void ConvolutionFeature::initializeParameterVector()
 
         betaLength = numChannels*window_size_x*window_size_y + 1;
 
+        if (useSpatialPriors) betaLength += 2;
+
         beta = new NICE::Vector( betaLength, (1.0/(double)(betaLength-1) ) );
         beta[0] = 1;
     }
@@ -143,6 +149,12 @@ NICE::Vector ConvolutionFeature::getFeatureVector( const Example *example ) cons
             }
         }
 
+    if (useSpatialPriors)
+    {
+        vec[betaLength-2] = (double)x/(double)xsize;
+        vec[betaLength-1] = (double)y/(double)ysize;
+    }
+
     return vec;
 }
 
@@ -219,6 +231,12 @@ double ConvolutionFeature::val ( const Example *example ) const
             }
         }
 
+    if (useSpatialPriors)
+    {
+        val1 += (double)x/(double)xsize * beta->operator [](betaLength-2);
+        val1 += (double)y/(double)ysize * beta->operator [](betaLength-1);
+    }
+
     return val1;
 }
 
@@ -254,11 +272,23 @@ void ConvolutionFeature::restore ( std::istream & is, int format )
     is >> betaLength;
 
     isColor = false;
+    useSpatialPriors = false;
     numChannels = 1;
-    if ( betaLength > (window_size_x*window_size_y+1) )
+
+    if ( betaLength == (window_size_x*window_size_y+3) )
+    {
+        useSpatialPriors = true;
+    }
+    else if ( betaLength == (3*window_size_x*window_size_y+1) )
+    {
+        isColor = true;
+        numChannels = 3;
+    }
+    else if ( betaLength == (3*window_size_x*window_size_y+3) )
     {
         isColor = true;
         numChannels = 3;
+        useSpatialPriors = true;
     }
 
     beta = new NICE::Vector( betaLength, 1.0 );

+ 1 - 0
features/fpfeatures/ConvolutionFeature.h

@@ -35,6 +35,7 @@ class ConvolutionFeature : public Feature
     int betaLength;
     int numChannels;
     bool isColor;
+    bool useSpatialPriors;
 
     NICE::Vector *beta;