ConvolutionFeature.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. void ConvolutionFeature::getFeatureVector(
  101. const Example *example,
  102. NICE::Vector & vec ) const
  103. {
  104. NICE::MultiChannelImageT<double> * imgD = NULL;
  105. imgD = & example->ce->getDChannel( CachedExample::D_EOH );
  106. double** data = imgD->getDataPointer();
  107. int xsize, ysize;
  108. example->ce->getImageSize( xsize, ysize );
  109. const int x = example->x;
  110. const int y = example->y;
  111. const int halfwsx = std::floor ( window_size_x / 2 );
  112. const int halfwsy = std::floor ( window_size_y / 2 );
  113. //const int step = window_size_x*window_size_y;
  114. int k = 1;
  115. for ( int c = 0; c < numChannels; c++)
  116. for ( int v = -halfwsy; v <= halfwsy; v++ )
  117. for ( int u = -halfwsx; u <= halfwsx; u++, k++ )
  118. {
  119. int uu = u;
  120. int vv = v;
  121. if (x+u < 0 || x+u >= xsize) uu=-u;
  122. if (y+v < 0 || y+v >= ysize) vv=-v;
  123. //vec[k] = imgD->get(x+uu,y+vv,c);
  124. vec[k] = data[c][(x+uu)+(y+vv)*xsize];
  125. }
  126. if (useSpatialPriors)
  127. {
  128. vec[paramsLength-2] = (double)x/(double)xsize;
  129. vec[paramsLength-1] = (double)y/(double)ysize;
  130. }
  131. }
  132. /** return length of parameter vector */
  133. int ConvolutionFeature::getParameterLength() const
  134. {
  135. return paramsLength;
  136. }
  137. void ConvolutionFeature::setRandomParameterVector ( )
  138. {
  139. srand (time(NULL));
  140. for ( NICE::Vector::iterator it = params->begin();
  141. it != params->end(); ++it )
  142. {
  143. double b = (double) rand() / (double) RAND_MAX;
  144. *it = b;
  145. }
  146. params->normalizeL2();
  147. }
  148. /** set parameter vector */
  149. void ConvolutionFeature::setParameterVector( const Vector & vec )
  150. {
  151. if ( params->size() == vec.size() )
  152. {
  153. int i = 0;
  154. for ( NICE::Vector::iterator it = params->begin();
  155. it != params->end(); ++it, i++ )
  156. {
  157. *it = vec[i];
  158. }
  159. params->normalizeL2();
  160. }
  161. else
  162. std::cerr << "ConvolutionFeature::setParameterVector: Vector sizes do not match!"
  163. << " expected: " << params->size() << ", got: " << vec.size()
  164. << std::endl;
  165. }
  166. /** return feature value */
  167. double ConvolutionFeature::val ( const Example *example ) const
  168. {
  169. double val1 = 0.0;
  170. // is parameter vector and image data available?
  171. if (params == NULL)
  172. {
  173. std::cerr << "ConvolutionalFeature::val: Missing parameter vector!"
  174. << std::endl;
  175. return val1;
  176. }
  177. NICE::Vector featVec (paramsLength, 1.0);
  178. getFeatureVector ( example, featVec );
  179. // for ( int i = 0; i < featVec.size(); i++ )
  180. // val1 += featVec[i] * params->operator [](i);
  181. val1 = params->scalarProduct ( featVec );
  182. return val1;
  183. }
  184. /** creature feature pool */
  185. void ConvolutionFeature::explode ( FeaturePool &featurePool, bool variableWindow ) const
  186. {
  187. ConvolutionFeature *f = new ConvolutionFeature (
  188. this->window_size_x,
  189. this->window_size_y,
  190. this->isColor,
  191. this->useSpatialPriors );
  192. featurePool.addFeature(f);
  193. }
  194. /** clone current feature */
  195. Feature *ConvolutionFeature::clone ( ) const
  196. {
  197. ConvolutionFeature *f = new ConvolutionFeature (
  198. this->window_size_x,
  199. this->window_size_y,
  200. this->isColor,
  201. this->useSpatialPriors );
  202. f->setParameterVector( *params );
  203. return f;
  204. }
  205. Feature *ConvolutionFeature::generateFirstParameter () const
  206. {
  207. return clone();
  208. }
  209. void ConvolutionFeature::restore ( std::istream & is, int format )
  210. {
  211. is >> window_size_x;
  212. is >> window_size_y;
  213. is >> paramsLength;
  214. isColor = false;
  215. useSpatialPriors = false;
  216. numChannels = 1;
  217. if ( paramsLength == (window_size_x*window_size_y+3) )
  218. {
  219. useSpatialPriors = true;
  220. }
  221. else if ( paramsLength == (3*window_size_x*window_size_y+1) )
  222. {
  223. isColor = true;
  224. numChannels = 3;
  225. }
  226. else if ( paramsLength == (3*window_size_x*window_size_y+3) )
  227. {
  228. isColor = true;
  229. numChannels = 3;
  230. useSpatialPriors = true;
  231. }
  232. params = new NICE::Vector( paramsLength, 1.0 );
  233. for ( NICE::Vector::iterator it = params->begin();
  234. it != params->end(); ++it )
  235. is >> *it;
  236. }
  237. void ConvolutionFeature::store ( std::ostream & os, int format ) const
  238. {
  239. os << "ConvolutionFeature "
  240. << window_size_x << " "
  241. << window_size_y << " "
  242. << paramsLength;
  243. for ( NICE::Vector::const_iterator it = params->begin();
  244. it != params->end(); ++it )
  245. os << ' ' << *it;
  246. }
  247. void ConvolutionFeature::clear ()
  248. {
  249. params->clear();
  250. }