ConvolutionFeature.cpp 8.9 KB

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