ConvolutionFeature.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /**
  2. * @file ConvolutionFeature.cpp
  3. * @brief convolutional feature
  4. * @author Sven Sickert
  5. * @date 10/13/2008
  6. */
  7. #include <iostream>
  8. #include "ConvolutionFeature.h"
  9. #include "vislearning/cbaselib/FeaturePool.h"
  10. using namespace OBJREC;
  11. using namespace std;
  12. using namespace NICE;
  13. /** simple constructor */
  14. ConvolutionFeature::ConvolutionFeature ( )
  15. {
  16. window_size_x = 15;
  17. window_size_y = 15;
  18. initializeParameterVector();
  19. }
  20. /** default constructor */
  21. ConvolutionFeature::ConvolutionFeature ( const Config *conf )
  22. {
  23. window_size_x = conf->gI ( "ConvolutionFeature", "window_size_x", 15 );
  24. window_size_y = conf->gI ( "ConvolutionFeature", "window_size_y", 15 );
  25. initializeParameterVector();
  26. }
  27. /** simple destructor */
  28. ConvolutionFeature::~ConvolutionFeature ( )
  29. {
  30. }
  31. /** (re)initialize parameter vector */
  32. void ConvolutionFeature::initializeParameterVector()
  33. {
  34. if (window_size_x > 0 && window_size_y > 0)
  35. {
  36. beta_length = window_size_x*window_size_y;
  37. beta = new NICE::Vector( beta_length, (1.0/beta_length) );
  38. }
  39. else
  40. std::cerr << "ConvolutionFeature::initializeVector: Size of window is Zero! Could not initialize..." << std::endl;
  41. }
  42. /** return parameter vector */
  43. NICE::Vector ConvolutionFeature::getParameterVector() const
  44. {
  45. NICE::Vector res = (*this->beta);
  46. return res;
  47. }
  48. /** return feature vector */
  49. NICE::Vector ConvolutionFeature::getFeatureVector( const Example *example )
  50. {
  51. NICE::Vector vec(window_size_x*window_size_y, 0.0);;
  52. const NICE::MultiChannelImageT<int> & img =
  53. example->ce->getIChannel( CachedExample::I_GRAYVALUES );
  54. int xsize, ysize, x, y;
  55. example->ce->getImageSize( xsize, ysize );
  56. x = example->x;
  57. y = example->y;
  58. int halfwsx = std::floor ( window_size_x / 2 );
  59. int halfwsy = std::floor ( window_size_y / 2 );
  60. int k = 0;
  61. for ( int v = -halfwsy; v <= halfwsy; v++ )
  62. for ( int u = -halfwsx; u <= halfwsx; u++ )
  63. {
  64. if ( x+u > 0
  65. && x+u < xsize
  66. && y+v > 0
  67. && y+v < ysize
  68. && k < vec.size() )
  69. {
  70. vec[k] = img.get(x+u,y+v);
  71. }
  72. k++;
  73. }
  74. return vec;
  75. }
  76. /** return length of parameter vector */
  77. int ConvolutionFeature::getParameterLength() const
  78. {
  79. return beta_length;
  80. }
  81. /** set parameter vector */
  82. void ConvolutionFeature::setParameterVector( const Vector & vec )
  83. {
  84. double sum = 0.0;
  85. if ( beta->size() == vec.size() )
  86. {
  87. int i = 0;
  88. for ( NICE::Vector::iterator it = beta->begin();
  89. it != beta->end(); ++it, i++ )
  90. {
  91. *it = vec[i];
  92. sum += vec[i];
  93. }
  94. }
  95. else
  96. std::cerr << "ConvolutionFeature::setParameterVector: Vector sizes do not match! Could not update parameter vector..." << std::endl;
  97. if ( beta->Sum() != 1.0 )
  98. (*beta) /= sum;
  99. }
  100. /** return feature value */
  101. double ConvolutionFeature::val ( const Example *example ) const
  102. {
  103. // is parameter vector initialized?
  104. if (beta == NULL)
  105. return 0.0;
  106. const NICE::MultiChannelImageT<int> & img =
  107. example->ce->getIChannel( CachedExample::I_GRAYVALUES );
  108. int xsize, ysize, x, y;
  109. example->ce->getImageSize( xsize, ysize );
  110. x = example->x;
  111. y = example->y;
  112. int halfwsx = std::floor ( window_size_x / 2 );
  113. int halfwsy = std::floor ( window_size_y / 2 );
  114. int k = 0;
  115. double val1 = 0.0;
  116. for ( int v = -halfwsy; v <= halfwsy; v++ )
  117. for ( int u = -halfwsx; u <= halfwsx; u++, k++ )
  118. {
  119. if ( x+u > 0
  120. && x+u < xsize
  121. && y+v > 0
  122. && y+v < ysize
  123. && k < beta->size() )
  124. {
  125. val1 += (double)img.get(x+u,y+v) * beta->operator [](k);
  126. }
  127. }
  128. return std::floor(val1);
  129. }
  130. /** creature feature pool */
  131. void ConvolutionFeature::explode ( FeaturePool &featurePool, bool variableWindow ) const
  132. {
  133. ConvolutionFeature *f = new ConvolutionFeature();
  134. f->window_size_x = window_size_x;
  135. f->window_size_y = window_size_y;
  136. f->initializeParameterVector();
  137. featurePool.addFeature(f);
  138. }
  139. /** clone current feature */
  140. Feature *ConvolutionFeature::clone ( ) const
  141. {
  142. ConvolutionFeature *f = new ConvolutionFeature ();
  143. f->window_size_x = window_size_x;
  144. f->window_size_y = window_size_y;
  145. f->beta = beta;
  146. f->beta_length = beta_length;
  147. return f;
  148. }
  149. Feature *ConvolutionFeature::generateFirstParameter () const
  150. {
  151. return clone();
  152. }
  153. void ConvolutionFeature::restore ( istream & is, int format )
  154. {
  155. is >> window_size_x;
  156. is >> window_size_y;
  157. is >> beta_length;
  158. beta = new NICE::Vector( beta_length, 1.0 );
  159. for ( NICE::Vector::iterator it = beta->begin();
  160. it != beta->end(); ++it )
  161. is >> *it;
  162. }
  163. void ConvolutionFeature::store ( ostream & os, int format ) const
  164. {
  165. os << "ConvolutionFeature "
  166. << window_size_x << " "
  167. << window_size_y << " "
  168. << beta_length;
  169. for ( NICE::Vector::const_iterator it = beta->begin();
  170. it != beta->end(); ++it )
  171. os << ' ' << *it;
  172. }
  173. void ConvolutionFeature::clear ()
  174. {
  175. beta->clear();
  176. }