Example.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. #include "Example.h"
  2. #ifdef NICE_USELIB_OPENMP
  3. #include <omp.h>
  4. #endif
  5. using namespace OBJREC;
  6. using namespace std;
  7. using namespace NICE;
  8. Example::Example()
  9. {
  10. weight = 0.0;
  11. height = 0;
  12. depth = 0;
  13. width = 0;
  14. x = y = z = 0;
  15. vec = NULL;
  16. svec = NULL;
  17. ce = NULL;
  18. position = 0;
  19. }
  20. Example::~Example ()
  21. {
  22. }
  23. Example::Example ( CachedExample *_ce )
  24. {
  25. if ( _ce->getNumImages() > 1 )
  26. {
  27. _ce->getImageSize3 ( width, height, depth );
  28. }
  29. else
  30. {
  31. _ce->getImageSize( width, height );
  32. depth = 1;
  33. }
  34. if ( width % 2 == 0 ) width--;
  35. if ( height % 2 == 0 ) height--;
  36. if ( depth % 2 == 0 ) depth--;
  37. x = width/2;
  38. y = height/2;
  39. z = depth/2;
  40. ce = _ce;
  41. vec = NULL;
  42. svec = NULL;
  43. weight = 1.0;
  44. position = 0;
  45. }
  46. Example::Example ( CachedExample *_ce,
  47. int _x,
  48. int _y,
  49. double _weight )
  50. {
  51. ce = _ce;
  52. x = _x;
  53. y = _y;
  54. z = 0;
  55. width = 0;
  56. height = 0;
  57. depth = 0;
  58. vec = NULL;
  59. svec = NULL;
  60. weight = _weight;
  61. position = 0;
  62. }
  63. Example::Example ( CachedExample *_ce,
  64. int _x,
  65. int _y,
  66. int _z,
  67. double _weight )
  68. {
  69. ce = _ce;
  70. x = _x;
  71. y = _y;
  72. z = _z;
  73. width = 0;
  74. height = 0;
  75. depth = 0;
  76. vec = NULL;
  77. svec = NULL;
  78. weight = _weight;
  79. position = 0;
  80. }
  81. Example::Example ( CachedExample *_ce,
  82. int _x,
  83. int _y,
  84. int _width,
  85. int _height,
  86. double _weight )
  87. {
  88. ce = _ce;
  89. x = _x;
  90. y = _y;
  91. z = 0;
  92. width = _width;
  93. height = _height;
  94. depth = 1;
  95. assert ( (width > 0) && (height > 0) );
  96. vec = NULL;
  97. svec = NULL;
  98. weight = _weight;
  99. position = 0;
  100. }
  101. Example::Example ( CachedExample *_ce,
  102. int _x,
  103. int _y,
  104. int _z,
  105. int _width,
  106. int _height,
  107. int _depth,
  108. double _weight )
  109. {
  110. ce = _ce;
  111. x = _x;
  112. y = _y;
  113. z = _z;
  114. width = _width;
  115. height = _height;
  116. depth = _depth;
  117. assert ( (width > 0) && (height > 0) && assert(depth > 0) );
  118. vec = NULL;
  119. svec = NULL;
  120. weight = _weight;
  121. position = 0;
  122. }
  123. Example::Example ( NICE::Vector *_vec,
  124. double _weight )
  125. {
  126. x = y = z = 0;
  127. width = height = depth = 0;
  128. ce = NULL;
  129. vec = _vec;
  130. svec = NULL;
  131. weight = _weight;
  132. position = 0;
  133. }
  134. void Example::clean ()
  135. {
  136. if ( ce != NULL )
  137. {
  138. delete ce;
  139. ce = NULL;
  140. }
  141. if ( vec != NULL )
  142. {
  143. delete vec;
  144. vec = NULL;
  145. }
  146. if ( svec != NULL )
  147. {
  148. delete svec;
  149. svec = NULL;
  150. }
  151. }
  152. Example::Example ( const Example &ex)
  153. {
  154. copy(ex);
  155. }
  156. void Example::copy ( const Example &ex)
  157. {
  158. vec = ex.vec;
  159. svec = ex.svec;
  160. weight = ex.weight;
  161. position = ex.position;
  162. width = ex.width;
  163. height = ex.height;
  164. depth = ex.depth;
  165. x = ex.x;
  166. y = ex.y;
  167. z = ex.z;
  168. ce = ex.ce;
  169. scale = ex.scale;
  170. }
  171. void Example::restore (istream & is, int format)
  172. {
  173. if ( format == 1 )
  174. {
  175. is >> weight;
  176. is >> x;
  177. is >> y;
  178. is >> z;
  179. is >> width;
  180. is >> height;
  181. is >> depth;
  182. is >> position;
  183. }
  184. else
  185. {
  186. is >> weight;
  187. is >> x;
  188. is >> y;
  189. is >> width;
  190. is >> height;
  191. is >> position;
  192. depth = 1;
  193. z = 0;
  194. }
  195. int tmp;
  196. is >> tmp;
  197. if(tmp == 1)
  198. {
  199. svec = new SparseVector();
  200. svec->restore(is);
  201. }
  202. else
  203. svec = NULL;
  204. is >> tmp;
  205. if(tmp >= 0 )
  206. {
  207. vec = new Vector(tmp);
  208. for(int i = 0; i < tmp; i++)
  209. {
  210. is >> vec[i];
  211. }
  212. }
  213. else
  214. vec = NULL;
  215. }
  216. void Example::store (ostream & os, int format) const
  217. {
  218. if ( format == 1 )
  219. os << weight << " " << x << " " << y << " " << z << " " << width << " " << height << " " << depth << " " << position << endl;
  220. else
  221. os << weight << " " << x << " " << y << " " << width << " " << height << " " << position << endl;
  222. if( svec == NULL )
  223. os << 0 << endl;
  224. else
  225. {
  226. os << 1 << endl;
  227. svec->store(os);
  228. }
  229. if(vec == NULL)
  230. os << -1 << endl;
  231. else
  232. {
  233. os << vec->size() << endl;
  234. for(int i = 0; i < (int)vec->size(); i++)
  235. {
  236. os << vec[i] << " ";
  237. }
  238. }
  239. }
  240. void Examples::clean ()
  241. {
  242. for ( iterator i = begin(); i != end(); i++ )
  243. {
  244. Example & example = i->second;
  245. example.clean();
  246. }
  247. clear();
  248. }
  249. bool Examples::wrapExamplesAroundFeatureMatrix(const Matrix &p_MatFeaturesColumWiseSamples, const Vector &p_VecLabels, Examples &p_Examples)
  250. {
  251. size_t t_iNumSamples = p_MatFeaturesColumWiseSamples.cols();
  252. size_t t_iNumFeatures = p_MatFeaturesColumWiseSamples.rows();
  253. if(p_VecLabels.size() != t_iNumSamples) // for every columnwise sample there need to be a label
  254. return false;
  255. p_Examples.reserve( t_iNumSamples );
  256. const double *pDataPtr = p_MatFeaturesColumWiseSamples.getDataPointer();
  257. for (size_t i = 0; i < t_iNumSamples; i++, pDataPtr+= t_iNumFeatures )
  258. {
  259. NICE::Vector *t_pVecTrainData = new NICE::Vector( pDataPtr , t_iNumFeatures);
  260. double t_fWeight = 1.0f;
  261. OBJREC::Example t_Example(t_pVecTrainData, t_fWeight);
  262. p_Examples.push_back( std::pair<int, OBJREC::Example>( (int)p_VecLabels[i], t_Example ) );
  263. }
  264. return true;
  265. }
  266. bool Examples::wrapExamplesAroundFeatureMatrix(const Matrix &p_MatFeaturesColumWiseSamples, const VectorT<int> &p_VecLabels, Examples &p_Examples)
  267. {
  268. size_t t_iNumSamples = p_MatFeaturesColumWiseSamples.cols();
  269. size_t t_iNumFeatures = p_MatFeaturesColumWiseSamples.rows();
  270. if(p_VecLabels.size() != t_iNumSamples) // for every columnwise sample there need to be a label
  271. return false;
  272. p_Examples.resize( t_iNumSamples );
  273. const double *pDataPtr = p_MatFeaturesColumWiseSamples.getDataPointer();
  274. #ifdef NICE_USELIB_OPENMP
  275. #pragma omp parallel for default(none) shared(p_VecLabels, p_Examples, t_iNumFeatures, t_iNumSamples, pDataPtr)
  276. #endif
  277. for (size_t i = 0; i < t_iNumSamples; i++)
  278. {
  279. const double *pDataIteration = pDataPtr + (i * t_iNumFeatures);
  280. NICE::Vector *t_pVecTrainData = new NICE::Vector( (double*) pDataIteration , t_iNumFeatures, VectorBase::external);
  281. // OBJREC::Example t_Example(t_pVecTrainData, t_fWeight);
  282. p_Examples[i] = std::pair<int, OBJREC::Example>( p_VecLabels[i], OBJREC::Example(t_pVecTrainData, 1.0) ) ;
  283. }
  284. return true;
  285. }