ConvolutionFeature.cpp 8.3 KB

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