SemSegContextTree.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. /** whether to run in 3D mode or not */
  29. bool run3Dseg;
  30. /** whether to use a particular feature type or not */
  31. bool useFeat0, useFeat1, useFeat2, useFeat3, useFeat4, useFeat5;
  32. /** array of usable feature types*/
  33. std::vector<int> featTypes;
  34. /** maximum samples for tree */
  35. int maxSamples;
  36. /** size for neighbourhood */
  37. int windowSize;
  38. /** multiplier for window size if context feature */
  39. int contextMultiplier;
  40. /** how many feats should be considered for a split */
  41. int featsPerSplit;
  42. /** count samples per label */
  43. std::map<int, int> labelcounter;
  44. /** map of labels */
  45. std::map<int, int> labelmap;
  46. /** map of labels inverse*/
  47. std::map<int, int> labelmapback;
  48. /** scalefactor for balancing for each class */
  49. std::vector<double> a;
  50. /** the minimum number of features allowed in a leaf */
  51. int minFeats;
  52. /** maximal depth of tree */
  53. int maxDepth;
  54. /** current depth for training */
  55. int depth;
  56. /** how many splittests */
  57. int randomTests;
  58. /** prototype operations for pairwise features */
  59. std::vector<std::vector<Operation*> > ops;
  60. /** use alternative calculation for information gain */
  61. bool useShannonEntropy;
  62. /** Classnames */
  63. ClassNames classnames;
  64. /** train selection */
  65. std::set<int> forbidden_classes;
  66. /** Configfile */
  67. const NICE::Config *conf;
  68. /** use pixelwise labeling or regionlabeling with additional segmenation */
  69. bool pixelWiseLabeling;
  70. /** Number of trees used for the forest */
  71. int nbTrees;
  72. /** use Gradient image or not */
  73. bool useGradient;
  74. /** use Color features from van de Weijer or not */
  75. bool useWeijer;
  76. /** use additional input Layer or not */
  77. bool useAdditionalLayer;
  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: grayvalue integral images
  86. * 3: context integral images
  87. * 4: simple context features
  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. /** file location of trained icf classifier */
  97. std::string fileLocation;
  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. NICE::GPHIKClassifier *fasthik;
  106. /** unique numbers for nodes */
  107. int uniquenumber;
  108. /**
  109. * @brief initOperations initialize the operation types
  110. */
  111. void initOperations();
  112. /**
  113. * @brief train the actual training method
  114. * @param trainp pointer to training data
  115. */
  116. void train ( const LabeledSet * trainp );
  117. public:
  118. /** simple constructor */
  119. SemSegContextTree ();
  120. /** constructor */
  121. SemSegContextTree ( const NICE::Config *conf, const MultiDataset *md );
  122. /** simple destructor */
  123. virtual ~SemSegContextTree();
  124. /**
  125. * classify each pixel of a single 3d image
  126. * @param imgData input data
  127. * @param segresult segmentation results
  128. * @param probabilities probabilities for each pixel
  129. */
  130. void classify ( NICE::MultiChannelImage3DT<double> &imgData,
  131. NICE::MultiChannelImageT<double> & segresult,
  132. NICE::MultiChannelImage3DT<double> & probabilities,
  133. const std::vector<std::string> & filelist );
  134. /**
  135. * the training method with checking for already existing trained classifier from file
  136. * @param md training data
  137. */
  138. void train ( const MultiDataset *md );
  139. bool active3DMode ()
  140. {
  141. return run3Dseg;
  142. }
  143. /**
  144. * @brief computes integral image of given feats
  145. *
  146. * @param nodeIndices matrix with current node for each feature
  147. * @param feats output image (must be initilized)
  148. * @param firstChannel index of the first channel
  149. * @return void
  150. **/
  151. void computeIntegralImage ( const NICE::MultiChannelImage3DT<unsigned short int> &nodeIndices, NICE::MultiChannelImage3DT<double> &feats, int firstChannel );
  152. /**
  153. * @brief computes ray feature images using canny filter
  154. * @param feats
  155. * @param channel
  156. * @return void
  157. */
  158. void computeRayFeatImage ( NICE::MultiChannelImage3DT<double> &feats, int firstChannel );
  159. /**
  160. * @brief reads image and does some simple convertions
  161. *
  162. * @param feats output image
  163. * @param currentFile image filename
  164. * @return void
  165. **/
  166. void addFeatureMaps ( NICE::MultiChannelImage3DT<double> &imgData, const std::vector<std::string> &filelist, int &amountRegions );
  167. /**
  168. * compute best split for current settings
  169. * @param feats features
  170. * @param nodeIndices matrix with current node for each feature
  171. * @param labels labels for each feature
  172. * @param node current node
  173. * @param splitfeat output selected feature dimension
  174. * @param splitval output threshold for selected feature
  175. * @return double best information gain value
  176. */
  177. 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 );
  178. /**
  179. * @brief computes the mean probability for a given class over all trees
  180. * @param x x position
  181. * @param y y position
  182. * @param z z position
  183. * @param channel current class
  184. * @param nodeIndices matrix with current node for each feature
  185. * @return double mean value
  186. **/
  187. inline double getMeanProb ( const int &x, const int &y, const int &z, const int &channel, const NICE::MultiChannelImage3DT<unsigned short int> &nodeIndices );
  188. /**
  189. * @brief load all data to is stream
  190. *
  191. * @param is input stream
  192. * @param format has no influence
  193. * @return void
  194. **/
  195. virtual void restore ( std::istream & is, int format = 0 );
  196. /**
  197. * @brief save all data to is stream
  198. *
  199. * @param os output stream
  200. * @param format has no influence
  201. * @return void
  202. **/
  203. virtual void store ( std::ostream & os, int format = 0 ) const;
  204. /**
  205. * @brief clean up
  206. *
  207. * @return void
  208. **/
  209. virtual void clear () {}
  210. };
  211. } // namespace
  212. #endif