SemSegContextTree.h 5.1 KB

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