FPCGPHIK.cpp 7.3 KB

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