SemSegContextTree.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. /** how many feats should be considered for a split */
  35. int featsPerSplit;
  36. /** count samples per label */
  37. std::map<int, int> labelcounter;
  38. /** map of labels */
  39. std::map<int, int> labelmap;
  40. /** map of labels inverse*/
  41. std::map<int, int> labelmapback;
  42. /** scalefactor for balancing for each class */
  43. std::vector<double> a;
  44. /** counter for used operations */
  45. std::vector<int> opOverview;
  46. /** relative use of context vs raw features per tree level*/
  47. std::vector<std::vector<double> > contextOverview;
  48. /** the minimum number of features allowed in a leaf */
  49. int minFeats;
  50. /** maximal depth of tree */
  51. int maxDepth;
  52. /** current depth for training */
  53. int depth;
  54. /** how many splittests */
  55. int randomTests;
  56. /** prototype operations for pairwise features */
  57. std::vector<std::vector<Operation*> > ops;
  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 Regions as extra feature channel or not */
  77. bool useRegionFeature;
  78. /** use external image categorization to avoid some classes */
  79. bool useCategorization;
  80. /** categorization information for external categorization */
  81. std::string cndir;
  82. /** how to handle each channel
  83. * 0: simple grayvalue features
  84. * 1: which pixel belongs to which region
  85. * 2: graycolor integral images
  86. * 3: context integral images
  87. * 4: context features (not in MultiChannelImageT encoded)
  88. */
  89. std::vector<int> channelType;
  90. /** list of channels per feature type */
  91. std::vector<std::vector<int> > channelsPerType;
  92. /** whether we should use the geometric features of Hoeim (only offline computation with MATLAB supported) */
  93. bool useHoiemFeatures;
  94. /** save / load trained icf classifier */
  95. bool saveLoadData;
  96. /** directory of the geometric features */
  97. std::string hoiemDirectory;
  98. /** file location of trained icf classifier */
  99. std::string fileLocation;
  100. /** first iteration or not */
  101. bool firstiteration;
  102. /** which IntegralImage channel belongs to which raw value channel */
  103. std::vector<std::pair<int, int> > integralMap;
  104. /** amount of grayvalue Channels */
  105. int rawChannels;
  106. /** classifier for categorization */
  107. NICE::GPHIKClassifier *fasthik;
  108. /** unique numbers for nodes */
  109. int uniquenumber;
  110. /**
  111. * the main training method
  112. * @param trainp pointer to training data
  113. */
  114. void train ( const LabeledSet * trainp );
  115. public:
  116. /** simple constructor */
  117. SemSegContextTree ( const NICE::Config *conf, const MultiDataset *md );
  118. /** simple destructor */
  119. virtual ~SemSegContextTree();
  120. /**
  121. * classify each pixel of a single 3d image
  122. * @param imgData input data
  123. * @param segresult segmentation results
  124. * @param probabilities probabilities for each pixel
  125. */
  126. void classify ( const NICE::MultiChannelImage3DT<double> &imgData,
  127. NICE::MultiChannelImageT<double> & segresult,
  128. NICE::MultiChannelImage3DT<double> & probabilities,
  129. const std::vector<std::string> & filelist );
  130. /**
  131. * the training method with checking for already existing trained classifier from file
  132. * @param md training data
  133. */
  134. void train ( const MultiDataset *md );
  135. /**
  136. * @brief computes integral image of given feats
  137. *
  138. * @param nodeIndices matrix with current node for each feature
  139. * @param integralImage output image (must be initilized)
  140. * @param firstChannel index of the first channel
  141. * @return void
  142. **/
  143. void computeIntegralImage ( const NICE::MultiChannelImage3DT<unsigned short int> &nodeIndices, NICE::MultiChannelImage3DT<double> &integralImage, int firstChannel );
  144. /**
  145. * @brief reads image and does some simple convertions
  146. *
  147. * @param feats output image
  148. * @param currentFile image filename
  149. * @return void
  150. **/
  151. void extractBasicFeatures ( NICE::MultiChannelImage3DT<double> &feats, const NICE::MultiChannelImage3DT<double> &imgData, const std::vector<std::string> &filelist, int &amountRegions );
  152. /**
  153. * compute best split for current settings
  154. * @param feats features
  155. * @param nodeIndices matrix with current node for each feature
  156. * @param labels labels for each feature
  157. * @param node current node
  158. * @param splitfeat output feature position
  159. * @param splitval
  160. * @return void
  161. */
  162. 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 );
  163. /**
  164. * @brief computes the mean probability for a given class over all trees
  165. * @param x x position
  166. * @param y y position
  167. * @param z z position
  168. * @param channel current class
  169. * @param nodeIndices matrix with current node for each feature
  170. * @return double mean value
  171. **/
  172. inline double getMeanProb ( const int &x, const int &y, const int &z, const int &channel, const NICE::MultiChannelImage3DT<unsigned short int> &nodeIndices );
  173. /**
  174. * @brief load all data to is stream
  175. *
  176. * @param is input stream
  177. * @param format has no influence
  178. * @return void
  179. **/
  180. virtual void restore ( std::istream & is, int format = 0 );
  181. /**
  182. * @brief save all data to is stream
  183. *
  184. * @param os output stream
  185. * @param format has no influence
  186. * @return void
  187. **/
  188. virtual void store ( std::ostream & os, int format = 0 ) const;
  189. /**
  190. * @brief clean up
  191. *
  192. * @return void
  193. **/
  194. virtual void clear () {}
  195. };
  196. } // namespace
  197. #endif