SemSegContextTree.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 MultiChannelImageT<int> &cfeats, const int &cTree, 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<std::vector<TreeNode> > forest;
  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. /** Number of trees used for the forest */
  116. int nbTrees;
  117. public:
  118. /** simple constructor */
  119. SemSegContextTree( const NICE::Config *conf, const MultiDataset *md );
  120. /** simple destructor */
  121. virtual ~SemSegContextTree();
  122. /**
  123. * test a single image
  124. * @param ce input data
  125. * @param segresult segmentation results
  126. * @param probabilities probabilities for each pixel
  127. */
  128. void semanticseg ( CachedExample *ce, NICE::Image & segresult, NICE::MultiChannelImageT<double> & probabilities );
  129. /**
  130. * the main training method
  131. * @param md training data
  132. */
  133. void train ( const MultiDataset *md );
  134. /**
  135. * @brief computes integral image of given feats
  136. *
  137. * @param currentfeats input features
  138. * @param integralImage output image (must be initilized)
  139. * @return void
  140. **/
  141. void computeIntegralImage(const NICE::MultiChannelImageT<int> &currentfeats, NICE::MultiChannelImageT<double> &integralImage);
  142. /**
  143. * compute best split for current settings
  144. * @param feats features
  145. * @param currentfeats matrix with current node for each feature
  146. * @param labels labels for each feature
  147. * @param node current node
  148. * @param splitfeat output feature position
  149. * @param splitval
  150. * @return best information gain
  151. */
  152. double getBestSplit(const std::vector<NICE::MultiChannelImageT<double> > &feats, std::vector<NICE::MultiChannelImageT<int> > &currentfeats, std::vector<NICE::MultiChannelImageT<double> > &integralImgs, const std::vector<NICE::MatrixT<int> > &labels, int node, Operation *&splitop, double &splitval, const int &tree);
  153. /**
  154. * @brief computes the mean probability for a given class over all trees
  155. * @param x x position
  156. * @param y y position
  157. * @param channel current class
  158. * @param currentfeats information about the nodes
  159. * @return double mean value
  160. **/
  161. inline double getMeanProb(const int &x,const int &y,const int &channel, const MultiChannelImageT<int> &currentfeats);
  162. };
  163. } // namespace
  164. #endif