FPCRandomForests.h 4.2 KB

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