ConvolutionFeature.cpp 8.7 KB

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