FPCRandomForests.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /**
  2. * @file FPCRandomForests.h
  3. * @brief implementation of random set forests
  4. * @author Erik Rodner
  5. * @date 04/24/2008
  6. */
  7. #ifndef FPCRANDOMFORESTSINCLUDE
  8. #define FPCRANDOMFORESTSINCLUDE
  9. #include <vector>
  10. #include "core/vector/VectorT.h"
  11. #include "core/vector/MatrixT.h"
  12. #include "vislearning/classifier/classifierbase/FeaturePoolClassifier.h"
  13. #include "vislearning/cbaselib/FeaturePool.h"
  14. #include "DecisionTree.h"
  15. #include "DecisionTreeBuilder.h"
  16. namespace OBJREC {
  17. /** implementation of random set forests */
  18. class FPCRandomForests : public FeaturePoolClassifier
  19. {
  20. protected:
  21. /** vector containing all decision trees */
  22. std::vector<DecisionTree *> forest;
  23. /** number of trees which will be generated in the
  24. during training */
  25. int number_of_trees;
  26. /** fraction of features used for each tree */
  27. double features_per_tree;
  28. /** fraction of training examples used for each tree */
  29. double samples_per_tree;
  30. /** use an equal number of training examples of each class
  31. to build a single tree */
  32. bool use_simple_balancing;
  33. /** weight examples according to a priori class probabilities
  34. as estimated using the distribution contained in the training data */
  35. bool weight_examples;
  36. /** if >0 then prune the trees using pruneTreeEntropy */
  37. double minimum_entropy;
  38. /** clear all examples after building a tree, this deletes
  39. all cached images contained in CachedExample etc. */
  40. bool memory_efficient;
  41. /** stored config to initialize a tree */
  42. const NICE::Config *conf;
  43. /** config section containing important config values */
  44. std::string confsection;
  45. /** pointer to the tree builder method */
  46. DecisionTreeBuilder *builder;
  47. /** out-of-bag statistics */
  48. bool enableOutOfBagEstimates;
  49. std::vector<std::pair<double, int> > oobResults;
  50. /** classify using only a subset of all trees */
  51. ClassificationResult classify ( Example & pce,
  52. const std::vector<int> & outofbagtrees );
  53. /** calculate out-of-bag statistics */
  54. void calcOutOfBagEstimates ( std::vector< std::vector<int> > & outofbagtrees,
  55. Examples & examples );
  56. /** save example selection per tree */
  57. std::vector<std::vector<int> > exselection;
  58. public:
  59. /** initialize the classifier */
  60. FPCRandomForests( const NICE::Config *conf,
  61. std::string section );
  62. /** do nothing */
  63. FPCRandomForests ();
  64. /** simple destructor */
  65. virtual ~FPCRandomForests();
  66. /** main classification function */
  67. ClassificationResult classify ( Example & pce );
  68. int classify_optimize ( Example & pce );
  69. /** get all leaf nodes for an given example (or inner nodes if depth is set to the level) */
  70. void getLeafNodes ( Example & pce,
  71. std::vector<DecisionNode *> & leafNodes,
  72. int depth = 100000 );
  73. /** get all leaf nodes (or inner nodes if depth is set to the level) */
  74. void getAllLeafNodes ( std::vector<DecisionNode *> & leafNodes);
  75. /** perform training using a given feature pool and some training data */
  76. virtual void train ( FeaturePool & fp,
  77. Examples & examples );
  78. /** enumerate all nodes within the trees */
  79. void indexDescendants ( std::map<DecisionNode *, std::pair<long, int> > & index ) const;
  80. /** reset all counters in all nodes contained in the forest */
  81. void resetCounters ();
  82. /** direct access to all trees */
  83. const std::vector<DecisionTree *> & getForest () const { return forest; };
  84. /** direct write access to all trees */
  85. std::vector<DecisionTree *> & getForestNonConst () { return forest; };
  86. /** clone this object */
  87. FeaturePoolClassifier *clone () const;
  88. /** get out of bag estimates */
  89. std::vector<std::pair<double, int> > & getOutOfBagResults () { return oobResults; };
  90. /** set the number of trees */
  91. void setComplexity ( int size );
  92. /** IO functions */
  93. void restore (std::istream & is, int format = 0);
  94. void store (std::ostream & os, int format = 0) const;
  95. void clear ();
  96. };
  97. } // namespace
  98. #endif