123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335 |
- /**
- * @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 NICE;
- /* Convolutional feature consists of shift parameter beta[0] and the
- convolutional mask, which is stored in the rest of the parameter vector
- beta */
- /** simple constructor */
- ConvolutionFeature::ConvolutionFeature ( )
- {
- window_size_x = 15;
- window_size_y = 15;
- isColor = false;
- useSpatialPriors = false;
- initializeParameterVector();
- }
- /** alternative constructor */
- 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;
- useSpatialPriors = false;
- initializeParameterVector();
- }
- /** default constructor */
- 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 );
- isColor = conf->gB ( section, "is_color", false );
- useSpatialPriors = conf->gB ( section, "use_spatial_priors", false );
- initializeParameterVector();
- }
- /** copy constructor */
- ConvolutionFeature::ConvolutionFeature ( const ConvolutionFeature *confFeat )
- {
- window_size_x = confFeat->window_size_x;
- 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 );
- int i = 0;
- for ( NICE::Vector::iterator it = confFeat->beta->begin();
- it != confFeat->beta->end(); ++it, i++ )
- {
- beta[i] = *it;
- }
- }
- /** simple destructor */
- ConvolutionFeature::~ConvolutionFeature ( )
- {
- }
- /** (re)initialize parameter vector */
- void ConvolutionFeature::initializeParameterVector()
- {
- if (window_size_x > 0 && window_size_y > 0)
- {
- if (isColor)
- numChannels = 3;
- else
- numChannels = 1;
- 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;
- }
- else
- std::cerr << "ConvolutionFeature::initializeVector: Size of window is Zero! Could not initialize..."
- << std::endl;
- }
- bool ConvolutionFeature::isColorMode() const
- {
- return isColor;
- }
- /** 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 ) const
- {
- NICE::Vector vec(betaLength, 1.0);
- NICE::MultiChannelImageT<double> * imgD = NULL;
- imgD = & example->ce->getDChannel( CachedExample::D_EOH );
- int xsize, ysize, x, y;
- example->ce->getImageSize( xsize, ysize );
- x = example->x;
- y = example->y;
- const int halfwsx = std::floor ( window_size_x / 2 );
- const int halfwsy = std::floor ( window_size_y / 2 );
- const int step = window_size_x*window_size_y;
- int k = 1;
- 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 < vec.size() )
- {
- for ( int c = 0; c < numChannels; c++)
- vec[k+c*step] = imgD->get(x+uu,y+vv,c);
- }
- }
- if (useSpatialPriors)
- {
- vec[betaLength-2] = (double)x/(double)xsize;
- vec[betaLength-1] = (double)y/(double)ysize;
- }
- return vec;
- }
- /** return length of parameter vector */
- int ConvolutionFeature::getParameterLength() const
- {
- return betaLength;
- }
- /** set parameter vector */
- 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++ )
- {
- *it = vec[i];
- }
- beta->normalizeL2();
- }
- else
- 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
- {
- double val1 = 0.0;
- // is parameter vector and image data available?
- if (beta == NULL)
- {
- std::cerr << "ConvolutionalFeature::val: Missing parameter vector!"
- << std::endl;
- return val1;
- }
- NICE::MultiChannelImageT<double> * imgD = NULL;
- imgD = & example->ce->getDChannel( CachedExample::D_EOH );
- int xsize, ysize, x, y;
- example->ce->getImageSize( xsize, ysize );
- x = example->x;
- y = example->y;
- const int colorStep = window_size_x*window_size_y;
- const int scalingSteps = 3;
- int halfwsx = std::floor ( window_size_x / 2 );
- int halfwsy = std::floor ( window_size_y / 2 );
- int wScale = 1;
- for ( int s = 0; s < scalingSteps; s++ )
- {
- int k = 1;
- for ( int v = -halfwsy; v <= halfwsy; v+=wScale )
- for ( int u = -halfwsx; u <= halfwsx; u+=wScale, 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++ )
- val1 += imgD->get(x+uu,y+vv,c) * beta->operator [](k+c*colorStep);
- }
- }
- // increase scaling
- halfwsx *= 2;
- halfwsy *= 2;
- wScale *= 2;
- }
- // normalize scaling
- val1 /= (double)scalingSteps;
- if (useSpatialPriors)
- {
- val1 += (double)x/(double)xsize * beta->operator [](betaLength-2);
- val1 += (double)y/(double)ysize * beta->operator [](betaLength-1);
- }
- return val1;
- }
- /** creature feature pool */
- void ConvolutionFeature::explode ( FeaturePool &featurePool, bool variableWindow ) const
- {
- ConvolutionFeature *f =
- new ConvolutionFeature ( this->window_size_x, this->window_size_y, this->isColor );
- featurePool.addFeature(f);
- }
- /** clone current feature */
- Feature *ConvolutionFeature::clone ( ) const
- {
- ConvolutionFeature *f =
- new ConvolutionFeature ( this->window_size_x, this->window_size_y, this->isColor );
- f->setParameterVector( *beta );
- return f;
- }
- Feature *ConvolutionFeature::generateFirstParameter () const
- {
- return clone();
- }
- void ConvolutionFeature::restore ( std::istream & is, int format )
- {
- is >> window_size_x;
- is >> window_size_y;
- is >> betaLength;
- isColor = false;
- useSpatialPriors = false;
- numChannels = 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 );
- for ( NICE::Vector::iterator it = beta->begin();
- it != beta->end(); ++it )
- is >> *it;
- }
- void ConvolutionFeature::store ( std::ostream & os, int format ) const
- {
- os << "ConvolutionFeature "
- << window_size_x << " "
- << window_size_y << " "
- << betaLength;
- for ( NICE::Vector::const_iterator it = beta->begin();
- it != beta->end(); ++it )
- os << ' ' << *it;
- }
- void ConvolutionFeature::clear ()
- {
- beta->clear();
- }
|