ConvolutionFeature.cpp 7.0 KB

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