SemSegContextTree.h 7.0 KB

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