ConvolutionFeature.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. }
  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. paramsLength = numChannels*window_size_x*window_size_y + 1;
  79. if (useSpatialPriors) paramsLength += 2;
  80. params = new NICE::Vector( paramsLength, (1.0/(double)(paramsLength-1) ) );
  81. params[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->params);
  95. return res;
  96. }
  97. /** return feature vector */
  98. NICE::Vector ConvolutionFeature::getFeatureVector( const Example *example ) const
  99. {
  100. NICE::Vector vec(paramsLength, 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[paramsLength-2] = (double)x/(double)xsize;
  131. vec[paramsLength-1] = (double)y/(double)ysize;
  132. }
  133. return vec;
  134. }
  135. /** return length of parameter vector */
  136. int ConvolutionFeature::getParameterLength() const
  137. {
  138. return paramsLength;
  139. }
  140. void ConvolutionFeature::setRandomParameterVector ( )
  141. {
  142. srand (time(NULL));
  143. for ( NICE::Vector::iterator it = params->begin();
  144. it != params->end(); ++it )
  145. {
  146. double b = (double) rand() / (double) RAND_MAX;
  147. *it = b;
  148. }
  149. params->normalizeL2();
  150. }
  151. /** set parameter vector */
  152. void ConvolutionFeature::setParameterVector( const Vector & vec )
  153. {
  154. if ( params->size() == vec.size() )
  155. {
  156. int i = 0;
  157. for ( NICE::Vector::iterator it = params->begin();
  158. it != params->end(); ++it, i++ )
  159. {
  160. *it = vec[i];
  161. }
  162. params->normalizeL2();
  163. }
  164. else
  165. std::cerr << "ConvolutionFeature::setParameterVector: Vector sizes do not match!"
  166. << " expected: " << params->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 (params == NULL)
  175. {
  176. std::cerr << "ConvolutionalFeature::val: Missing parameter vector!"
  177. << std::endl;
  178. return val1;
  179. }
  180. NICE::Vector featVec = getFeatureVector ( example );
  181. // for ( int i = 0; i < featVec.size(); i++ )
  182. // val1 += featVec[i] * params->operator [](i);
  183. val1 = params->scalarProduct ( featVec );
  184. return val1;
  185. }
  186. /** creature feature pool */
  187. void ConvolutionFeature::explode ( FeaturePool &featurePool, bool variableWindow ) const
  188. {
  189. ConvolutionFeature *f = new ConvolutionFeature (
  190. this->window_size_x,
  191. this->window_size_y,
  192. this->isColor,
  193. this->useSpatialPriors );
  194. featurePool.addFeature(f);
  195. }
  196. /** clone current feature */
  197. Feature *ConvolutionFeature::clone ( ) const
  198. {
  199. ConvolutionFeature *f = new ConvolutionFeature (
  200. this->window_size_x,
  201. this->window_size_y,
  202. this->isColor,
  203. this->useSpatialPriors );
  204. f->setParameterVector( *params );
  205. return f;
  206. }
  207. Feature *ConvolutionFeature::generateFirstParameter () const
  208. {
  209. return clone();
  210. }
  211. void ConvolutionFeature::restore ( std::istream & is, int format )
  212. {
  213. is >> window_size_x;
  214. is >> window_size_y;
  215. is >> paramsLength;
  216. isColor = false;
  217. useSpatialPriors = false;
  218. numChannels = 1;
  219. if ( paramsLength == (window_size_x*window_size_y+3) )
  220. {
  221. useSpatialPriors = true;
  222. }
  223. else if ( paramsLength == (3*window_size_x*window_size_y+1) )
  224. {
  225. isColor = true;
  226. numChannels = 3;
  227. }
  228. else if ( paramsLength == (3*window_size_x*window_size_y+3) )
  229. {
  230. isColor = true;
  231. numChannels = 3;
  232. useSpatialPriors = true;
  233. }
  234. params = new NICE::Vector( paramsLength, 1.0 );
  235. for ( NICE::Vector::iterator it = params->begin();
  236. it != params->end(); ++it )
  237. is >> *it;
  238. }
  239. void ConvolutionFeature::store ( std::ostream & os, int format ) const
  240. {
  241. os << "ConvolutionFeature "
  242. << window_size_x << " "
  243. << window_size_y << " "
  244. << paramsLength;
  245. for ( NICE::Vector::const_iterator it = params->begin();
  246. it != params->end(); ++it )
  247. os << ' ' << *it;
  248. }
  249. void ConvolutionFeature::clear ()
  250. {
  251. params->clear();
  252. }