SemSegContextTree.h 3.8 KB

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