SemSegContextTree.h 6.7 KB

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