FPCGPHIK.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /**
  2. * @file FPCGPHIK.cpp
  3. * @brief feature pool interface for our GP HIK classifier
  4. * @author Alexander Freytag
  5. * @date 02/01/2012
  6. */
  7. #include <iostream>
  8. #include "core/basics/numerictools.h"
  9. #include <core/basics/Timer.h>
  10. #include "FPCGPHIK.h"
  11. using namespace std;
  12. using namespace NICE;
  13. using namespace OBJREC;
  14. FPCGPHIK::FPCGPHIK( const Config *conf, const string & confSection )
  15. {
  16. this->verbose = conf->gB(confSection, "verbose", false);
  17. this->useSimpleBalancing = conf->gB(confSection, "use_simple_balancing", false);
  18. this->minSamples = conf->gI(confSection, "min_samples", -1);
  19. this->performOptimizationAfterIncrement = conf->gB(confSection, "performOptimizationAfterIncrement", true);
  20. classifier = new GPHIKClassifier(conf, confSection);
  21. }
  22. FPCGPHIK::~FPCGPHIK()
  23. {
  24. if ( classifier != NULL )
  25. delete classifier;
  26. }
  27. ClassificationResult FPCGPHIK::classify ( Example & pe )
  28. {
  29. NICE::SparseVector *svec;// = pe.svec;
  30. // was only a NICE::Vector given?
  31. // Than we had to allocate a new NICE::SparseVector and converted the given NICE::Vector into it.
  32. bool newvec = false;
  33. if ( pe.svec != NULL )
  34. {
  35. svec = pe.svec;
  36. }
  37. else
  38. {
  39. NICE::Vector x;
  40. x = * ( pe.vec );
  41. svec = new NICE::SparseVector ( x );
  42. svec->setDim ( x.size() );
  43. newvec = true;
  44. }
  45. ClassificationResult result ( this->classify( svec ) );
  46. if ( newvec )
  47. delete svec;
  48. return result;
  49. }
  50. ClassificationResult FPCGPHIK::classify ( const NICE::SparseVector * example )
  51. {
  52. NICE::SparseVector scores;
  53. int result;
  54. double uncertainty;
  55. classifier->classify ( example, result, scores, uncertainty);
  56. if ( scores.size() == 0 ) {
  57. fthrow(Exception, "Zero scores, something is likely to be wrong here: svec.size() = " << example->size() );
  58. }
  59. int classes = scores.getDim();
  60. FullVector fvscores(classes);
  61. NICE::SparseVector::const_iterator it;
  62. for(int c = 0; c < classes; c++)
  63. {
  64. it = scores.find(c);
  65. if ( it == scores.end() )
  66. fvscores[c] = -std::numeric_limits<double>::max();
  67. else
  68. fvscores[c] = it->second;
  69. }
  70. ClassificationResult r ( fvscores.maxElement(), fvscores );
  71. r.uncertainty = uncertainty;
  72. if (verbose)
  73. {
  74. std::cerr << " FPCGPHIK::classify scores" << std::endl;
  75. scores.store(std::cerr);
  76. std::cerr << " FPCGPHIK::classify fvscores" << std::endl;
  77. fvscores.store(std::cerr);
  78. }
  79. return r;
  80. }
  81. /** training process */
  82. void FPCGPHIK::train ( FeaturePool & fp, Examples & examples )
  83. {
  84. // we completely ignore the feature pool :)
  85. //
  86. initRand(0);
  87. Vector classCounts;
  88. int minClass = -1;
  89. if (verbose)
  90. std::cerr << "FPCGPHIK::train" << std::endl;
  91. if ( useSimpleBalancing)
  92. {
  93. classCounts.resize( examples.getMaxClassNo()+1 );
  94. classCounts.set( 0.0 );
  95. for ( uint i = 0 ; i < examples.size() ; i++ )
  96. classCounts[ examples[i].first ]++;
  97. // we need a probability distribution
  98. //classCounts.normalizeL1();
  99. // we need the class index of the class with the least non-zero examples
  100. for ( uint i = 0 ; i < classCounts.size(); i++ )
  101. if ( (classCounts[i] > 0) && ((minClass < 0) || (classCounts[i] < classCounts[minClass])) )
  102. minClass = i;
  103. if (verbose)
  104. {
  105. cerr << "Class distribution: " << classCounts << endl;
  106. cerr << "Class with the least number of examples: " << minClass << endl;
  107. }
  108. if(minSamples < 0)
  109. minSamples = classCounts[minClass];
  110. }
  111. // (multi-class) label vector
  112. Vector y ( examples.size() /* maximum size */ );
  113. // flat structure of our training data
  114. std::vector< SparseVector * > sparseExamples;
  115. if (verbose)
  116. cerr << "Converting (and sampling) feature vectors" << endl;
  117. for ( uint i = 0 ; i < examples.size() ; i++ )
  118. {
  119. const Example & example = examples[i].second;
  120. int classno = examples[i].first;
  121. // simple weird balancing method
  122. if ( useSimpleBalancing )
  123. {
  124. double t = randDouble() * classCounts[classno];
  125. if ( t >= minSamples ) continue;
  126. }
  127. y[ sparseExamples.size() ] = classno;
  128. if ( example.svec == NULL )
  129. fthrow(Exception, "FPCGPHIK requires example.svec (SparseVector stored in an Example struct)");
  130. sparseExamples.push_back( example.svec );
  131. }
  132. // we only use a subset for training
  133. y.resize( sparseExamples.size() );
  134. classifier->train(sparseExamples, y);
  135. }
  136. /** training process */
  137. void FPCGPHIK::train ( const std::vector< SparseVector *> & examples, std::map<int, NICE::Vector> & binLabels )
  138. {
  139. std::cerr << "call internal train method " << std::endl;
  140. classifier->train(examples, binLabels);
  141. std::cerr << "training done" << std::endl;
  142. }
  143. void FPCGPHIK::clear ()
  144. {
  145. if ( classifier != NULL )
  146. delete classifier;
  147. classifier = NULL;
  148. }
  149. FeaturePoolClassifier *FPCGPHIK::clone () const
  150. {
  151. fthrow(Exception, "FPCGPHIK: clone() not yet implemented" );
  152. return NULL;
  153. }
  154. void FPCGPHIK::predictUncertainty( Example & pe, NICE::Vector & uncertainties )
  155. {
  156. const SparseVector *svec = pe.svec;
  157. if ( svec == NULL )
  158. fthrow(Exception, "FPCGPHIK requires example.svec (SparseVector stored in an Example struct)");
  159. classifier->predictUncertainty(svec, uncertainties);
  160. }
  161. void FPCGPHIK::predictUncertainty( const NICE::SparseVector * example, NICE::Vector & uncertainties )
  162. {
  163. classifier->predictUncertainty(example, uncertainties);
  164. }
  165. //---------------------------------------------------------------------
  166. // protected methods
  167. //---------------------------------------------------------------------
  168. void FPCGPHIK::restore ( std::istream & is, int format )
  169. {
  170. if (is.good())
  171. {
  172. classifier->restore(is, format);
  173. std::string tmp;
  174. is >> tmp; //"performOptimizationAfterIncrement: "
  175. is >> this->performOptimizationAfterIncrement;
  176. }
  177. else
  178. {
  179. std::cerr << "FPCGPHIK::restore -- InStream not initialized - restoring not possible!" << std::endl;
  180. }
  181. }
  182. void FPCGPHIK::store ( std::ostream & os, int format ) const
  183. {
  184. if (os.good())
  185. {
  186. os.precision (numeric_limits<double>::digits10 + 1);
  187. classifier->store(os, format);
  188. os << "performOptimizationAfterIncrement: " << performOptimizationAfterIncrement << std::endl;
  189. }
  190. else
  191. {
  192. std::cerr << "OutStream not initialized - storing not possible!" << std::endl;
  193. }
  194. }
  195. void FPCGPHIK::addExample( const Example & pe, const double & label)
  196. {
  197. const SparseVector *svec = pe.svec;
  198. classifier->addExample(svec, label, this->performOptimizationAfterIncrement);
  199. }
  200. void FPCGPHIK::addMultipleExamples( Examples & newExamples)
  201. {
  202. //are new examples available? If not, nothing has to be done
  203. if ( newExamples.size() < 1)
  204. return;
  205. // (multi-class) label vector
  206. Vector y ( newExamples.size() );
  207. // flat structure of our training data
  208. std::vector< const SparseVector * > sparseExamples;
  209. if (verbose)
  210. cerr << "Converting (and sampling) feature vectors" << endl;
  211. for ( uint i = 0 ; i < newExamples.size() ; i++ )
  212. {
  213. const Example & example = newExamples[i].second;
  214. int classno = newExamples[i].first;
  215. y[ i ] = classno;
  216. if ( example.svec == NULL )
  217. fthrow(Exception, "FPCGPHIK requires example.svec (SparseVector stored in an Example struct)");
  218. sparseExamples.push_back( example.svec );
  219. }
  220. classifier->addMultipleExamples(sparseExamples, y, this->performOptimizationAfterIncrement);
  221. }