SemSegContextTree.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. // nice-core includes
  10. #include <core/vector/VVector.h>
  11. // nice-gphik-core includes
  12. #include <gp-hik-exp/GPHIKClassifierNICE.h>
  13. // nice-vislearning includes
  14. #include <vislearning/features/localfeatures/LocalFeatureColorWeijer.h>
  15. // nice-segmentation includes
  16. #include <segmentation/RegionSegmentationMethod.h>
  17. // nice-semseg includes
  18. #include "semseg/semseg/operations/Operations.h"
  19. #include "SemanticSegmentation.h"
  20. namespace OBJREC {
  21. /** Localization system */
  22. class SemSegContextTree : public SemanticSegmentation
  23. {
  24. /** Segmentation Method */
  25. RegionSegmentationMethod *segmentation;
  26. /** tree -> saved as vector of nodes */
  27. std::vector<std::vector<TreeNode> > forest;
  28. /** local features */
  29. LocalFeatureColorWeijer *lfcw;
  30. /** number of featuretype -> currently: local and context features = 2 */
  31. int ftypes;
  32. /** maximum samples for tree */
  33. int maxSamples;
  34. /** size for neighbourhood */
  35. int windowSize;
  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. /** counter for used operations */
  47. std::vector<int> opOverview;
  48. /** relative use of context vs raw features per tree level*/
  49. std::vector<std::vector<double> > contextOverview;
  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. /** operations for pairwise features */
  59. std::vector<std::vector<Operation*> > ops;
  60. std::vector<ValueAccess*> calcVal;
  61. /** use alternative calculation for information gain */
  62. bool useShannonEntropy;
  63. /** Classnames */
  64. ClassNames classnames;
  65. /** train selection */
  66. std::set<int> forbidden_classes;
  67. /** Configfile */
  68. const NICE::Config *conf;
  69. /** use pixelwise labeling or regionlabeling with additional segmenation */
  70. bool pixelWiseLabeling;
  71. /** Number of trees used for the forest */
  72. int nbTrees;
  73. /** use Gradient image or not */
  74. bool useGradient;
  75. /** use Color features from van de Weijer or not */
  76. bool useWeijer;
  77. /** use Regions as extra feature channel or not */
  78. bool useRegionFeature;
  79. /** use external image categorization to avoid some classes */
  80. bool useCategorization;
  81. /** categorization information for external categorization */
  82. std::string cndir;
  83. /** how to handle each channel
  84. * 0: simple grayvalue features
  85. * 1: which pixel belongs to which region
  86. * 2: graycolor integral images
  87. * 3: context integral images
  88. * 4: context features (not in MultiChannelImageT encoded)
  89. */
  90. std::vector<int> channelType;
  91. /** list of channels per feature type */
  92. std::vector<std::vector<int> > channelsPerType;
  93. /** whether we should use the geometric features of Hoeim (only offline computation with MATLAB supported) */
  94. bool useHoiemFeatures;
  95. /** directory of the geometric features */
  96. std::string hoiemDirectory;
  97. /** first iteration or not */
  98. bool firstiteration;
  99. /** which IntegralImage channel belongs to which raw value channel */
  100. std::vector<std::pair<int, int> > integralMap;
  101. /** amount of grayvalue Channels */
  102. int rawChannels;
  103. /** classifier for categorization */
  104. OBJREC::GPHIKClassifierNICE *fasthik;
  105. /** unique numbers for nodes */
  106. int uniquenumber;
  107. public:
  108. /** simple constructor */
  109. SemSegContextTree ( const NICE::Config *conf, const MultiDataset *md );
  110. /** simple destructor */
  111. virtual ~SemSegContextTree();
  112. /**
  113. * test a single image
  114. * @param ce input data
  115. * @param segresult segmentation results
  116. * @param probabilities probabilities for each pixel
  117. */
  118. void semanticseg ( CachedExample *ce, NICE::ImageT<int> & segresult, NICE::MultiChannelImageT<double> & probabilities );
  119. /**
  120. * the main training method
  121. * @param md training data
  122. */
  123. void train ( const MultiDataset *md );
  124. /**
  125. * @brief computes integral image of given feats
  126. *
  127. * @param currentfeats input features
  128. * @param integralImage output image (must be initilized)
  129. * @return void
  130. **/
  131. void computeIntegralImage ( const NICE::MultiChannelImageT<unsigned short int> &currentfeats, NICE::MultiChannelImageT<double> &lfeats,int firstChannel );
  132. /**
  133. * @brief reads image and does some simple convertions
  134. *
  135. * @param feats output image
  136. * @param currentFile image filename
  137. * @return void
  138. **/
  139. void extractBasicFeatures ( NICE::MultiChannelImageT<double> &feats, const NICE::ColorImage &img, const std::string &currentFile, int &amountRegions);
  140. /**
  141. * compute best split for current settings
  142. * @param feats features
  143. * @param currentfeats matrix with current node for each feature
  144. * @param labels labels for each feature
  145. * @param node current node
  146. * @param splitfeat output feature position
  147. * @param splitval
  148. * @return best information gain
  149. */
  150. double getBestSplit ( std::vector<NICE::MultiChannelImageT<double> > &feats, std::vector<NICE::MultiChannelImageT<unsigned short int> > &currentfeats, const std::vector<NICE::MatrixT<int> > &labels, int node, Operation *&splitop, double &splitval, const int &tree, std::vector<std::vector<std::vector<double> > > &regionProbs );
  151. /**
  152. * @brief computes the mean probability for a given class over all trees
  153. * @param x x position
  154. * @param y y position
  155. * @param channel current class
  156. * @param currentfeats information about the nodes
  157. * @return double mean value
  158. **/
  159. inline double getMeanProb ( const int &x, const int &y, const int &channel, const NICE::MultiChannelImageT<unsigned short int> &currentfeats );
  160. /**
  161. * @brief load all data to is stream
  162. *
  163. * @param is input stream
  164. * @param format has no influence
  165. * @return void
  166. **/
  167. virtual void restore ( std::istream & is, int format = 0 );
  168. /**
  169. * @brief save all data to is stream
  170. *
  171. * @param os output stream
  172. * @param format has no influence
  173. * @return void
  174. **/
  175. virtual void store ( std::ostream & os, int format = 0 ) const;
  176. /**
  177. * @brief clean up
  178. *
  179. * @return void
  180. **/
  181. virtual void clear () {}
  182. };
  183. } // namespace
  184. #endif