SemSegContextTree.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 "segmentation/RegionSegmentationMethod.h"
  13. #include "semseg3d/semseg/operations/Operations.h"
  14. #include "semseg3d/image/MultiChannelImage3DT.h"
  15. #include "gp-hik-exp/GPHIKClassifierNICE.h"
  16. namespace OBJREC {
  17. /** Localization system */
  18. class SemSegContextTree : public SemanticSegmentation, public NICE::Persistent
  19. {
  20. /** Segmentation Method */
  21. RegionSegmentationMethod *segmentation;
  22. /** tree -> saved as vector of nodes */
  23. std::vector<std::vector<TreeNode> > forest;
  24. /** local features */
  25. LFColorWeijer *lfcw;
  26. /** number of featuretype -> currently: local and context features = 2 */
  27. int ftypes;
  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 Edge features from Difference of Gaussian preprocessing or not */
  74. bool useGaussian;
  75. /** use Regions as extra feature channel or not */
  76. bool useRegionFeature;
  77. /** use external image categorization to avoid some classes */
  78. bool useCategorization;
  79. /** categorization information for external categorization */
  80. std::string cndir;
  81. /** how to handle each channel
  82. * 0: simple grayvalue features
  83. * 1: which pixel belongs to which region
  84. * 2: graycolor integral images
  85. * 3: context integral images
  86. * 4: context features (not in MultiChannelImageT encoded)
  87. */
  88. std::vector<int> channelType;
  89. /** list of channels per feature type */
  90. std::vector<std::vector<int> > channelsPerType;
  91. /** whether we should use the geometric features of Hoeim (only offline computation with MATLAB supported) */
  92. bool useHoiemFeatures;
  93. /** directory of the geometric features */
  94. std::string hoiemDirectory;
  95. /** first iteration or not */
  96. bool firstiteration;
  97. /** which IntegralImage channel belongs to which raw value channel */
  98. std::vector<std::pair<int, int> > integralMap;
  99. /** amount of grayvalue Channels */
  100. int rawChannels;
  101. /** classifier for categorization */
  102. OBJREC::GPHIKClassifierNICE *fasthik;
  103. /** unique numbers for nodes */
  104. int uniquenumber;
  105. public:
  106. /** simple constructor */
  107. SemSegContextTree ( const NICE::Config *conf, const MultiDataset *md );
  108. /** simple destructor */
  109. virtual ~SemSegContextTree();
  110. /**
  111. * test a single 3d image
  112. * @param imgData input data
  113. * @param segresult segmentation results
  114. * @param probabilities probabilities for each pixel
  115. */
  116. void semanticseg ( NICE::MultiChannelImage3DT<double> & imgData, NICE::MultiChannelImageT<double> & segresult, NICE::MultiChannelImage3DT<double> & probabilities, const std::vector<std::string> & filelist );
  117. /**
  118. * the main training method
  119. * @param md training data
  120. */
  121. void train ( const MultiDataset *md );
  122. /**
  123. * @brief computes integral image of given feats
  124. *
  125. * @param currentfeats input features
  126. * @param integralImage output image (must be initilized)
  127. * @return void
  128. **/
  129. void computeIntegralImage ( const NICE::MultiChannelImage3DT<unsigned short int> &currentfeats, NICE::MultiChannelImage3DT<double> &lfeats, int firstChannel );
  130. /**
  131. * @brief reads image and does some simple convertions
  132. *
  133. * @param feats output image
  134. * @param currentFile image filename
  135. * @return void
  136. **/
  137. void extractBasicFeatures ( NICE::MultiChannelImage3DT<double> &feats, const NICE::MultiChannelImage3DT<double> &imgData, const std::vector<std::string> &filelist, int &amountRegions);
  138. /**
  139. * compute best split for current settings
  140. * @param feats features
  141. * @param currentfeats matrix with current node for each feature
  142. * @param labels labels for each feature
  143. * @param node current node
  144. * @param splitfeat output feature position
  145. * @param splitval
  146. * @return best information gain
  147. */
  148. double getBestSplit ( std::vector<NICE::MultiChannelImage3DT<double> > &feats, std::vector<NICE::MultiChannelImage3DT<unsigned short int> > &currentfeats, const std::vector<NICE::MultiChannelImageT<int> > &labels, int node, Operation *&splitop, double &splitval, const int &tree, std::vector<std::vector<std::vector<double> > > &regionProbs );
  149. /**
  150. * @brief computes the mean probability for a given class over all trees
  151. * @param x x position
  152. * @param y y position
  153. * @param z z position
  154. * @param channel current class
  155. * @param currentfeats information about the nodes
  156. * @return double mean value
  157. **/
  158. inline double getMeanProb ( const int &x, const int &y, const int &z, const int &channel, const NICE::MultiChannelImage3DT<unsigned short int> &currentfeats );
  159. /**
  160. * @brief load all data to is stream
  161. *
  162. * @param is input stream
  163. * @param format has no influence
  164. * @return void
  165. **/
  166. virtual void restore ( std::istream & is, int format = 0 );
  167. /**
  168. * @brief save all data to is stream
  169. *
  170. * @param os output stream
  171. * @param format has no influence
  172. * @return void
  173. **/
  174. virtual void store ( std::ostream & os, int format = 0 ) const;
  175. /**
  176. * @brief clean up
  177. *
  178. * @return void
  179. **/
  180. virtual void clear () {}
  181. };
  182. } // namespace
  183. #endif