ConvolutionFeature.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. beta = new NICE::Vector( betaLength, 0.0 );
  51. int i = 0;
  52. for ( NICE::Vector::iterator it = confFeat->beta->begin();
  53. it != confFeat->beta->end(); ++it, i++ )
  54. {
  55. beta[i] = *it;
  56. }
  57. }
  58. /** simple destructor */
  59. ConvolutionFeature::~ConvolutionFeature ( )
  60. {
  61. }
  62. /** (re)initialize parameter vector */
  63. void ConvolutionFeature::initializeParameterVector()
  64. {
  65. if (window_size_x > 0 && window_size_y > 0)
  66. {
  67. if (isColor)
  68. betaLength = 3*window_size_x*window_size_y + 1;
  69. else
  70. betaLength = window_size_x*window_size_y + 1;
  71. beta = new NICE::Vector( betaLength, (1.0/(double)(betaLength-1) ) );
  72. beta[0] = 1;
  73. }
  74. else
  75. std::cerr << "ConvolutionFeature::initializeVector: Size of window is Zero! Could not initialize..."
  76. << std::endl;
  77. }
  78. /** return parameter vector */
  79. NICE::Vector ConvolutionFeature::getParameterVector() const
  80. {
  81. NICE::Vector res = (*this->beta);
  82. return res;
  83. }
  84. /** return feature vector */
  85. NICE::Vector ConvolutionFeature::getFeatureVector( const Example *example ) const
  86. {
  87. NICE::Vector vec(betaLength, 1.0);
  88. NICE::MultiChannelImageT<int> * img = NULL;
  89. if (isColor)
  90. img = & example->ce->getIChannel( CachedExample::I_COLOR );
  91. else
  92. img = & example->ce->getIChannel( CachedExample::I_GRAYVALUES );
  93. int xsize, ysize, x, y;
  94. example->ce->getImageSize( xsize, ysize );
  95. x = example->x;
  96. y = example->y;
  97. int halfwsx = std::floor ( window_size_x / 2 );
  98. int halfwsy = std::floor ( window_size_y / 2 );
  99. int numChannels = img->channels();
  100. int k = 1;
  101. for ( int c = 0; c < numChannels; c++ )
  102. for ( int v = -halfwsy; v <= halfwsy; v++ )
  103. for ( int u = -halfwsx; u <= halfwsx; u++, k++ )
  104. {
  105. int uu = u;
  106. int vv = v;
  107. if (x+u < 0 || x+u >= xsize) uu=-u;
  108. if (y+v < 0 || y+v >= ysize) vv=-v;
  109. if ( x+uu > 0
  110. && x+uu < xsize
  111. && y+vv > 0
  112. && y+vv < ysize
  113. && k < vec.size() )
  114. {
  115. vec[k] = img->get(x+uu,y+vv,c);
  116. }
  117. }
  118. return vec;
  119. }
  120. /** return length of parameter vector */
  121. int ConvolutionFeature::getParameterLength() const
  122. {
  123. return betaLength;
  124. }
  125. /** set parameter vector */
  126. void ConvolutionFeature::setParameterVector( const Vector & vec )
  127. {
  128. if ( beta->size() == vec.size() )
  129. {
  130. int i = 0;
  131. for ( NICE::Vector::iterator it = beta->begin();
  132. it != beta->end(); ++it, i++ )
  133. {
  134. *it = vec[i];
  135. }
  136. beta->normalizeL2();
  137. }
  138. else
  139. std::cerr << "ConvolutionFeature::setParameterVector: Vector sizes do not match!"
  140. << " expected: " << beta->size() << ", got: " << vec.size()
  141. << std::endl;
  142. }
  143. /** return feature value */
  144. double ConvolutionFeature::val ( const Example *example ) const
  145. {
  146. double val1 = 0.0;
  147. // is parameter vector and image data available?
  148. if (beta == NULL)
  149. {
  150. std::cerr << "ConvolutionalFeature::val: Missing parameter vector!"
  151. << std::endl;
  152. return val1;
  153. }
  154. NICE::MultiChannelImageT<int> * img = NULL;
  155. if (isColor)
  156. img = & example->ce->getIChannel( CachedExample::I_COLOR );
  157. else
  158. img = & example->ce->getIChannel( CachedExample::I_GRAYVALUES );
  159. int xsize, ysize, x, y;
  160. example->ce->getImageSize( xsize, ysize );
  161. x = example->x;
  162. y = example->y;
  163. int halfwsx = std::floor ( window_size_x / 2 );
  164. int halfwsy = std::floor ( window_size_y / 2 );
  165. int numChannels = img->channels();
  166. int k = 1;
  167. for ( int c = 0; c < numChannels; c++ )
  168. for ( int v = -halfwsy; v <= halfwsy; v++ )
  169. for ( int u = -halfwsx; u <= halfwsx; u++, k++ )
  170. {
  171. int uu = u;
  172. int vv = v;
  173. if (x+u < 0 || x+u >= xsize) uu=-u;
  174. if (y+v < 0 || y+v >= ysize) vv=-v;
  175. if ( x+uu > 0
  176. && x+uu < xsize
  177. && y+vv > 0
  178. && y+vv < ysize
  179. && k < beta->size() )
  180. {
  181. val1 += (double)img->get(x+uu,y+vv,c) * beta->operator [](k);
  182. }
  183. }
  184. return val1;
  185. }
  186. /** creature feature pool */
  187. void ConvolutionFeature::explode ( FeaturePool &featurePool, bool variableWindow ) const
  188. {
  189. ConvolutionFeature *f =
  190. new ConvolutionFeature ( this->window_size_x, this->window_size_y, this->isColor );
  191. featurePool.addFeature(f);
  192. }
  193. /** clone current feature */
  194. Feature *ConvolutionFeature::clone ( ) const
  195. {
  196. ConvolutionFeature *f =
  197. new ConvolutionFeature ( this->window_size_x, this->window_size_y, this->isColor );
  198. f->setParameterVector( *beta );
  199. return f;
  200. }
  201. Feature *ConvolutionFeature::generateFirstParameter () const
  202. {
  203. return clone();
  204. }
  205. void ConvolutionFeature::restore ( std::istream & is, int format )
  206. {
  207. is >> window_size_x;
  208. is >> window_size_y;
  209. is >> betaLength;
  210. isColor = false;
  211. if ( betaLength > (window_size_x*window_size_y+1) )
  212. isColor = true;
  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. }