ConvolutionFeature.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. NICE::MultiChannelImageT<double> * imgD = NULL;
  90. if (isColor)
  91. imgD = & example->ce->getDChannel( CachedExample::D_EOH );
  92. else
  93. img = & example->ce->getIChannel( CachedExample::I_GRAYVALUES );
  94. int xsize, ysize, x, y;
  95. example->ce->getImageSize( xsize, ysize );
  96. x = example->x;
  97. y = example->y;
  98. int halfwsx = std::floor ( window_size_x / 2 );
  99. int halfwsy = std::floor ( window_size_y / 2 );
  100. int step = window_size_x*window_size_y;
  101. int k = 1;
  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. if (isColor)
  116. {
  117. // using HSV color space
  118. vec[k] = imgD->get(x+uu,y+vv,0);
  119. vec[k+step] = imgD->get(x+uu,y+vv,1);
  120. vec[k+step+step] = imgD->get(x+uu,y+vv,2);
  121. }
  122. else
  123. {
  124. vec[k] = (double)img->get(x+uu,y+vv);
  125. }
  126. }
  127. }
  128. return vec;
  129. }
  130. /** return length of parameter vector */
  131. int ConvolutionFeature::getParameterLength() const
  132. {
  133. return betaLength;
  134. }
  135. /** set parameter vector */
  136. void ConvolutionFeature::setParameterVector( const Vector & vec )
  137. {
  138. if ( beta->size() == vec.size() )
  139. {
  140. int i = 0;
  141. for ( NICE::Vector::iterator it = beta->begin();
  142. it != beta->end(); ++it, i++ )
  143. {
  144. *it = vec[i];
  145. }
  146. beta->normalizeL2();
  147. }
  148. else
  149. std::cerr << "ConvolutionFeature::setParameterVector: Vector sizes do not match!"
  150. << " expected: " << beta->size() << ", got: " << vec.size()
  151. << std::endl;
  152. }
  153. /** return feature value */
  154. double ConvolutionFeature::val ( const Example *example ) const
  155. {
  156. double val1 = 0.0;
  157. // is parameter vector and image data available?
  158. if (beta == NULL)
  159. {
  160. std::cerr << "ConvolutionalFeature::val: Missing parameter vector!"
  161. << std::endl;
  162. return val1;
  163. }
  164. NICE::MultiChannelImageT<int> * img = NULL;
  165. NICE::MultiChannelImageT<double> * imgD = NULL;
  166. if (isColor)
  167. imgD = & example->ce->getDChannel( CachedExample::D_EOH );
  168. else
  169. img = & example->ce->getIChannel( CachedExample::I_GRAYVALUES );
  170. int xsize, ysize, x, y;
  171. example->ce->getImageSize( xsize, ysize );
  172. x = example->x;
  173. y = example->y;
  174. int halfwsx = std::floor ( window_size_x / 2 );
  175. int halfwsy = std::floor ( window_size_y / 2 );
  176. int step = window_size_x*window_size_y;
  177. int k = 1;
  178. for ( int v = -halfwsy; v <= halfwsy; v++ )
  179. for ( int u = -halfwsx; u <= halfwsx; u++, k++ )
  180. {
  181. int uu = u;
  182. int vv = v;
  183. if (x+u < 0 || x+u >= xsize) uu=-u;
  184. if (y+v < 0 || y+v >= ysize) vv=-v;
  185. if ( x+uu > 0
  186. && x+uu < xsize
  187. && y+vv > 0
  188. && y+vv < ysize
  189. && k < beta->size() )
  190. {
  191. if (isColor)
  192. {
  193. // using HSV color space
  194. double h = imgD->get(x+uu,y+vv,0);
  195. double s = imgD->get(x+uu,y+vv,1);
  196. double v = imgD->get(x+uu,y+vv,2);
  197. val1 += h * beta->operator [](k);
  198. val1 += s * beta->operator [](k+step);
  199. val1 += v * beta->operator [](k+step+step);
  200. }
  201. else
  202. {
  203. val1 += (double)img->get(x+uu,y+vv) * beta->operator [](k);
  204. }
  205. }
  206. }
  207. return val1;
  208. }
  209. /** creature feature pool */
  210. void ConvolutionFeature::explode ( FeaturePool &featurePool, bool variableWindow ) const
  211. {
  212. ConvolutionFeature *f =
  213. new ConvolutionFeature ( this->window_size_x, this->window_size_y, this->isColor );
  214. featurePool.addFeature(f);
  215. }
  216. /** clone current feature */
  217. Feature *ConvolutionFeature::clone ( ) const
  218. {
  219. ConvolutionFeature *f =
  220. new ConvolutionFeature ( this->window_size_x, this->window_size_y, this->isColor );
  221. f->setParameterVector( *beta );
  222. return f;
  223. }
  224. Feature *ConvolutionFeature::generateFirstParameter () const
  225. {
  226. return clone();
  227. }
  228. void ConvolutionFeature::restore ( std::istream & is, int format )
  229. {
  230. is >> window_size_x;
  231. is >> window_size_y;
  232. is >> betaLength;
  233. isColor = false;
  234. if ( betaLength > (window_size_x*window_size_y+1) )
  235. isColor = true;
  236. beta = new NICE::Vector( betaLength, 1.0 );
  237. for ( NICE::Vector::iterator it = beta->begin();
  238. it != beta->end(); ++it )
  239. is >> *it;
  240. }
  241. void ConvolutionFeature::store ( std::ostream & os, int format ) const
  242. {
  243. os << "ConvolutionFeature "
  244. << window_size_x << " "
  245. << window_size_y << " "
  246. << betaLength;
  247. for ( NICE::Vector::const_iterator it = beta->begin();
  248. it != beta->end(); ++it )
  249. os << ' ' << *it;
  250. }
  251. void ConvolutionFeature::clear ()
  252. {
  253. beta->clear();
  254. }