SemSegContextTree.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. /** use Gradient image or not */
  76. bool useGradient;
  77. /** use Regions as extra feature channel or not */
  78. bool useRegionFeature;
  79. /** how to handle each channel */
  80. std::vector<int> channelType;
  81. /** whether we should use the geometric features of Hoeim (only offline computation with MATLAB supported) */
  82. bool useHoiemFeatures;
  83. /** directory of the geometric features */
  84. std::string hoiemDirectory;
  85. public:
  86. /** simple constructor */
  87. SemSegContextTree ( const NICE::Config *conf, const MultiDataset *md );
  88. /** simple destructor */
  89. virtual ~SemSegContextTree();
  90. /**
  91. * test a single image
  92. * @param ce input data
  93. * @param segresult segmentation results
  94. * @param probabilities probabilities for each pixel
  95. */
  96. void semanticseg ( CachedExample *ce, NICE::Image & segresult, NICE::MultiChannelImageT<double> & probabilities );
  97. /**
  98. * the main training method
  99. * @param md training data
  100. */
  101. void train ( const MultiDataset *md );
  102. /**
  103. * @brief computes integral image of given feats
  104. *
  105. * @param currentfeats input features
  106. * @param integralImage output image (must be initilized)
  107. * @return void
  108. **/
  109. void computeIntegralImage ( const NICE::MultiChannelImageT<unsigned short int> &currentfeats, const NICE::MultiChannelImageT<double> &lfeats, NICE::MultiChannelImageT<double> &integralImage );
  110. /**
  111. * @brief reads image and does some simple convertions
  112. *
  113. * @param feats output image
  114. * @param currentFile image filename
  115. * @return void
  116. **/
  117. void extractBasicFeatures ( NICE::MultiChannelImageT<double> &feats, const NICE::ColorImage &img, const std::string &currentFile);
  118. /**
  119. * @brief computes integral image for Sparse Multichannel Image
  120. *
  121. * @param currentfeats input features
  122. * @param integralImage output image (must be initilized)
  123. * @return void
  124. **/
  125. void computeIntegralImage ( const NICE::MultiChannelImageT<NICE::SparseVectorInt> &infeats, NICE::MultiChannelImageT<NICE::SparseVectorInt> &integralImage );
  126. /**
  127. * compute best split for current settings
  128. * @param feats features
  129. * @param currentfeats matrix with current node for each feature
  130. * @param labels labels for each feature
  131. * @param node current node
  132. * @param splitfeat output feature position
  133. * @param splitval
  134. * @return best information gain
  135. */
  136. 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 );
  137. /**
  138. * @brief computes the mean probability for a given class over all trees
  139. * @param x x position
  140. * @param y y position
  141. * @param channel current class
  142. * @param currentfeats information about the nodes
  143. * @return double mean value
  144. **/
  145. inline double getMeanProb ( const int &x, const int &y, const int &channel, const NICE::MultiChannelImageT<unsigned short int> &currentfeats );
  146. /**
  147. * @brief load all data to is stream
  148. *
  149. * @param is input stream
  150. * @param format has no influence
  151. * @return void
  152. **/
  153. virtual void restore ( std::istream & is, int format = 0 );
  154. /**
  155. * @brief save all data to is stream
  156. *
  157. * @param os output stream
  158. * @param format has no influence
  159. * @return void
  160. **/
  161. virtual void store ( std::ostream & os, int format = 0 ) const;
  162. /**
  163. * @brief clean up
  164. *
  165. * @return void
  166. **/
  167. virtual void clear () {}
  168. };
  169. } // namespace
  170. #endif