FPCRandomForests.h 3.8 KB

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