ConvolutionFeature.cpp 7.6 KB

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