GPHIKClassifierNICE.cpp 6.8 KB

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