Example.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. width = 0;
  13. x = 0;
  14. y = 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. _ce->getImageSize ( width, height );
  26. if ( width % 2 == 0 )
  27. width--;
  28. if ( height % 2 == 0 )
  29. height--;
  30. x = width/2;
  31. y = height/2;
  32. ce = _ce;
  33. vec = NULL;
  34. svec = NULL;
  35. weight = 1.0;
  36. position = 0;
  37. }
  38. Example::Example ( CachedExample *_ce,
  39. int _x,
  40. int _y,
  41. double _weight )
  42. {
  43. ce = _ce;
  44. x = _x;
  45. y = _y;
  46. width = 0;
  47. height = 0;
  48. vec = NULL;
  49. svec = NULL;
  50. weight = _weight;
  51. position = 0;
  52. }
  53. Example::Example ( CachedExample *_ce,
  54. int _x,
  55. int _y,
  56. int _width,
  57. int _height,
  58. double _weight )
  59. {
  60. ce = _ce;
  61. x = _x;
  62. y = _y;
  63. width = _width;
  64. height = _height;
  65. assert ( (width > 0) && (height > 0) );
  66. vec = NULL;
  67. svec = NULL;
  68. weight = _weight;
  69. position = 0;
  70. }
  71. Example::Example ( NICE::Vector *_vec,
  72. double _weight )
  73. {
  74. x = y = 0;
  75. width = height = 0;
  76. ce = NULL;
  77. vec = _vec;
  78. svec = NULL;
  79. weight = _weight;
  80. position = 0;
  81. }
  82. void Example::clean ()
  83. {
  84. if ( ce != NULL )
  85. {
  86. delete ce;
  87. ce = NULL;
  88. }
  89. if ( vec != NULL )
  90. {
  91. delete vec;
  92. vec = NULL;
  93. }
  94. if ( svec != NULL )
  95. {
  96. delete svec;
  97. svec = NULL;
  98. }
  99. }
  100. Example::Example ( const Example &ex)
  101. {
  102. copy(ex);
  103. }
  104. void Example::copy ( const Example &ex)
  105. {
  106. vec = ex.vec;
  107. svec = ex.svec;
  108. weight = ex.weight;
  109. position = ex.position;
  110. width = ex.width;
  111. height = ex.height;
  112. x = ex.x;
  113. y = ex.y;
  114. ce = ex.ce;
  115. scale = ex.scale;
  116. }
  117. void Example::restore (istream & is, int format)
  118. {
  119. is >> weight;
  120. is >> x;
  121. is >> y;
  122. is >> width;
  123. is >> height;
  124. is >> position;
  125. int tmp;
  126. is >> tmp;
  127. if(tmp == 1)
  128. {
  129. svec = new SparseVector();
  130. svec->restore(is);
  131. }
  132. else
  133. svec = NULL;
  134. is >> tmp;
  135. if(tmp >= 0 )
  136. {
  137. vec = new Vector(tmp);
  138. for(int i = 0; i < tmp; i++)
  139. {
  140. is >> vec[i];
  141. }
  142. }
  143. else
  144. vec = NULL;
  145. }
  146. void Example::store (ostream & os, int format) const
  147. {
  148. os << weight << " " << x << " " << y << " " << width << " " << height << " " << position << endl;
  149. if(svec == NULL)
  150. os << 0 << endl;
  151. else
  152. {
  153. os << 1 << endl;
  154. svec->store(os);
  155. }
  156. if(vec == NULL)
  157. os << -1 << endl;
  158. else
  159. {
  160. os << vec->size() << endl;
  161. for(int i = 0; i < (int)vec->size(); i++)
  162. {
  163. os << vec[i] << " ";
  164. }
  165. }
  166. }
  167. void Examples::clean ()
  168. {
  169. for ( iterator i = begin(); i != end(); i++ )
  170. {
  171. Example & example = i->second;
  172. example.clean();
  173. }
  174. clear();
  175. }
  176. bool Examples::wrapExamplesAroundFeatureMatrix(const Matrix &p_MatFeaturesColumWiseSamples, const Vector &p_VecLabels, Examples &p_Examples)
  177. {
  178. size_t t_iNumSamples = p_MatFeaturesColumWiseSamples.cols();
  179. size_t t_iNumFeatures = p_MatFeaturesColumWiseSamples.rows();
  180. if(p_VecLabels.size() != t_iNumSamples) // for every columnwise sample there need to be a label
  181. return false;
  182. p_Examples.reserve( t_iNumSamples );
  183. const double *pDataPtr = p_MatFeaturesColumWiseSamples.getDataPointer();
  184. for (size_t i = 0; i < t_iNumSamples; i++, pDataPtr+= t_iNumFeatures )
  185. {
  186. NICE::Vector *t_pVecTrainData = new NICE::Vector( pDataPtr , t_iNumFeatures);
  187. double t_fWeight = 1.0f;
  188. OBJREC::Example t_Example(t_pVecTrainData, t_fWeight);
  189. p_Examples.push_back( std::pair<int, OBJREC::Example>( (int)p_VecLabels[i], t_Example ) );
  190. }
  191. return true;
  192. }
  193. bool Examples::wrapExamplesAroundFeatureMatrix(const Matrix &p_MatFeaturesColumWiseSamples, const VectorT<int> &p_VecLabels, Examples &p_Examples)
  194. {
  195. size_t t_iNumSamples = p_MatFeaturesColumWiseSamples.cols();
  196. size_t t_iNumFeatures = p_MatFeaturesColumWiseSamples.rows();
  197. if(p_VecLabels.size() != t_iNumSamples) // for every columnwise sample there need to be a label
  198. return false;
  199. p_Examples.resize( t_iNumSamples );
  200. const double *pDataPtr = p_MatFeaturesColumWiseSamples.getDataPointer();
  201. #ifdef NICE_USELIB_OPENMP
  202. #pragma omp parallel for default(none) shared(p_VecLabels, p_Examples, t_iNumFeatures, t_iNumSamples, pDataPtr)
  203. #endif
  204. for (size_t i = 0; i < t_iNumSamples; i++)
  205. {
  206. const double *pDataIteration = pDataPtr + (i * t_iNumFeatures);
  207. NICE::Vector *t_pVecTrainData = new NICE::Vector( (double*) pDataIteration , t_iNumFeatures, VectorBase::external);
  208. // OBJREC::Example t_Example(t_pVecTrainData, t_fWeight);
  209. p_Examples[i] = std::pair<int, OBJREC::Example>( p_VecLabels[i], OBJREC::Example(t_pVecTrainData, 1.0) ) ;
  210. }
  211. return true;
  212. }