SemSegContextTree.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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<std::vector<Operation*> > ops;
  56. std::vector<ValueAccess*> calcVal;
  57. /** use alternative calculation for information gain */
  58. bool useShannonEntropy;
  59. /** Classnames */
  60. ClassNames classnames;
  61. /** train selection */
  62. std::set<int> forbidden_classes;
  63. /** Configfile */
  64. const NICE::Config *conf;
  65. /** use pixelwise labeling or regionlabeling with additional segmenation */
  66. bool pixelWiseLabeling;
  67. /** Number of trees used for the forest */
  68. int nbTrees;
  69. /** use Gradient image or not */
  70. bool useGradient;
  71. /** use Color features from van de Weijer or not */
  72. bool useWeijer;
  73. /** use Regions as extra feature channel or not */
  74. bool useRegionFeature;
  75. /** how to handle each channel
  76. * 0: simple grayvalue features
  77. * 1: which pixel belongs to which region
  78. * 2: graycolor integral images
  79. * 3: context integral images
  80. * 4: context features (not in MultiChannelImageT encoded)
  81. */
  82. std::vector<int> channelType;
  83. /** list of channels per feature type */
  84. std::vector<std::vector<int> > channelsPerType;
  85. /** whether we should use the geometric features of Hoeim (only offline computation with MATLAB supported) */
  86. bool useHoiemFeatures;
  87. /** directory of the geometric features */
  88. std::string hoiemDirectory;
  89. /** first iteration or not */
  90. bool firstiteration;
  91. /** which IntegralImage channel belongs to which raw value channel */
  92. std::vector<std::pair<int, int> > integralMap;
  93. /** amount of grayvalue Channels */
  94. int rawChannels;
  95. public:
  96. /** simple constructor */
  97. SemSegContextTree ( const NICE::Config *conf, const MultiDataset *md );
  98. /** simple destructor */
  99. virtual ~SemSegContextTree();
  100. /**
  101. * test a single image
  102. * @param ce input data
  103. * @param segresult segmentation results
  104. * @param probabilities probabilities for each pixel
  105. */
  106. void semanticseg ( CachedExample *ce, NICE::Image & segresult, NICE::MultiChannelImageT<double> & probabilities );
  107. /**
  108. * the main training method
  109. * @param md training data
  110. */
  111. void train ( const MultiDataset *md );
  112. /**
  113. * @brief computes integral image of given feats
  114. *
  115. * @param currentfeats input features
  116. * @param integralImage output image (must be initilized)
  117. * @return void
  118. **/
  119. void computeIntegralImage ( const NICE::MultiChannelImageT<unsigned short int> &currentfeats, NICE::MultiChannelImageT<double> &lfeats,int firstChannel );
  120. /**
  121. * @brief reads image and does some simple convertions
  122. *
  123. * @param feats output image
  124. * @param currentFile image filename
  125. * @return void
  126. **/
  127. void extractBasicFeatures ( NICE::MultiChannelImageT<double> &feats, const NICE::ColorImage &img, const std::string &currentFile, int &amountRegions);
  128. /**
  129. * @brief computes integral image for Sparse Multichannel Image
  130. *
  131. * @param currentfeats input features
  132. * @param integralImage output image (must be initilized)
  133. * @return void
  134. **/
  135. void computeIntegralImage ( const NICE::MultiChannelImageT<NICE::SparseVectorInt> &infeats, NICE::MultiChannelImageT<NICE::SparseVectorInt> &integralImage );
  136. /**
  137. * compute best split for current settings
  138. * @param feats features
  139. * @param currentfeats matrix with current node for each feature
  140. * @param labels labels for each feature
  141. * @param node current node
  142. * @param splitfeat output feature position
  143. * @param splitval
  144. * @return best information gain
  145. */
  146. double getBestSplit ( std::vector<NICE::MultiChannelImageT<double> > &feats, std::vector<NICE::MultiChannelImageT<unsigned short int> > &currentfeats, const std::vector<NICE::MatrixT<int> > &labels, int node, Operation *&splitop, double &splitval, const int &tree, std::vector<std::vector<std::vector<double> > > &regionProbs );
  147. /**
  148. * @brief computes the mean probability for a given class over all trees
  149. * @param x x position
  150. * @param y y position
  151. * @param channel current class
  152. * @param currentfeats information about the nodes
  153. * @return double mean value
  154. **/
  155. inline double getMeanProb ( const int &x, const int &y, const int &channel, const NICE::MultiChannelImageT<unsigned short int> &currentfeats );
  156. /**
  157. * @brief load all data to is stream
  158. *
  159. * @param is input stream
  160. * @param format has no influence
  161. * @return void
  162. **/
  163. virtual void restore ( std::istream & is, int format = 0 );
  164. /**
  165. * @brief save all data to is stream
  166. *
  167. * @param os output stream
  168. * @param format has no influence
  169. * @return void
  170. **/
  171. virtual void store ( std::ostream & os, int format = 0 ) const;
  172. /**
  173. * @brief clean up
  174. *
  175. * @return void
  176. **/
  177. virtual void clear () {}
  178. };
  179. } // namespace
  180. #endif