SemSegContextTree.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. public:
  93. /** simple constructor */
  94. SemSegContextTree( const Config *conf, const MultiDataset *md );
  95. /** simple destructor */
  96. virtual ~SemSegContextTree();
  97. /**
  98. * test a single image
  99. * @param ce input data
  100. * @param segresult segmentation results
  101. * @param probabilities probabilities for each pixel
  102. */
  103. void semanticseg ( CachedExample *ce, NICE::Image & segresult, GenericImage<double> & probabilities );
  104. /**
  105. * the main training method
  106. * @param md training data
  107. */
  108. void train ( const MultiDataset *md );
  109. /**
  110. * compute best split for current settings
  111. * @param feats features
  112. * @param currentfeats matrix with current node for each feature
  113. * @param labels labels for each feature
  114. * @param node current node
  115. * @param splitfeat output feature position
  116. * @param splitval
  117. */
  118. 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);
  119. };
  120. } // namespace
  121. #endif