ConvolutionFeature.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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 = 1;
  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. for ( int c = 0; c < numChannels; c++ )
  181. {
  182. int paramIdx = 1;
  183. int pxIdx = 0;
  184. for ( int v = -halfwsy; v <= halfwsy; v++ )
  185. for ( int u = -halfwsx; u <= halfwsx; u++, pxIdx++ )
  186. {
  187. if (pxIdx % wScale == 0) paramIdx++;
  188. int colorShift = paramIdx + c*colorStep;
  189. int uu = u;
  190. int vv = v;
  191. if (x+u < 0 || x+u >= xsize) uu=-u;
  192. if (y+v < 0 || y+v >= ysize) vv=-v;
  193. if ( x+uu > 0
  194. && x+uu < xsize
  195. && y+vv > 0
  196. && y+vv < ysize
  197. && paramIdx < beta->size() )
  198. {
  199. val1 += imgD->get(x+uu,y+vv,c)
  200. * beta->operator [](colorShift)
  201. / wScale * wScale;
  202. }
  203. }
  204. }
  205. // increase scaling
  206. halfwsx *= 3;
  207. halfwsy *= 3;
  208. wScale *= 3;
  209. }
  210. // normalize scaling
  211. val1 /= (double)scalingSteps;
  212. if (useSpatialPriors)
  213. {
  214. val1 += (double)x/(double)xsize * beta->operator [](betaLength-2);
  215. val1 += (double)y/(double)ysize * beta->operator [](betaLength-1);
  216. }
  217. return val1;
  218. }
  219. /** creature feature pool */
  220. void ConvolutionFeature::explode ( FeaturePool &featurePool, bool variableWindow ) const
  221. {
  222. ConvolutionFeature *f =
  223. new ConvolutionFeature ( this->window_size_x, this->window_size_y, this->isColor );
  224. featurePool.addFeature(f);
  225. }
  226. /** clone current feature */
  227. Feature *ConvolutionFeature::clone ( ) const
  228. {
  229. ConvolutionFeature *f =
  230. new ConvolutionFeature ( this->window_size_x, this->window_size_y, this->isColor );
  231. f->setParameterVector( *beta );
  232. return f;
  233. }
  234. Feature *ConvolutionFeature::generateFirstParameter () const
  235. {
  236. return clone();
  237. }
  238. void ConvolutionFeature::restore ( std::istream & is, int format )
  239. {
  240. is >> window_size_x;
  241. is >> window_size_y;
  242. is >> betaLength;
  243. isColor = false;
  244. useSpatialPriors = false;
  245. numChannels = 1;
  246. if ( betaLength == (window_size_x*window_size_y+3) )
  247. {
  248. useSpatialPriors = true;
  249. }
  250. else if ( betaLength == (3*window_size_x*window_size_y+1) )
  251. {
  252. isColor = true;
  253. numChannels = 3;
  254. }
  255. else if ( betaLength == (3*window_size_x*window_size_y+3) )
  256. {
  257. isColor = true;
  258. numChannels = 3;
  259. useSpatialPriors = true;
  260. }
  261. beta = new NICE::Vector( betaLength, 1.0 );
  262. for ( NICE::Vector::iterator it = beta->begin();
  263. it != beta->end(); ++it )
  264. is >> *it;
  265. }
  266. void ConvolutionFeature::store ( std::ostream & os, int format ) const
  267. {
  268. os << "ConvolutionFeature "
  269. << window_size_x << " "
  270. << window_size_y << " "
  271. << betaLength;
  272. for ( NICE::Vector::const_iterator it = beta->begin();
  273. it != beta->end(); ++it )
  274. os << ' ' << *it;
  275. }
  276. void ConvolutionFeature::clear ()
  277. {
  278. beta->clear();
  279. }