|
@@ -24,15 +24,20 @@ ConvolutionFeature::ConvolutionFeature ( )
|
|
|
{
|
|
|
window_size_x = 15;
|
|
|
window_size_y = 15;
|
|
|
+ isColor = false;
|
|
|
|
|
|
initializeParameterVector();
|
|
|
}
|
|
|
|
|
|
/** alternative constructor */
|
|
|
-ConvolutionFeature::ConvolutionFeature ( const int wsize_x, const int wsize_y )
|
|
|
+ConvolutionFeature::ConvolutionFeature (
|
|
|
+ const int wsize_x,
|
|
|
+ const int wsize_y,
|
|
|
+ const bool color )
|
|
|
{
|
|
|
window_size_x = wsize_x;
|
|
|
window_size_y = wsize_y;
|
|
|
+ isColor = color;
|
|
|
|
|
|
initializeParameterVector();
|
|
|
}
|
|
@@ -40,8 +45,10 @@ ConvolutionFeature::ConvolutionFeature ( const int wsize_x, const int wsize_y )
|
|
|
/** 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 );
|
|
|
+ std::string section = "ConvolutionFeature";
|
|
|
+ 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 );
|
|
|
|
|
|
initializeParameterVector();
|
|
|
}
|
|
@@ -51,8 +58,9 @@ 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 );
|
|
|
+ betaLength = confFeat->betaLength;
|
|
|
+ isColor = confFeat->isColor;
|
|
|
+ beta = new NICE::Vector( betaLength, 0.0 );
|
|
|
|
|
|
int i = 0;
|
|
|
for ( NICE::Vector::iterator it = confFeat->beta->begin();
|
|
@@ -74,12 +82,17 @@ void ConvolutionFeature::initializeParameterVector()
|
|
|
{
|
|
|
if (window_size_x > 0 && window_size_y > 0)
|
|
|
{
|
|
|
- beta_length = window_size_x*window_size_y + 1;
|
|
|
- beta = new NICE::Vector( beta_length, (1.0/(double)(beta_length-1) ) );
|
|
|
+ if (isColor)
|
|
|
+ betaLength = 3*window_size_x*window_size_y + 1;
|
|
|
+ else
|
|
|
+ betaLength = window_size_x*window_size_y + 1;
|
|
|
+
|
|
|
+ beta = new NICE::Vector( betaLength, (1.0/(double)(betaLength-1) ) );
|
|
|
beta[0] = 1;
|
|
|
}
|
|
|
else
|
|
|
- std::cerr << "ConvolutionFeature::initializeVector: Size of window is Zero! Could not initialize..." << std::endl;
|
|
|
+ std::cerr << "ConvolutionFeature::initializeVector: Size of window is Zero! Could not initialize..."
|
|
|
+ << std::endl;
|
|
|
}
|
|
|
|
|
|
/** return parameter vector */
|
|
@@ -92,10 +105,13 @@ NICE::Vector ConvolutionFeature::getParameterVector() const
|
|
|
/** return feature vector */
|
|
|
NICE::Vector ConvolutionFeature::getFeatureVector( const Example *example ) const
|
|
|
{
|
|
|
- NICE::Vector vec(window_size_x*window_size_y + 1, 1.0);;
|
|
|
+ NICE::Vector vec(betaLength, 1.0);
|
|
|
|
|
|
- const NICE::MultiChannelImageT<int> & img =
|
|
|
- example->ce->getIChannel( CachedExample::I_GRAYVALUES );
|
|
|
+ NICE::MultiChannelImageT<int> * img = NULL;
|
|
|
+ if (isColor)
|
|
|
+ img = & example->ce->getIChannel( CachedExample::I_COLOR );
|
|
|
+ else
|
|
|
+ img = & example->ce->getIChannel( CachedExample::I_GRAYVALUES );
|
|
|
|
|
|
int xsize, ysize, x, y;
|
|
|
|
|
@@ -105,26 +121,27 @@ NICE::Vector ConvolutionFeature::getFeatureVector( const Example *example ) cons
|
|
|
|
|
|
int halfwsx = std::floor ( window_size_x / 2 );
|
|
|
int halfwsy = std::floor ( window_size_y / 2 );
|
|
|
+ int numChannels = img->channels();
|
|
|
|
|
|
int k = 1;
|
|
|
- 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() )
|
|
|
+ for ( int c = 0; c < numChannels; c++ )
|
|
|
+ for ( int v = -halfwsy; v <= halfwsy; v++ )
|
|
|
+ for ( int u = -halfwsx; u <= halfwsx; u++, k++ )
|
|
|
{
|
|
|
- vec[k] = img.get(x+uu,y+vv);
|
|
|
+ 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,c);
|
|
|
+ }
|
|
|
}
|
|
|
- k++;
|
|
|
- }
|
|
|
|
|
|
return vec;
|
|
|
}
|
|
@@ -132,7 +149,7 @@ NICE::Vector ConvolutionFeature::getFeatureVector( const Example *example ) cons
|
|
|
/** return length of parameter vector */
|
|
|
int ConvolutionFeature::getParameterLength() const
|
|
|
{
|
|
|
- return beta_length;
|
|
|
+ return betaLength;
|
|
|
}
|
|
|
|
|
|
/** set parameter vector */
|
|
@@ -141,35 +158,39 @@ void ConvolutionFeature::setParameterVector( const Vector & vec )
|
|
|
if ( beta->size() == vec.size() )
|
|
|
{
|
|
|
int i = 0;
|
|
|
- double sum = 0.0;
|
|
|
for ( NICE::Vector::iterator it = beta->begin();
|
|
|
it != beta->end(); ++it, i++ )
|
|
|
{
|
|
|
*it = vec[i];
|
|
|
- sum = vec[i];
|
|
|
}
|
|
|
-
|
|
|
- // Normalize only kernel parameters
|
|
|
- double betaZero = vec[0];
|
|
|
- sum -= betaZero;
|
|
|
- beta->operator/= (sum);
|
|
|
- beta->operator[](0) = betaZero;
|
|
|
-
|
|
|
+ beta->normalizeL2();
|
|
|
}
|
|
|
else
|
|
|
- std::cerr << "ConvolutionFeature::setParameterVector: Vector sizes do not match! Could not update parameter vector..." << std::endl;
|
|
|
+ std::cerr << "ConvolutionFeature::setParameterVector: Vector sizes do not match!"
|
|
|
+ << " expected: " << beta->size() << ", got: " << vec.size()
|
|
|
+ << std::endl;
|
|
|
|
|
|
}
|
|
|
|
|
|
/** return feature value */
|
|
|
double ConvolutionFeature::val ( const Example *example ) const
|
|
|
{
|
|
|
- // is parameter vector initialized?
|
|
|
+ double val1 = 0.0;
|
|
|
+
|
|
|
+ // is parameter vector and image data available?
|
|
|
if (beta == NULL)
|
|
|
- return 0.0;
|
|
|
+ {
|
|
|
+ std::cerr << "ConvolutionalFeature::val: Missing parameter vector!"
|
|
|
+ << std::endl;
|
|
|
+
|
|
|
+ return val1;
|
|
|
+ }
|
|
|
|
|
|
- const NICE::MultiChannelImageT<int> & img =
|
|
|
- example->ce->getIChannel( CachedExample::I_GRAYVALUES );
|
|
|
+ NICE::MultiChannelImageT<int> * img = NULL;
|
|
|
+ if (isColor)
|
|
|
+ img = & example->ce->getIChannel( CachedExample::I_COLOR );
|
|
|
+ else
|
|
|
+ img = & example->ce->getIChannel( CachedExample::I_GRAYVALUES );
|
|
|
|
|
|
int xsize, ysize, x, y;
|
|
|
|
|
@@ -179,25 +200,26 @@ double ConvolutionFeature::val ( const Example *example ) const
|
|
|
|
|
|
int halfwsx = std::floor ( window_size_x / 2 );
|
|
|
int halfwsy = std::floor ( window_size_y / 2 );
|
|
|
+ int numChannels = img->channels();
|
|
|
|
|
|
int k = 1;
|
|
|
- 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() )
|
|
|
+ for ( int c = 0; c < numChannels; c++ )
|
|
|
+ for ( int v = -halfwsy; v <= halfwsy; v++ )
|
|
|
+ for ( int u = -halfwsx; u <= halfwsx; u++, k++ )
|
|
|
{
|
|
|
- val1 += (double)img.get(x+uu,y+vv) * beta->operator [](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,c) * beta->operator [](k);
|
|
|
+ }
|
|
|
}
|
|
|
- }
|
|
|
|
|
|
return val1;
|
|
|
}
|
|
@@ -206,7 +228,7 @@ double ConvolutionFeature::val ( const Example *example ) const
|
|
|
void ConvolutionFeature::explode ( FeaturePool &featurePool, bool variableWindow ) const
|
|
|
{
|
|
|
ConvolutionFeature *f =
|
|
|
- new ConvolutionFeature ( this->window_size_x, this->window_size_y );
|
|
|
+ new ConvolutionFeature ( this->window_size_x, this->window_size_y, this->isColor );
|
|
|
|
|
|
featurePool.addFeature(f);
|
|
|
}
|
|
@@ -215,7 +237,7 @@ void ConvolutionFeature::explode ( FeaturePool &featurePool, bool variableWindow
|
|
|
Feature *ConvolutionFeature::clone ( ) const
|
|
|
{
|
|
|
ConvolutionFeature *f =
|
|
|
- new ConvolutionFeature ( this->window_size_x, this->window_size_y );
|
|
|
+ new ConvolutionFeature ( this->window_size_x, this->window_size_y, this->isColor );
|
|
|
|
|
|
f->setParameterVector( *beta );
|
|
|
|
|
@@ -231,9 +253,13 @@ void ConvolutionFeature::restore ( std::istream & is, int format )
|
|
|
{
|
|
|
is >> window_size_x;
|
|
|
is >> window_size_y;
|
|
|
- is >> beta_length;
|
|
|
+ is >> betaLength;
|
|
|
+
|
|
|
+ isColor = false;
|
|
|
+ if ( betaLength > (window_size_x*window_size_y+1) )
|
|
|
+ isColor = true;
|
|
|
|
|
|
- beta = new NICE::Vector( beta_length, 1.0 );
|
|
|
+ beta = new NICE::Vector( betaLength, 1.0 );
|
|
|
for ( NICE::Vector::iterator it = beta->begin();
|
|
|
it != beta->end(); ++it )
|
|
|
is >> *it;
|
|
@@ -244,7 +270,7 @@ void ConvolutionFeature::store ( std::ostream & os, int format ) const
|
|
|
os << "ConvolutionFeature "
|
|
|
<< window_size_x << " "
|
|
|
<< window_size_y << " "
|
|
|
- << beta_length;
|
|
|
+ << betaLength;
|
|
|
|
|
|
for ( NICE::Vector::const_iterator it = beta->begin();
|
|
|
it != beta->end(); ++it )
|