VCPreRandomForest.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /**
  2. * @file VCPreRandomForest.cpp
  3. * @brief Combination of a classifier with a pre-clustering using a random forest
  4. * @author Erik Rodner
  5. * @date 06/17/2010
  6. */
  7. #include "VCPreRandomForest.h"
  8. #include <iostream>
  9. #include <vislearning/features/fpfeatures/VectorFeature.h>
  10. #include "core/image/ImageT.h"
  11. //#include "core/imagedisplay/ImageDisplay.h"
  12. using namespace OBJREC;
  13. using namespace std;
  14. using namespace NICE;
  15. VCPreRandomForest::VCPreRandomForest( const Config *conf, const std::string & section, VecClassifier *_leafClassifierPrototype )
  16. : leafClassifierPrototype(_leafClassifierPrototype), fp(conf)
  17. {
  18. string cluster_section = conf->gS(section, "cluster_section", "RandomForest");
  19. mEx = conf->gI("DTBRandom", "min_examples", numeric_limits<int>::max());
  20. mEx = 500;
  21. randomforest = new FPCRandomForests ( conf, cluster_section );
  22. }
  23. VCPreRandomForest::~VCPreRandomForest()
  24. {
  25. // delete the random forest
  26. if ( randomforest != NULL )
  27. delete randomforest;
  28. // delete all classifiers in the leafs
  29. for ( map<DecisionNode *, VecClassifier *>::const_iterator i = leafClassifiers.begin();
  30. i != leafClassifiers.end(); i++ )
  31. {
  32. VecClassifier *lc = i->second;
  33. delete lc;
  34. }
  35. }
  36. ClassificationResult VCPreRandomForest::classify ( const NICE::Vector & x ) const
  37. {
  38. NICE::Vector *v = new NICE::Vector(x);
  39. Example example(v);
  40. vector<DecisionNode *> leafNodes;
  41. // traverse the forest and obtain all involved leaf nodes
  42. randomforest->getLeafNodes(example, leafNodes);
  43. ClassificationResult r ( ClassificationResult::REJECTION_NONE, maxClassNo );
  44. r.scores.set(0.0);
  45. for ( vector<DecisionNode *>::const_iterator i = leafNodes.begin();
  46. i != leafNodes.end(); i++ )
  47. {
  48. DecisionNode *node = *i;
  49. map<DecisionNode *, VecClassifier *>::const_iterator leafClassifierIt =
  50. leafClassifiers.find ( node );
  51. if ( leafClassifierIt == leafClassifiers.end() ) {
  52. // this leaf has no associated classifier
  53. // -> we will use the random forest "score" :)
  54. //
  55. double sum = node->distribution.sum();
  56. for (uint k = 0; k < (uint)std::min(node->distribution.size(), r.scores.size());k++)
  57. {
  58. r.scores[k] += node->distribution[k] / sum;
  59. }
  60. //fthrow(Exception, "Unable to find this leaf node !! (implementation bug)");
  61. continue;
  62. }
  63. VecClassifier *leafClassifier = leafClassifierIt->second;
  64. ClassificationResult rSingle = leafClassifier->classify ( x );
  65. rSingle.scores.normalize();
  66. for (uint k = 0; k < (uint)std::min(rSingle.scores.size(), r.scores.size());k++)
  67. {
  68. r.scores[k] += rSingle.scores[k];
  69. }
  70. }
  71. r.scores.multiply ( 1.0 / (leafNodes.size()) );
  72. r.classno = r.scores.maxElement();
  73. if ( fabs(r.scores.sum() - 1.0) > 1e-2 )
  74. {
  75. //fthrow(Exception, "Ups !\n");
  76. r.scores[0] = 1.0;
  77. }
  78. example.clean();
  79. return r;
  80. }
  81. void VCPreRandomForest::teach ( const LabeledSetVector & teachSet )
  82. {
  83. Examples examples;
  84. maxClassNo = teachSet.getMaxClassno();
  85. LOOP_ALL(teachSet)
  86. {
  87. EACH(classno, x);
  88. NICE::Vector *v = new Vector(x);
  89. examples.push_back( pair<int, Example> (classno, Example(v)));
  90. }
  91. uint dimension = teachSet.dimension();
  92. fp.clear();
  93. Feature *f = new VectorFeature(dimension);
  94. f->explode(fp);
  95. // train the forest
  96. randomforest->setMaxClassNo( teachSet.getMaxClassno() );
  97. randomforest->train ( fp, examples );
  98. // free some useless memory, we do not need this
  99. // data structure any more
  100. examples.clean();
  101. vector<DecisionNode *> leafNodes;
  102. randomforest->getAllLeafNodes ( leafNodes );
  103. int lsize = leafNodes.size();
  104. cout << "leafnodes: " << lsize << endl;
  105. int leafNo = 0;
  106. #pragma omp parallel for
  107. for ( int l = 0; l < lsize; l++)
  108. {
  109. cerr << "Training classifier for leaf " << leafNo << endl;
  110. leafNo++;
  111. DecisionNode *node = leafNodes[l];
  112. if ( node->distribution.entropy() <= 0.0) continue;
  113. if ( ! node->isLeaf() ) continue;
  114. vector<int> examplesSet = node->trainExamplesIndices;
  115. assert(examplesSet.size() > 0);
  116. sort (examplesSet.begin(), examplesSet.end());
  117. LabeledSetVector trainSubSet;
  118. vector<double> counter(maxClassNo, 0.0);
  119. uint exampleIndex = 0;
  120. uint c = 0;
  121. LOOP_ALL(teachSet)
  122. {
  123. EACH(classno, x);
  124. if ( examplesSet[c] == exampleIndex )
  125. {
  126. c++;
  127. trainSubSet.add ( classno, x );
  128. }
  129. exampleIndex++;
  130. }
  131. VecClassifier *lc = leafClassifierPrototype->clone();
  132. lc->teach ( trainSubSet );
  133. leafClassifiers.insert ( pair<DecisionNode *, VecClassifier *> ( node, lc ) );
  134. }
  135. }
  136. void VCPreRandomForest::clear()
  137. {
  138. map<DecisionNode *, VecClassifier *>::iterator iter;
  139. for ( iter = leafClassifiers.begin(); iter != leafClassifiers.end(); ++iter )
  140. {
  141. iter->second->clear();
  142. }
  143. randomforest->clear();
  144. }