CodebookRandomForest.h 4.0 KB

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