SemSegContextTree.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. struct Features{
  40. NICE::MultiChannelImageT<double> *feats;
  41. MultiChannelImageT<int> *cfeats;
  42. int cTree;
  43. std::vector<TreeNode> *tree;
  44. NICE::MultiChannelImageT<double> *integralImg;
  45. };
  46. class ValueAccess
  47. {
  48. public:
  49. virtual double getVal(const Features &feats, const int &x, const int &y, const int &channel) = 0;
  50. virtual std::string writeInfos() = 0;
  51. };
  52. class Operation
  53. {
  54. protected:
  55. int x1, y1, x2, y2, channel1, channel2;
  56. ValueAccess *values;
  57. public:
  58. virtual void set(int _x1, int _y1, int _x2, int _y2, int _channel1, int _channel2, ValueAccess *_values)
  59. {
  60. x1 = _x1;
  61. y1 = _y1;
  62. x2 = _x2;
  63. y2 = _y2;
  64. channel1 = _channel1;
  65. channel2 = _channel2;
  66. values = _values;
  67. }
  68. /**
  69. * @brief abstract interface for feature computation
  70. * @param feats features
  71. * @param cfeats number of tree node for each pixel
  72. * @param tree current tree
  73. * @param x current x position
  74. * @param y current y position
  75. * @return double distance
  76. **/
  77. virtual double getVal(const Features &feats, const int &x, const int &y) = 0;
  78. virtual Operation* clone() = 0;
  79. virtual std::string writeInfos() = 0;
  80. inline void getXY(const Features &feats, int &xsize, int &ysize)
  81. {
  82. xsize = feats.feats->width();
  83. ysize = feats.feats->height();
  84. }
  85. };
  86. /** Localization system */
  87. class SemSegContextTree : public SemanticSegmentation
  88. {
  89. /** Segmentation Method */
  90. RegionSegmentationMethod *segmentation;
  91. /** tree -> saved as vector of nodes */
  92. std::vector<std::vector<TreeNode> > forest;
  93. /** local features */
  94. LFColorWeijer *lfcw;
  95. /** number of featuretype -> currently: local and context features = 2 */
  96. int ftypes;
  97. /** distance between features */
  98. int grid;
  99. /** maximum samples for tree */
  100. int maxSamples;
  101. /** size for neighbourhood */
  102. int windowSize;
  103. /** how many feats should be considered for a split */
  104. int featsPerSplit;
  105. /** count samples per label */
  106. std::map<int,int> labelcounter;
  107. /** map of labels */
  108. std::map<int,int> labelmap;
  109. /** map of labels inverse*/
  110. std::map<int,int> labelmapback;
  111. /** scalefactor for balancing for each class */
  112. std::vector<double> a;
  113. /** the minimum number of features allowed in a leaf */
  114. int minFeats;
  115. /** maximal depth of tree */
  116. int maxDepth;
  117. /** operations for pairwise features */
  118. std::vector<Operation*> ops;
  119. /** operations for pairwise context features */
  120. std::vector<Operation*> cops;
  121. std::vector<ValueAccess*> calcVal;
  122. /** vector of all possible features */
  123. std::vector<Operation*> featsel;
  124. /** use alternative calculation for information gain */
  125. bool useShannonEntropy;
  126. /** Classnames */
  127. ClassNames classnames;
  128. /** train selection */
  129. std::set<int> forbidden_classes;
  130. /** Configfile */
  131. const Config *conf;
  132. /** use pixelwise labeling or regionlabeling with additional segmenation */
  133. bool pixelWiseLabeling;
  134. /** use Gaussian distributed features based on the feature position */
  135. bool useGaussian;
  136. /** Number of trees used for the forest */
  137. int nbTrees;
  138. public:
  139. /** simple constructor */
  140. SemSegContextTree( const NICE::Config *conf, const MultiDataset *md );
  141. /** simple destructor */
  142. virtual ~SemSegContextTree();
  143. /**
  144. * test a single image
  145. * @param ce input data
  146. * @param segresult segmentation results
  147. * @param probabilities probabilities for each pixel
  148. */
  149. void semanticseg ( CachedExample *ce, NICE::Image & segresult, NICE::MultiChannelImageT<double> & probabilities );
  150. /**
  151. * the main training method
  152. * @param md training data
  153. */
  154. void train ( const MultiDataset *md );
  155. /**
  156. * @brief computes integral image of given feats
  157. *
  158. * @param currentfeats input features
  159. * @param integralImage output image (must be initilized)
  160. * @return void
  161. **/
  162. void computeIntegralImage(const NICE::MultiChannelImageT<int> &currentfeats, const NICE::MultiChannelImageT<int> &lfeats, NICE::MultiChannelImageT<double> &integralImage);
  163. /**
  164. * compute best split for current settings
  165. * @param feats features
  166. * @param currentfeats matrix with current node for each feature
  167. * @param labels labels for each feature
  168. * @param node current node
  169. * @param splitfeat output feature position
  170. * @param splitval
  171. * @return best information gain
  172. */
  173. double getBestSplit(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);
  174. /**
  175. * @brief computes the mean probability for a given class over all trees
  176. * @param x x position
  177. * @param y y position
  178. * @param channel current class
  179. * @param currentfeats information about the nodes
  180. * @return double mean value
  181. **/
  182. inline double getMeanProb(const int &x,const int &y,const int &channel, const MultiChannelImageT<int> &currentfeats);
  183. };
  184. } // namespace
  185. #endif