FPCRandomForests.h 4.0 KB

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