ConvolutionFeature.cpp 6.3 KB

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