SemSegContextTree.h 6.3 KB

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