ConvolutionFeature.cpp 7.9 KB

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