ConvolutionFeature.cpp 8.4 KB

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