SemSegContextTree.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /**
  2. * @file SemSegContextTree.h
  3. * @brief Context Trees -> Combination of decision tree and context information
  4. * @author Björn Fröhlich
  5. * @date 29.11.2011
  6. */
  7. #ifndef SemSegContextTreeINCLUDE
  8. #define SemSegContextTreeINCLUDE
  9. #include "SemanticSegmentation.h"
  10. #include <core/vector/VVector.h>
  11. #include "vislearning/features/localfeatures/LFColorWeijer.h"
  12. namespace OBJREC {
  13. class Operation;
  14. class TreeNode
  15. {
  16. public:
  17. /** probabilities for each class */
  18. std::vector<double> probs;
  19. /** left child node */
  20. int left;
  21. /** right child node */
  22. int right;
  23. /** position of feat for decision */
  24. Operation *feat;
  25. /** decision stamp */
  26. double decision;
  27. /** is the node a leaf or not */
  28. bool isleaf;
  29. /** distribution in current node */
  30. std::vector<double> dist;
  31. /** depth of the node in the tree */
  32. int depth;
  33. /** simple constructor */
  34. TreeNode():left(-1),right(-1),feat(NULL), decision(-1.0), isleaf(false){}
  35. /** standard constructor */
  36. TreeNode(int _left, int _right, Operation *_feat, double _decision):left(_left),right(_right),feat(_feat), decision(_decision),isleaf(false){}
  37. };
  38. class Operation
  39. {
  40. protected:
  41. int x1, y1, x2, y2, channel1, channel2;
  42. public:
  43. void set(int _x1, int _y1, int _x2, int _y2, int _channel1, int _channel2)
  44. {
  45. x1 = _x1;
  46. y1 = _y1;
  47. x2 = _x2;
  48. y2 = _y2;
  49. channel1 = _channel1;
  50. channel2 = _channel2;
  51. }
  52. /**
  53. * @brief abstract interface for feature computation
  54. * @param feats features
  55. * @param cfeats number of tree node for each pixel
  56. * @param tree current tree
  57. * @param x current x position
  58. * @param y current y position
  59. * @return double distance
  60. **/
  61. virtual double getVal(const NICE::MultiChannelImageT<double> &feats, const std::vector<std::vector<int> > &cfeats, const std::vector<TreeNode> &tree, const int &x, const int &y) = 0;
  62. virtual Operation* clone() = 0;
  63. virtual void writeInfos() = 0;
  64. };
  65. /** Localization system */
  66. class SemSegContextTree : public SemanticSegmentation
  67. {
  68. protected:
  69. /** tree -> saved as vector of nodes */
  70. std::vector<TreeNode> tree;
  71. /** local features */
  72. LFColorWeijer *lfcw;
  73. /** number of featuretype -> currently: local and context features = 2 */
  74. int ftypes;
  75. /** distance between features */
  76. int grid;
  77. /** maximum samples for tree */
  78. int maxSamples;
  79. /** size for neighbourhood */
  80. int windowSize;
  81. /** how many feats should be considered for a split */
  82. int featsPerSplit;
  83. /** count samples per label */
  84. std::map<int,int> labelcounter;
  85. /** map of labels */
  86. std::map<int,int> labelmap;
  87. /** map of labels inverse*/
  88. std::map<int,int> labelmapback;
  89. /** scalefactor for balancing for each class */
  90. std::vector<double> a;
  91. /** the minimum number of features allowed in a leaf */
  92. int minFeats;
  93. /** maximal depth of tree */
  94. int maxDepth;
  95. /** operations for pairwise features */
  96. std::vector<Operation*> ops;
  97. /** operations for pairwise context features */
  98. std::vector<Operation*> cops;
  99. /** vector of all possible features */
  100. std::vector<Operation*> featsel;
  101. /** use alternative calculation for information gain */
  102. bool useShannonEntropy;
  103. /** Classnames */
  104. ClassNames classnames;
  105. /** train selection */
  106. std::set<int> forbidden_classes;
  107. /** Configfile */
  108. const Config *conf;
  109. public:
  110. /** simple constructor */
  111. SemSegContextTree( const NICE::Config *conf, const MultiDataset *md );
  112. /** simple destructor */
  113. virtual ~SemSegContextTree();
  114. /**
  115. * test a single image
  116. * @param ce input data
  117. * @param segresult segmentation results
  118. * @param probabilities probabilities for each pixel
  119. */
  120. void semanticseg ( CachedExample *ce, NICE::Image & segresult, NICE::MultiChannelImageT<double> & probabilities );
  121. /**
  122. * the main training method
  123. * @param md training data
  124. */
  125. void train ( const MultiDataset *md );
  126. /**
  127. * compute best split for current settings
  128. * @param feats features
  129. * @param currentfeats matrix with current node for each feature
  130. * @param labels labels for each feature
  131. * @param node current node
  132. * @param splitfeat output feature position
  133. * @param splitval
  134. */
  135. void getBestSplit(const std::vector<MultiChannelImageT<double> > &feats, std::vector<std::vector<std::vector<int> > > &currentfeats,const std::vector<std::vector<std::vector<int> > > &labels, int node, Operation *&splitfeat, double &splitval);
  136. };
  137. } // namespace
  138. #endif