SemSegContextTree.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 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. /** save / load trained icf classifier */
  97. bool saveLoadData;
  98. /** directory of the geometric features */
  99. std::string hoiemDirectory;
  100. /** file location of trained icf classifier */
  101. std::string fileLocation;
  102. /** first iteration or not */
  103. bool firstiteration;
  104. /** which IntegralImage channel belongs to which raw value channel */
  105. std::vector<std::pair<int, int> > integralMap;
  106. /** amount of grayvalue Channels */
  107. int rawChannels;
  108. /** classifier for categorization */
  109. NICE::GPHIKClassifier *fasthik;
  110. /** unique numbers for nodes */
  111. int uniquenumber;
  112. /**
  113. * the main training method
  114. * @param trainp pointer to training data
  115. */
  116. void train ( const LabeledSet * trainp );
  117. public:
  118. /** simple constructor */
  119. SemSegContextTree ( const NICE::Config *conf, const MultiDataset *md );
  120. /** simple destructor */
  121. virtual ~SemSegContextTree();
  122. /**
  123. * classify each pixel of a single 3d image
  124. * @param imgData input data
  125. * @param segresult segmentation results
  126. * @param probabilities probabilities for each pixel
  127. */
  128. void classify ( const NICE::MultiChannelImage3DT<double> &imgData,
  129. NICE::MultiChannelImageT<double> & segresult,
  130. NICE::MultiChannelImage3DT<double> & probabilities,
  131. const std::vector<std::string> & filelist );
  132. /**
  133. * the training method with checking for already existing trained classifier from file
  134. * @param md training data
  135. */
  136. void train ( const MultiDataset *md );
  137. /**
  138. * @brief computes integral image of given feats
  139. *
  140. * @param nodeIndices matrix with current node for each feature
  141. * @param integralImage output image (must be initilized)
  142. * @param firstChannel index of the first channel
  143. * @return void
  144. **/
  145. void computeIntegralImage ( const NICE::MultiChannelImage3DT<unsigned short int> &nodeIndices, NICE::MultiChannelImage3DT<double> &integralImage, int firstChannel );
  146. /**
  147. * @brief reads image and does some simple convertions
  148. *
  149. * @param feats output image
  150. * @param currentFile image filename
  151. * @return void
  152. **/
  153. void extractBasicFeatures ( NICE::MultiChannelImage3DT<double> &feats, const NICE::MultiChannelImage3DT<double> &imgData, const std::vector<std::string> &filelist, int &amountRegions );
  154. /**
  155. * compute best split for current settings
  156. * @param feats features
  157. * @param nodeIndices matrix with current node for each feature
  158. * @param labels labels for each feature
  159. * @param node current node
  160. * @param splitfeat output feature position
  161. * @param splitval
  162. * @return void
  163. */
  164. 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 );
  165. /**
  166. * @brief computes the mean probability for a given class over all trees
  167. * @param x x position
  168. * @param y y position
  169. * @param z z position
  170. * @param channel current class
  171. * @param nodeIndices matrix with current node for each feature
  172. * @return double mean value
  173. **/
  174. inline double getMeanProb ( const int &x, const int &y, const int &z, const int &channel, const NICE::MultiChannelImage3DT<unsigned short int> &nodeIndices );
  175. /**
  176. * @brief load all data to is stream
  177. *
  178. * @param is input stream
  179. * @param format has no influence
  180. * @return void
  181. **/
  182. virtual void restore ( std::istream & is, int format = 0 );
  183. /**
  184. * @brief save all data to is stream
  185. *
  186. * @param os output stream
  187. * @param format has no influence
  188. * @return void
  189. **/
  190. virtual void store ( std::ostream & os, int format = 0 ) const;
  191. /**
  192. * @brief clean up
  193. *
  194. * @return void
  195. **/
  196. virtual void clear () {}
  197. };
  198. } // namespace
  199. #endif