SemSegContextTree.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 <objrec/math/mathbase/VVector.h>
  11. #include "objrec/features/localfeatures/LFColorWeijer.h"
  12. namespace OBJREC {
  13. /** Localization system */
  14. class SemSegContextTree : public SemanticSegmentation
  15. {
  16. protected:
  17. class Node
  18. {
  19. public:
  20. /** probabilities for each class */
  21. vector<double> probs;
  22. /** left child node */
  23. int left;
  24. /** right child node */
  25. int right;
  26. /** position of feat for decision */
  27. int feat;
  28. /** decision stamp */
  29. double decision;
  30. /** is the node a leaf or not */
  31. bool isleaf;
  32. /** distribution in current node */
  33. vector<double> dist;
  34. /** depth of the node in the tree */
  35. int depth;
  36. /** simple constructor */
  37. Node():left(-1),right(-1),feat(-1), decision(-1.0), isleaf(false){}
  38. /** standard constructor */
  39. Node(int _left, int _right, int _feat, double _decision):left(_left),right(_right),feat(_feat), decision(_decision),isleaf(false){}
  40. };
  41. class Operation
  42. {
  43. public:
  44. virtual double getVal(const double &v1, const double &v2) = 0;
  45. };
  46. class Minus:public Operation
  47. {
  48. public:
  49. virtual double getVal(const double &v1, const double &v2)
  50. {
  51. return v1-v2;
  52. }
  53. };
  54. class MinusAbs:public Operation
  55. {
  56. public:
  57. virtual double getVal(const double &v1, const double &v2)
  58. {
  59. return abs(v1-v2);
  60. }
  61. };
  62. class Addition:public Operation
  63. {
  64. public:
  65. virtual double getVal(const double &v1, const double &v2)
  66. {
  67. return v1+v2;
  68. }
  69. };
  70. class Only1:public Operation
  71. {
  72. public:
  73. virtual double getVal(const double &v1, const double &v2)
  74. {
  75. return v1;
  76. }
  77. };
  78. /** store features */
  79. VVector currentfeats;
  80. /** store the positions of the features */
  81. VVector positions;
  82. /** tree -> saved as vector of nodes */
  83. vector<Node> tree;
  84. /** local features */
  85. LFColorWeijer *lfcw;
  86. /** distance between features */
  87. int grid;
  88. /** maximum samples for tree */
  89. int maxSamples;
  90. /** size for neighbourhood */
  91. int windowSize;
  92. /** how many feats should be considered for a split */
  93. int featsPerSplit;
  94. /** count samples per label */
  95. map<int,int> labelcounter;
  96. /** map of labels */
  97. map<int,int> labelmap;
  98. /** map of labels inverse*/
  99. map<int,int> labelmapback;
  100. /** scalefactor for balancing for each class */
  101. vector<double> a;
  102. /** the minimum number of features allowed in a leaf */
  103. int minFeats;
  104. /** maximal depth of tree */
  105. int maxDepth;
  106. /** operations for pairwise features */
  107. vector<Operation*> ops;
  108. /** number of possible features */
  109. int allfeatsize;
  110. /** vector of all possible features */
  111. vector<vector<int> > featsel;
  112. public:
  113. /** simple constructor */
  114. SemSegContextTree( const Config *conf, const MultiDataset *md );
  115. /** simple destructor */
  116. virtual ~SemSegContextTree();
  117. /**
  118. * test a single image
  119. * @param ce input data
  120. * @param segresult segmentation results
  121. * @param probabilities probabilities for each pixel
  122. */
  123. void semanticseg ( CachedExample *ce, NICE::Image & segresult, GenericImage<double> & probabilities );
  124. /**
  125. * the main training method
  126. * @param md training data
  127. */
  128. void train ( const MultiDataset *md );
  129. /**
  130. * compute best split for current settings
  131. * @param feats features
  132. * @param currentfeats matrix with current node for each feature
  133. * @param labels labels for each feature
  134. * @param node current node
  135. * @param splitfeat output feature position
  136. * @param splitval
  137. */
  138. void getBestSplit(const vector<vector<vector<vector<double> > > > &feats, vector<vector<vector<int> > > &currentfeats,const vector<vector<vector<int> > > &labels, int node, int &splitfeat, double &splitval);
  139. };
  140. } // namespace
  141. #endif