Эх сурвалжийг харах

modified ConvFeature for 3d examples

Sven Sickert 9 жил өмнө
parent
commit
be2a706525

+ 77 - 28
features/fpfeatures/ConvolutionFeature.cpp

@@ -25,6 +25,7 @@ ConvolutionFeature::ConvolutionFeature ( )
 {
     window_size_x = 15;
     window_size_y = 15;
+    window_size_z = 15;
     isColor = false;
     useSpatialPriors = false;
 
@@ -40,6 +41,24 @@ ConvolutionFeature::ConvolutionFeature (
 {
     window_size_x = wsize_x;
     window_size_y = wsize_y;
+    window_size_z = 1;
+    isColor = color;
+    useSpatialPriors = prior;
+
+    initializeParameterVector();
+}
+
+/** alternative 3d constructor */
+ConvolutionFeature::ConvolutionFeature (
+        const int wsize_x,
+        const int wsize_y,
+        const int wsize_z,
+        const bool color,
+        const bool prior )
+{
+    window_size_x = wsize_x;
+    window_size_y = wsize_y;
+    window_size_z = wsize_z;
     isColor = color;
     useSpatialPriors = prior;
 
@@ -52,6 +71,7 @@ ConvolutionFeature::ConvolutionFeature ( const Config *conf )
     std::string section = "ConvolutionFeature";
     window_size_x = conf->gI ( section, "window_size_x", 15 );
     window_size_y = conf->gI ( section, "window_size_y", 15 );
+    window_size_z = conf->gI ( section, "window_size_z", 15 );
     isColor = conf->gB ( section, "is_color", false );
     useSpatialPriors = conf->gB ( section, "use_spatial_priors", false );
 
@@ -63,6 +83,7 @@ ConvolutionFeature::ConvolutionFeature ( const ConvolutionFeature *confFeat )
 {
     window_size_x = confFeat->window_size_x;
     window_size_y = confFeat->window_size_y;
+    window_size_z = confFeat->window_size_z;
     paramsLength = confFeat->paramsLength;
     isColor = confFeat->isColor;
     useSpatialPriors = confFeat->useSpatialPriors;
@@ -88,14 +109,14 @@ ConvolutionFeature::~ConvolutionFeature ( )
 /** (re)initialize parameter vector */
 void ConvolutionFeature::initializeParameterVector()
 {
-    if (window_size_x > 0 && window_size_y > 0)
+    if (window_size_x > 0 && window_size_y > 0 && window_size_z > 0)
     {
         if (isColor)
             numChannels = 3;
         else
             numChannels = 1;
 
-        paramsLength = numChannels*window_size_x*window_size_y + 1;
+        paramsLength = numChannels*window_size_x*window_size_y*window_size_z + 1;
 
         if (useSpatialPriors) paramsLength += 2;
 
@@ -124,33 +145,38 @@ void ConvolutionFeature::getFeatureVector(
         const Example *example,
         NICE::Vector & vec ) const
 {
-    NICE::MultiChannelImageT<double> * imgD = NULL;
-    imgD = & example->ce->getDChannel( CachedExample::D_EOH );
-    double** data = imgD->getDataPointer();
+    NICE::MultiChannelImage3DT<double> * imgD = NULL;
+    imgD = & example->ce->getDChannel3( CachedExample::D_EOH );
+    std::vector<double*> data = imgD->getDataPointer();
 
-    int xsize, ysize;
-    example->ce->getImageSize( xsize, ysize );
+    int xsize, ysize, zsize;
+    example->ce->getImageSize3( xsize, ysize, zsize );
 
     const int x = example->x;
     const int y = example->y;
+    const int z = example->z;
     const int halfwsx = std::floor ( window_size_x / 2 );
     const int halfwsy = std::floor ( window_size_y / 2 );
+    const int halfwsz = std::floor ( window_size_z / 2 );
     //const int step = window_size_x*window_size_y;
 
     int k = 1;
     for ( int c = 0; c < numChannels; c++)
-        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;
-
-                //vec[k] = imgD->get(x+uu,y+vv,c);
-                vec[k] = data[c][(x+uu)+(y+vv)*xsize];
-
-            }
+        for ( int w = -halfwsz; w <= halfwsz; w++ )
+            for ( int v = -halfwsy; v <= halfwsy; v++ )
+                for ( int u = -halfwsx; u <= halfwsx; u++, k++ )
+                {
+                    int uu = u;
+                    int vv = v;
+                    int ww = w;
+                    if (x+u < 0 || x+u >= xsize) uu=-u;
+                    if (y+v < 0 || y+v >= ysize) vv=-v;
+                    if (z+v < 0 || z+v >= zsize) ww=-w;
+
+                    //vec[k] = imgD->get(x+uu,y+vv,c);
+                    vec[k] = data[c][(x+uu)+(y+vv)*xsize+(z+ww)*xsize*ysize];
+
+                }
 
     if (useSpatialPriors)
     {
@@ -228,6 +254,7 @@ void ConvolutionFeature::explode ( FeaturePool &featurePool, bool variableWindow
     ConvolutionFeature *f = new ConvolutionFeature (
                 this->window_size_x,
                 this->window_size_y,
+                this->window_size_z,
                 this->isColor,
                 this->useSpatialPriors );
 
@@ -240,6 +267,7 @@ Feature *ConvolutionFeature::clone ( ) const
     ConvolutionFeature *f = new ConvolutionFeature (
                 this->window_size_x,
                 this->window_size_y,
+                this->window_size_z,
                 this->isColor,
                 this->useSpatialPriors );
 
@@ -255,24 +283,34 @@ Feature *ConvolutionFeature::generateFirstParameter () const
 
 void ConvolutionFeature::restore ( std::istream & is, int format )
 {
-    is >> window_size_x;
-    is >> window_size_y;
+    if ( format == 1 )
+    {
+        is >> window_size_x;
+        is >> window_size_y;
+        is >> window_size_z;
+    }
+    else
+    {
+        is >> window_size_x;
+        is >> window_size_y;
+        window_size_z = 1;
+    }
     is >> paramsLength;
 
     isColor = false;
     useSpatialPriors = false;
     numChannels = 1;
 
-    if ( paramsLength == (window_size_x*window_size_y+3) )
+    if ( paramsLength == (window_size_x*window_size_y*window_size_z+3) )
     {
         useSpatialPriors = true;
     }
-    else if ( paramsLength == (3*window_size_x*window_size_y+1) )
+    else if ( paramsLength == (3*window_size_x*window_size_y*window_size_z+1) )
     {
         isColor = true;
         numChannels = 3;
     }
-    else if ( paramsLength == (3*window_size_x*window_size_y+3) )
+    else if ( paramsLength == (3*window_size_x*window_size_y*window_size_z+3) )
     {
         isColor = true;
         numChannels = 3;
@@ -287,10 +325,21 @@ void ConvolutionFeature::restore ( std::istream & is, int format )
 
 void ConvolutionFeature::store ( std::ostream & os, int format ) const
 {
-    os << "ConvolutionFeature "
-       << window_size_x << " "
-       << window_size_y << " "
-       << paramsLength;
+    if ( format == 1 )
+    {
+        os << "ConvolutionFeature "
+           << window_size_x << " "
+           << window_size_y << " "
+           << window_size_z << " "
+           << paramsLength;
+    }
+    else
+    {
+        os << "ConvolutionFeature "
+           << window_size_x << " "
+           << window_size_y << " "
+           << paramsLength;
+    }
 
     for ( NICE::Vector::const_iterator it = params->begin();
           it != params->end(); ++it )

+ 8 - 0
features/fpfeatures/ConvolutionFeature.h

@@ -32,6 +32,7 @@ class ConvolutionFeature : public Feature
     /** feature parameter */
     int window_size_x;
     int window_size_y;
+    int window_size_z;
     int paramsLength;
     int numChannels;
     bool isColor;
@@ -59,6 +60,13 @@ class ConvolutionFeature : public Feature
                          const bool color = false,
                          const bool prior = false );
 
+    /** alternative 3d constructor */
+    ConvolutionFeature ( const int wsize_x,
+                         const int wsize_y,
+                         const int wsize_z,
+                         const bool color = false,
+                         const bool prior = false );
+
     /** default constructor */
     ConvolutionFeature ( const NICE::Config *conf );