SemSegContextTree.h 6.5 KB

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