SemSegContextTree.h 6.4 KB

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