SemSegContextTree.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. std::vector<ValueAccess*> calcVal;
  59. /** use alternative calculation for information gain */
  60. bool useShannonEntropy;
  61. /** Classnames */
  62. ClassNames classnames;
  63. /** train selection */
  64. std::set<int> forbidden_classes;
  65. /** Configfile */
  66. const NICE::Config *conf;
  67. /** use pixelwise labeling or regionlabeling with additional segmenation */
  68. bool pixelWiseLabeling;
  69. /** Number of trees used for the forest */
  70. int nbTrees;
  71. /** use Gradient image or not */
  72. bool useGradient;
  73. /** use Color features from van de Weijer or not */
  74. bool useWeijer;
  75. /** use additional input Layer or not */
  76. bool useAdditionalLayer;
  77. /** use Variance map features or not */
  78. bool useVariance;
  79. /** use Regions as extra feature channel or not */
  80. bool useRegionFeature;
  81. /** use external image categorization to avoid some classes */
  82. bool useCategorization;
  83. /** categorization information for external categorization */
  84. std::string cndir;
  85. /** how to handle each channel
  86. * 0: simple grayvalue features
  87. * 1: which pixel belongs to which region
  88. * 2: graycolor integral images
  89. * 3: context integral images
  90. * 4: context features (not in MultiChannelImageT encoded)
  91. */
  92. std::vector<int> channelType;
  93. /** list of channels per feature type */
  94. std::vector<std::vector<int> > channelsPerType;
  95. /** whether we should use the geometric features of Hoeim (only offline computation with MATLAB supported) */
  96. bool useHoiemFeatures;
  97. /** save / load trained icf classifier */
  98. bool saveLoadData;
  99. /** directory of the geometric features */
  100. std::string hoiemDirectory;
  101. /** file location of trained icf classifier */
  102. std::string fileLocation;
  103. /** first iteration or not */
  104. bool firstiteration;
  105. /** which IntegralImage channel belongs to which raw value channel */
  106. std::vector<std::pair<int, int> > integralMap;
  107. /** amount of grayvalue Channels */
  108. int rawChannels;
  109. /** classifier for categorization */
  110. NICE::GPHIKClassifier *fasthik;
  111. /** unique numbers for nodes */
  112. int uniquenumber;
  113. /**
  114. * the main training method
  115. * @param trainp pointer to training data
  116. */
  117. void train ( const LabeledSet * trainp );
  118. public:
  119. /** simple constructor */
  120. SemSegContextTree ( const NICE::Config *conf, const MultiDataset *md );
  121. /** simple destructor */
  122. virtual ~SemSegContextTree();
  123. /**
  124. * classify each pixel of a single 3d image
  125. * @param imgData input data
  126. * @param segresult segmentation results
  127. * @param probabilities probabilities for each pixel
  128. */
  129. void classify ( const NICE::MultiChannelImage3DT<double> &imgData,
  130. NICE::MultiChannelImageT<double> & segresult,
  131. NICE::MultiChannelImage3DT<double> & probabilities,
  132. const std::vector<std::string> & filelist );
  133. /**
  134. * the training method with checking for already existing trained classifier from file
  135. * @param md training data
  136. */
  137. void train ( const MultiDataset *md );
  138. /**
  139. * @brief computes integral image of given feats
  140. *
  141. * @param nodeIndices matrix with current node for each feature
  142. * @param integralImage output image (must be initilized)
  143. * @param firstChannel index of the first channel
  144. * @return void
  145. **/
  146. void computeIntegralImage ( const NICE::MultiChannelImage3DT<unsigned short int> &nodeIndices, NICE::MultiChannelImage3DT<double> &integralImage, int firstChannel );
  147. /**
  148. * @brief reads image and does some simple convertions
  149. *
  150. * @param feats output image
  151. * @param currentFile image filename
  152. * @return void
  153. **/
  154. void extractBasicFeatures ( NICE::MultiChannelImage3DT<double> &feats, const NICE::MultiChannelImage3DT<double> &imgData, const std::vector<std::string> &filelist, int &amountRegions );
  155. /**
  156. * compute best split for current settings
  157. * @param feats features
  158. * @param nodeIndices matrix with current node for each feature
  159. * @param labels labels for each feature
  160. * @param node current node
  161. * @param splitfeat output feature position
  162. * @param splitval
  163. * @return void
  164. */
  165. 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 );
  166. /**
  167. * @brief computes the mean probability for a given class over all trees
  168. * @param x x position
  169. * @param y y position
  170. * @param z z position
  171. * @param channel current class
  172. * @param nodeIndices matrix with current node for each feature
  173. * @return double mean value
  174. **/
  175. inline double getMeanProb ( const int &x, const int &y, const int &z, const int &channel, const NICE::MultiChannelImage3DT<unsigned short int> &nodeIndices );
  176. /**
  177. * @brief load all data to is stream
  178. *
  179. * @param is input stream
  180. * @param format has no influence
  181. * @return void
  182. **/
  183. virtual void restore ( std::istream & is, int format = 0 );
  184. /**
  185. * @brief save all data to is stream
  186. *
  187. * @param os output stream
  188. * @param format has no influence
  189. * @return void
  190. **/
  191. virtual void store ( std::ostream & os, int format = 0 ) const;
  192. /**
  193. * @brief clean up
  194. *
  195. * @return void
  196. **/
  197. virtual void clear () {}
  198. };
  199. } // namespace
  200. #endif