SemSegContextTree.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. {
  18. /** Localization system */
  19. class SemSegContextTree : public SemanticSegmentation, public NICE::Persistent
  20. {
  21. /** Segmentation Method */
  22. RegionSegmentationMethod *segmentation;
  23. /** tree -> saved as vector of nodes */
  24. std::vector<std::vector<TreeNode> > forest;
  25. /** local features */
  26. LFColorWeijer *lfcw;
  27. /** number of featuretype -> currently: local and context features = 2 */
  28. int ftypes;
  29. /** maximum samples for tree */
  30. int maxSamples;
  31. /** size for neighbourhood */
  32. int windowSize;
  33. /** how many feats should be considered for a split */
  34. int featsPerSplit;
  35. /** count samples per label */
  36. std::map<int, int> labelcounter;
  37. /** map of labels */
  38. std::map<int, int> labelmap;
  39. /** map of labels inverse*/
  40. std::map<int, int> labelmapback;
  41. /** scalefactor for balancing for each class */
  42. std::vector<double> a;
  43. /** counter for used operations */
  44. std::vector<int> opOverview;
  45. /** relative use of context vs raw features per tree level*/
  46. std::vector<std::vector<double> > contextOverview;
  47. /** the minimum number of features allowed in a leaf */
  48. int minFeats;
  49. /** maximal depth of tree */
  50. int maxDepth;
  51. /** current depth for training */
  52. int depth;
  53. /** how many splittests */
  54. int randomTests;
  55. /** operations for pairwise features */
  56. std::vector<std::vector<Operation*> > ops;
  57. std::vector<ValueAccess*> calcVal;
  58. /** use alternative calculation for information gain */
  59. bool useShannonEntropy;
  60. /** Classnames */
  61. ClassNames classnames;
  62. /** train selection */
  63. std::set<int> forbidden_classes;
  64. /** Configfile */
  65. const NICE::Config *conf;
  66. /** use pixelwise labeling or regionlabeling with additional segmenation */
  67. bool pixelWiseLabeling;
  68. /** Number of trees used for the forest */
  69. int nbTrees;
  70. /** use Gradient image or not */
  71. bool useGradient;
  72. /** use Color features from van de Weijer or not */
  73. bool useWeijer;
  74. /** use additional input Layer or not */
  75. bool useAdditionalLayer;
  76. /** use Variance map features or not */
  77. bool useVariance;
  78. /** use Regions as extra feature channel or not */
  79. bool useRegionFeature;
  80. /** use external image categorization to avoid some classes */
  81. bool useCategorization;
  82. /** categorization information for external categorization */
  83. std::string cndir;
  84. /** how to handle each channel
  85. * 0: simple grayvalue features
  86. * 1: which pixel belongs to which region
  87. * 2: graycolor integral images
  88. * 3: context integral images
  89. * 4: context features (not in MultiChannelImageT encoded)
  90. */
  91. std::vector<int> channelType;
  92. /** list of channels per feature type */
  93. std::vector<std::vector<int> > channelsPerType;
  94. /** whether we should use the geometric features of Hoeim (only offline computation with MATLAB supported) */
  95. bool useHoiemFeatures;
  96. /** directory of the geometric features */
  97. std::string hoiemDirectory;
  98. /** first iteration or not */
  99. bool firstiteration;
  100. /** which IntegralImage channel belongs to which raw value channel */
  101. std::vector<std::pair<int, int> > integralMap;
  102. /** amount of grayvalue Channels */
  103. int rawChannels;
  104. /** classifier for categorization */
  105. OBJREC::GPHIKClassifierNICE *fasthik;
  106. /** unique numbers for nodes */
  107. int uniquenumber;
  108. public:
  109. /** simple constructor */
  110. SemSegContextTree ( const NICE::Config *conf, const MultiDataset *md );
  111. /** simple destructor */
  112. virtual ~SemSegContextTree();
  113. /**
  114. * test a single 3d image
  115. * @param imgData input data
  116. * @param segresult segmentation results
  117. * @param probabilities probabilities for each pixel
  118. */
  119. void semanticseg ( NICE::MultiChannelImage3DT<double> & imgData, NICE::MultiChannelImageT<double> & segresult, NICE::MultiChannelImage3DT<double> & probabilities, const std::vector<std::string> & filelist );
  120. /**
  121. * the main training method
  122. * @param md training data
  123. */
  124. void train ( const MultiDataset *md );
  125. /**
  126. * @brief computes integral image of given feats
  127. *
  128. * @param currentfeats input features
  129. * @param integralImage output image (must be initilized)
  130. * @return void
  131. **/
  132. void computeIntegralImage ( const NICE::MultiChannelImage3DT<unsigned short int> &currentfeats, NICE::MultiChannelImage3DT<double> &lfeats, int firstChannel );
  133. /**
  134. * @brief reads image and does some simple convertions
  135. *
  136. * @param feats output image
  137. * @param currentFile image filename
  138. * @return void
  139. **/
  140. void extractBasicFeatures ( NICE::MultiChannelImage3DT<double> &feats, const NICE::MultiChannelImage3DT<double> &imgData, const std::vector<std::string> &filelist, int &amountRegions );
  141. /**
  142. * compute best split for current settings
  143. * @param feats features
  144. * @param currentfeats matrix with current node for each feature
  145. * @param labels labels for each feature
  146. * @param node current node
  147. * @param splitfeat output feature position
  148. * @param splitval
  149. * @return best information gain
  150. */
  151. 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 );
  152. /**
  153. * @brief computes the mean probability for a given class over all trees
  154. * @param x x position
  155. * @param y y position
  156. * @param z z position
  157. * @param channel current class
  158. * @param currentfeats information about the nodes
  159. * @return double mean value
  160. **/
  161. inline double getMeanProb ( const int &x, const int &y, const int &z, const int &channel, const NICE::MultiChannelImage3DT<unsigned short int> &currentfeats );
  162. /**
  163. * @brief load all data to is stream
  164. *
  165. * @param is input stream
  166. * @param format has no influence
  167. * @return void
  168. **/
  169. virtual void restore ( std::istream & is, int format = 0 );
  170. /**
  171. * @brief save all data to is stream
  172. *
  173. * @param os output stream
  174. * @param format has no influence
  175. * @return void
  176. **/
  177. virtual void store ( std::ostream & os, int format = 0 ) const;
  178. /**
  179. * @brief clean up
  180. *
  181. * @return void
  182. **/
  183. virtual void clear () {}
  184. };
  185. } // namespace
  186. #endif