CodebookRandomForest.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #ifndef CodebookRandomForestINCLUDE
  2. #define CodebookRandomForestINCLUDE
  3. #include "core/vector/VVector.h"
  4. #include "vislearning/features/simplefeatures/Codebook.h"
  5. #include "vislearning/classifier/fpclassifier/randomforest/FPCRandomForests.h"
  6. #include <string>
  7. namespace OBJREC {
  8. /**
  9. * @brief Random Forest based codebook generator (Moosmann et al.)
  10. *
  11. * Implementation of the Extremely randomized clustering forests (ERC) by (Moosmann et al., 2006)
  12. *
  13. * There also exists an Matlab Mex implementation for this class (CodebookRandomForestMex.cpp).
  14. *
  15. * @example TestCodebookRandomForest
  16. * @example progCodebookRandomForest
  17. *
  18. * @author Erik Rodner (creator), Johannes Ruehle (updater)
  19. * @date 02/15/2008, 05/05/2014
  20. */
  21. class CodebookRandomForest : public Codebook
  22. {
  23. protected:
  24. /** leafs and their corresponding indices */
  25. std::map<DecisionNode *, int> leafMap;
  26. /** the random forest used for clustering */
  27. FPCRandomForests *clusterforest;
  28. /** maximum depth of the forest */
  29. int maxDepth;
  30. /** the forest is reduced in pruneForest to this
  31. specific number of leafs */
  32. int restrictedCodebookSize;
  33. /** build the a directed relation graph from a forest
  34. @param node starting node for traversal
  35. @param parentStructure (a,b) is in this map if b is the parent node of a
  36. */
  37. void buildParentStructure ( DecisionNode *node,
  38. std::map<DecisionNode *, DecisionNode *> & parentStructure );
  39. /** reduce the cluster forest to a specific number of leafs */
  40. void pruneForest ();
  41. /** enumerate all leafs and store this information in leafMap */
  42. void buildLeafMap ();
  43. public:
  44. /** simple constructor */
  45. CodebookRandomForest( int maxDepth, int restrictedCodebookSize = 0 );
  46. /** standard constructor */
  47. CodebookRandomForest( FPCRandomForests *clusterforest, int maxDepth, int restrictedCodebookSize = 0 );
  48. /** simple destructor */
  49. virtual ~CodebookRandomForest();
  50. /**
  51. * set a new clusterforest
  52. * @param clusterforest new random forest
  53. */
  54. void setClusterForest( FPCRandomForests *clusterforest);
  55. /**
  56. * not supported, please use one of the other vote functions
  57. */
  58. void vote ( const NICE::Vector & feature, int & codebookEntry, double & weight, double & distance ) const;
  59. virtual void voteVQ ( const NICE::Vector & feature, int & codebookEntry, double & weight, double & distance ) const
  60. {
  61. this->vote(feature,codebookEntry, weight, distance);
  62. }
  63. void vote ( const NICE::Vector & feature, NICE::Vector & histogram, int & codebookEntry, double & weight, double & distance ) const;
  64. virtual void voteVQ (const NICE::Vector &feature, NICE::Vector &histogram, int & codebookEntry, double & weight, double & distance ) const {
  65. this->vote(feature, histogram, codebookEntry, weight, distance);
  66. }
  67. virtual void voteVA ( const NICE::Vector & feature, NICE::Vector & votes ) const {
  68. int codebookEntry = 0;
  69. double weight = 0.0f;
  70. double distance = 0.0f;
  71. this->vote(feature, votes, codebookEntry, weight, distance);
  72. }
  73. /** this is the preferred voting interface for this codebook */
  74. void vote ( const NICE::Vector & feature, NICE::SparseVector & votes ) const;
  75. /** normal codebook voting, but additionally returns a probability distribution for the class label **/
  76. void voteAndClassify ( const NICE::Vector & feature, NICE::SparseVector & votes, FullVector & distribution ) const;
  77. /** normal codebook voting, but additionally returns a probability distribution for the class label **/
  78. void voteAndClassify ( const NICE::Vector & feature, NICE::SparseVector & votes, NICE::Vector & distribution ) const;
  79. virtual void voteVA ( const NICE::Vector & feature, NICE::SparseVector & votes ) const {
  80. this->vote(feature, votes);
  81. }
  82. /** this codebook method gives multiple votes for each local feature (depending
  83. on the number of decision trees in the forest */
  84. bool allowsMultipleVoting () { return true; }
  85. /** get the random forest for direct access */
  86. FPCRandomForests *getRandomForest (void) { return clusterforest; }
  87. void add ( const Codebook *codebook );
  88. void copy ( const Codebook *codebook );
  89. Codebook *clone () const;
  90. /** clear the codebook */
  91. void clear ();
  92. /** read the codebook from a stream */
  93. void restore ( std::istream & is, int format = 0);
  94. /** write the codebook to a stream */
  95. void store ( std::ostream & os, int format = 0) const;
  96. int getMaxDepth() const
  97. {
  98. return this->maxDepth;
  99. }
  100. int getRestrictedCodebookSize() const
  101. {
  102. return restrictedCodebookSize;
  103. }
  104. };
  105. } // namespace
  106. #endif