FPCGPHIK.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. const SparseVector *svec = pe.svec;
  30. if ( svec == NULL )
  31. fthrow(Exception, "FPCGPHIK requires example.svec (SparseVector stored in an Example struct)");
  32. return this->classify( svec );
  33. }
  34. ClassificationResult FPCGPHIK::classify ( const NICE::SparseVector * example )
  35. {
  36. NICE::SparseVector scores;
  37. int result;
  38. double uncertainty;
  39. classifier->classify ( example, result, scores, uncertainty);
  40. if ( scores.size() == 0 ) {
  41. fthrow(Exception, "Zero scores, something is likely to be wrong here: svec.size() = " << example->size() );
  42. }
  43. int classes = scores.getDim();
  44. FullVector fvscores(classes);
  45. NICE::SparseVector::const_iterator it;
  46. for(int c = 0; c < classes; c++)
  47. {
  48. it = scores.find(c);
  49. if ( it == scores.end() )
  50. fvscores[c] = -std::numeric_limits<double>::max();
  51. else
  52. fvscores[c] = it->second;
  53. }
  54. ClassificationResult r ( fvscores.maxElement(), fvscores );
  55. r.uncertainty = uncertainty;
  56. if (verbose)
  57. {
  58. std::cerr << " FPCGPHIK::classify scores" << std::endl;
  59. scores.store(std::cerr);
  60. std::cerr << " FPCGPHIK::classify fvscores" << std::endl;
  61. fvscores.store(std::cerr);
  62. }
  63. return r;
  64. }
  65. /** training process */
  66. void FPCGPHIK::train ( FeaturePool & fp, Examples & examples )
  67. {
  68. // we completely ignore the feature pool :)
  69. //
  70. initRand(0);
  71. Vector classCounts;
  72. int minClass = -1;
  73. if (verbose)
  74. std::cerr << "FPCGPHIK::train" << std::endl;
  75. if ( useSimpleBalancing)
  76. {
  77. classCounts.resize( examples.getMaxClassNo()+1 );
  78. classCounts.set( 0.0 );
  79. for ( uint i = 0 ; i < examples.size() ; i++ )
  80. classCounts[ examples[i].first ]++;
  81. // we need a probability distribution
  82. //classCounts.normalizeL1();
  83. // we need the class index of the class with the least non-zero examples
  84. for ( uint i = 0 ; i < classCounts.size(); i++ )
  85. if ( (classCounts[i] > 0) && ((minClass < 0) || (classCounts[i] < classCounts[minClass])) )
  86. minClass = i;
  87. if (verbose)
  88. {
  89. cerr << "Class distribution: " << classCounts << endl;
  90. cerr << "Class with the least number of examples: " << minClass << endl;
  91. }
  92. if(minSamples < 0)
  93. minSamples = classCounts[minClass];
  94. }
  95. // (multi-class) label vector
  96. Vector y ( examples.size() /* maximum size */ );
  97. // flat structure of our training data
  98. std::vector< SparseVector * > sparseExamples;
  99. if (verbose)
  100. cerr << "Converting (and sampling) feature vectors" << endl;
  101. for ( uint i = 0 ; i < examples.size() ; i++ )
  102. {
  103. const Example & example = examples[i].second;
  104. int classno = examples[i].first;
  105. // simple weird balancing method
  106. if ( useSimpleBalancing )
  107. {
  108. double t = randDouble() * classCounts[classno];
  109. if ( t >= minSamples ) continue;
  110. }
  111. y[ sparseExamples.size() ] = classno;
  112. if ( example.svec == NULL )
  113. fthrow(Exception, "FPCGPHIK requires example.svec (SparseVector stored in an Example struct)");
  114. sparseExamples.push_back( example.svec );
  115. }
  116. // we only use a subset for training
  117. y.resize( sparseExamples.size() );
  118. classifier->train(sparseExamples, y);
  119. }
  120. /** training process */
  121. void FPCGPHIK::train ( const std::vector< SparseVector *> & examples, std::map<int, NICE::Vector> & binLabels )
  122. {
  123. classifier->train(examples, binLabels);
  124. }
  125. void FPCGPHIK::clear ()
  126. {
  127. if ( classifier != NULL )
  128. delete classifier;
  129. classifier = NULL;
  130. }
  131. FeaturePoolClassifier *FPCGPHIK::clone () const
  132. {
  133. fthrow(Exception, "FPCGPHIK: clone() not yet implemented" );
  134. return NULL;
  135. }
  136. void FPCGPHIK::predictUncertainty( Example & pe, NICE::Vector & uncertainties )
  137. {
  138. const SparseVector *svec = pe.svec;
  139. if ( svec == NULL )
  140. fthrow(Exception, "FPCGPHIK requires example.svec (SparseVector stored in an Example struct)");
  141. classifier->predictUncertainty(svec, uncertainties);
  142. }
  143. void FPCGPHIK::predictUncertainty( const NICE::SparseVector * example, NICE::Vector & uncertainties )
  144. {
  145. classifier->predictUncertainty(example, uncertainties);
  146. }
  147. //---------------------------------------------------------------------
  148. // protected methods
  149. //---------------------------------------------------------------------
  150. void FPCGPHIK::restore ( std::istream & is, int format )
  151. {
  152. if (is.good())
  153. {
  154. classifier->restore(is, format);
  155. std::string tmp;
  156. is >> tmp; //"performOptimizationAfterIncrement: "
  157. is >> this->performOptimizationAfterIncrement;
  158. }
  159. else
  160. {
  161. std::cerr << "FPCGPHIK::restore -- InStream not initialized - restoring not possible!" << std::endl;
  162. }
  163. }
  164. void FPCGPHIK::store ( std::ostream & os, int format ) const
  165. {
  166. if (os.good())
  167. {
  168. os.precision (numeric_limits<double>::digits10 + 1);
  169. classifier->store(os, format);
  170. os << "performOptimizationAfterIncrement: " << performOptimizationAfterIncrement << std::endl;
  171. }
  172. else
  173. {
  174. std::cerr << "OutStream not initialized - storing not possible!" << std::endl;
  175. }
  176. }
  177. void FPCGPHIK::addExample( const Example & pe, const double & label)
  178. {
  179. const SparseVector *svec = pe.svec;
  180. classifier->addExample(svec, label, this->performOptimizationAfterIncrement);
  181. }
  182. void FPCGPHIK::addMultipleExamples( Examples & newExamples)
  183. {
  184. //are new examples available? If not, nothing has to be done
  185. if ( newExamples.size() < 1)
  186. return;
  187. // (multi-class) label vector
  188. Vector y ( newExamples.size() );
  189. // flat structure of our training data
  190. std::vector< const SparseVector * > sparseExamples;
  191. if (verbose)
  192. cerr << "Converting (and sampling) feature vectors" << endl;
  193. for ( uint i = 0 ; i < newExamples.size() ; i++ )
  194. {
  195. const Example & example = newExamples[i].second;
  196. int classno = newExamples[i].first;
  197. y[ i ] = classno;
  198. if ( example.svec == NULL )
  199. fthrow(Exception, "FPCGPHIK requires example.svec (SparseVector stored in an Example struct)");
  200. sparseExamples.push_back( example.svec );
  201. }
  202. classifier->addMultipleExamples(sparseExamples, y, this->performOptimizationAfterIncrement);
  203. }