SemSegContextTree.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. #include "objrec-froehlichexp/semseg/operations/Operations.h"
  14. namespace OBJREC {
  15. /** Localization system */
  16. class SemSegContextTree : public SemanticSegmentation, public NICE::Persistent
  17. {
  18. /** Segmentation Method */
  19. RegionSegmentationMethod *segmentation;
  20. /** tree -> saved as vector of nodes */
  21. std::vector<std::vector<TreeNode> > forest;
  22. /** local features */
  23. LFColorWeijer *lfcw;
  24. /** number of featuretype -> currently: local and context features = 2 */
  25. int ftypes;
  26. /** distance between features */
  27. int grid;
  28. /** maximum samples for tree */
  29. int maxSamples;
  30. /** size for neighbourhood */
  31. int windowSize;
  32. /** how many feats should be considered for a split */
  33. int featsPerSplit;
  34. /** count samples per label */
  35. std::map<int, int> labelcounter;
  36. /** map of labels */
  37. std::map<int, int> labelmap;
  38. /** map of labels inverse*/
  39. std::map<int, int> labelmapback;
  40. /** scalefactor for balancing for each class */
  41. std::vector<double> a;
  42. /** counter for used operations */
  43. std::vector<int> opOverview;
  44. /** relative use of context vs raw features per tree level*/
  45. std::vector<std::vector<double> > contextOverview;
  46. /** the minimum number of features allowed in a leaf */
  47. int minFeats;
  48. /** maximal depth of tree */
  49. int maxDepth;
  50. /** current depth for training */
  51. int depth;
  52. /** how many splittests */
  53. int randomTests;
  54. /** operations for pairwise features */
  55. std::vector<Operation*> ops;
  56. /** operations for pairwise context features */
  57. std::vector<Operation*> cops;
  58. std::vector<ValueAccess*> calcVal;
  59. /** vector of all possible features */
  60. std::vector<Operation*> featsel;
  61. /** use alternative calculation for information gain */
  62. bool useShannonEntropy;
  63. /** Classnames */
  64. ClassNames classnames;
  65. /** train selection */
  66. std::set<int> forbidden_classes;
  67. /** Configfile */
  68. const NICE::Config *conf;
  69. /** use pixelwise labeling or regionlabeling with additional segmenation */
  70. bool pixelWiseLabeling;
  71. /** use Gaussian distributed features based on the feature position */
  72. bool useGaussian;
  73. /** Number of trees used for the forest */
  74. int nbTrees;
  75. public:
  76. /** simple constructor */
  77. SemSegContextTree ( const NICE::Config *conf, const MultiDataset *md );
  78. /** simple destructor */
  79. virtual ~SemSegContextTree();
  80. /**
  81. * test a single image
  82. * @param ce input data
  83. * @param segresult segmentation results
  84. * @param probabilities probabilities for each pixel
  85. */
  86. void semanticseg ( CachedExample *ce, NICE::Image & segresult, NICE::MultiChannelImageT<double> & probabilities );
  87. /**
  88. * the main training method
  89. * @param md training data
  90. */
  91. void train ( const MultiDataset *md );
  92. /**
  93. * @brief computes integral image of given feats
  94. *
  95. * @param currentfeats input features
  96. * @param integralImage output image (must be initilized)
  97. * @return void
  98. **/
  99. void computeIntegralImage ( const NICE::MultiChannelImageT<unsigned short int> &currentfeats, const NICE::MultiChannelImageT<double> &lfeats, NICE::MultiChannelImageT<double> &integralImage );
  100. /**
  101. * @brief computes integral image for Sparse Multichannel Image
  102. *
  103. * @param currentfeats input features
  104. * @param integralImage output image (must be initilized)
  105. * @return void
  106. **/
  107. void computeIntegralImage ( const NICE::MultiChannelImageT<NICE::SparseVectorInt> &infeats, NICE::MultiChannelImageT<NICE::SparseVectorInt> &integralImage );
  108. /**
  109. * compute best split for current settings
  110. * @param feats features
  111. * @param currentfeats matrix with current node for each feature
  112. * @param labels labels for each feature
  113. * @param node current node
  114. * @param splitfeat output feature position
  115. * @param splitval
  116. * @return best information gain
  117. */
  118. double getBestSplit ( std::vector<NICE::MultiChannelImageT<double> > &feats, std::vector<NICE::MultiChannelImageT<unsigned short int> > &currentfeats, std::vector<NICE::MultiChannelImageT<double> > &integralImgs, const std::vector<NICE::MatrixT<int> > &labels, int node, Operation *&splitop, double &splitval, const int &tree );
  119. /**
  120. * @brief computes the mean probability for a given class over all trees
  121. * @param x x position
  122. * @param y y position
  123. * @param channel current class
  124. * @param currentfeats information about the nodes
  125. * @return double mean value
  126. **/
  127. inline double getMeanProb ( const int &x, const int &y, const int &channel, const NICE::MultiChannelImageT<unsigned short int> &currentfeats );
  128. /**
  129. * @brief load all data to is stream
  130. *
  131. * @param is input stream
  132. * @param format has no influence
  133. * @return void
  134. **/
  135. virtual void restore ( std::istream & is, int format = 0 );
  136. /**
  137. * @brief save all data to is stream
  138. *
  139. * @param os output stream
  140. * @param format has no influence
  141. * @return void
  142. **/
  143. virtual void store ( std::ostream & os, int format = 0 ) const;
  144. /**
  145. * @brief clean up
  146. *
  147. * @return void
  148. **/
  149. virtual void clear () {}
  150. };
  151. } // namespace
  152. #endif