ConvolutionFeature.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. int uu = u;
  65. int vv = v;
  66. if (x+u < 0 || x+u >= xsize) uu=-u;
  67. if (y+v < 0 || y+v >= ysize) vv=-v;
  68. if ( x+uu > 0
  69. && x+uu < xsize
  70. && y+vv > 0
  71. && y+vv < ysize
  72. && k < vec.size() )
  73. {
  74. vec[k] = img.get(x+uu,y+vv);
  75. }
  76. k++;
  77. }
  78. return vec;
  79. }
  80. /** return length of parameter vector */
  81. int ConvolutionFeature::getParameterLength() const
  82. {
  83. return beta_length;
  84. }
  85. /** set parameter vector */
  86. void ConvolutionFeature::setParameterVector( const Vector & vec )
  87. {
  88. if ( beta->size() == vec.size() )
  89. {
  90. int i = 0;
  91. for ( NICE::Vector::iterator it = beta->begin();
  92. it != beta->end(); ++it, i++ )
  93. {
  94. *it = vec[i];
  95. }
  96. }
  97. else
  98. std::cerr << "ConvolutionFeature::setParameterVector: Vector sizes do not match! Could not update parameter vector..." << std::endl;
  99. if ( beta->Sum() != 1.0 )
  100. beta->normalizeL2();
  101. }
  102. /** return feature value */
  103. double ConvolutionFeature::val ( const Example *example ) const
  104. {
  105. // is parameter vector initialized?
  106. if (beta == NULL)
  107. return 0.0;
  108. const NICE::MultiChannelImageT<int> & img =
  109. example->ce->getIChannel( CachedExample::I_GRAYVALUES );
  110. int xsize, ysize, x, y;
  111. example->ce->getImageSize( xsize, ysize );
  112. x = example->x;
  113. y = example->y;
  114. int halfwsx = std::floor ( window_size_x / 2 );
  115. int halfwsy = std::floor ( window_size_y / 2 );
  116. int k = 0;
  117. double val1 = 0.0;
  118. for ( int v = -halfwsy; v <= halfwsy; v++ )
  119. for ( int u = -halfwsx; u <= halfwsx; u++, k++ )
  120. {
  121. int uu = u;
  122. int vv = v;
  123. if (x+u < 0 || x+u >= xsize) uu=-u;
  124. if (y+v < 0 || y+v >= ysize) vv=-v;
  125. if ( x+uu > 0
  126. && x+uu < xsize
  127. && y+vv > 0
  128. && y+vv < ysize
  129. && k < beta->size() )
  130. {
  131. val1 += (double)img.get(x+uu,y+vv) * beta->operator [](k);
  132. }
  133. }
  134. return std::floor(val1);
  135. }
  136. /** creature feature pool */
  137. void ConvolutionFeature::explode ( FeaturePool &featurePool, bool variableWindow ) const
  138. {
  139. ConvolutionFeature *f = new ConvolutionFeature();
  140. f->window_size_x = window_size_x;
  141. f->window_size_y = window_size_y;
  142. f->initializeParameterVector();
  143. featurePool.addFeature(f);
  144. }
  145. /** clone current feature */
  146. Feature *ConvolutionFeature::clone ( ) const
  147. {
  148. ConvolutionFeature *f = new ConvolutionFeature ();
  149. f->window_size_x = window_size_x;
  150. f->window_size_y = window_size_y;
  151. f->beta = beta;
  152. f->beta_length = beta_length;
  153. return f;
  154. }
  155. Feature *ConvolutionFeature::generateFirstParameter () const
  156. {
  157. return clone();
  158. }
  159. void ConvolutionFeature::restore ( istream & is, int format )
  160. {
  161. is >> window_size_x;
  162. is >> window_size_y;
  163. is >> beta_length;
  164. beta = new NICE::Vector( beta_length, 1.0 );
  165. for ( NICE::Vector::iterator it = beta->begin();
  166. it != beta->end(); ++it )
  167. is >> *it;
  168. }
  169. void ConvolutionFeature::store ( ostream & os, int format ) const
  170. {
  171. os << "ConvolutionFeature "
  172. << window_size_x << " "
  173. << window_size_y << " "
  174. << beta_length;
  175. for ( NICE::Vector::const_iterator it = beta->begin();
  176. it != beta->end(); ++it )
  177. os << ' ' << *it;
  178. }
  179. void ConvolutionFeature::clear ()
  180. {
  181. beta->clear();
  182. }