SemSegContextTree.h 6.9 KB

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