Example.cpp 3.5 KB

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