ConvolutionFeature.cpp 5.9 KB

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