GPHIKClassifierNICE.cpp 7.3 KB

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