SemSegContextTree.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 "objrec/segmentation/RegionSegmentationMethod.h"
  13. #include "objrec-froehlichexp/semseg/operations/Operations.h"
  14. #include "fast-hik/GPHIKClassifier.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. /** how to handle each channel
  77. * 0: simple grayvalue features
  78. * 1: which pixel belongs to which region
  79. * 2: graycolor integral images
  80. * 3: context integral images
  81. * 4: context features (not in MultiChannelImageT encoded)
  82. */
  83. std::vector<int> channelType;
  84. /** list of channels per feature type */
  85. std::vector<std::vector<int> > channelsPerType;
  86. /** whether we should use the geometric features of Hoeim (only offline computation with MATLAB supported) */
  87. bool useHoiemFeatures;
  88. /** directory of the geometric features */
  89. std::string hoiemDirectory;
  90. /** first iteration or not */
  91. bool firstiteration;
  92. /** which IntegralImage channel belongs to which raw value channel */
  93. std::vector<std::pair<int, int> > integralMap;
  94. /** amount of grayvalue Channels */
  95. int rawChannels;
  96. /** classifier for categorization */
  97. OBJREC::GPHIKClassifier *fasthik;
  98. /** unique numbers for nodes */
  99. int uniquenumber;
  100. public:
  101. /** simple constructor */
  102. SemSegContextTree ( const NICE::Config *conf, const MultiDataset *md );
  103. /** simple destructor */
  104. virtual ~SemSegContextTree();
  105. /**
  106. * test a single image
  107. * @param ce input data
  108. * @param segresult segmentation results
  109. * @param probabilities probabilities for each pixel
  110. */
  111. void semanticseg ( CachedExample *ce, NICE::Image & segresult, NICE::MultiChannelImageT<double> & probabilities );
  112. /**
  113. * the main training method
  114. * @param md training data
  115. */
  116. void train ( const MultiDataset *md );
  117. /**
  118. * @brief computes integral image of given feats
  119. *
  120. * @param currentfeats input features
  121. * @param integralImage output image (must be initilized)
  122. * @return void
  123. **/
  124. void computeIntegralImage ( const NICE::MultiChannelImageT<unsigned short int> &currentfeats, NICE::MultiChannelImageT<double> &lfeats,int firstChannel );
  125. /**
  126. * @brief reads image and does some simple convertions
  127. *
  128. * @param feats output image
  129. * @param currentFile image filename
  130. * @return void
  131. **/
  132. void extractBasicFeatures ( NICE::MultiChannelImageT<double> &feats, const NICE::ColorImage &img, const std::string &currentFile, int &amountRegions);
  133. /**
  134. * compute best split for current settings
  135. * @param feats features
  136. * @param currentfeats matrix with current node for each feature
  137. * @param labels labels for each feature
  138. * @param node current node
  139. * @param splitfeat output feature position
  140. * @param splitval
  141. * @return best information gain
  142. */
  143. 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 );
  144. /**
  145. * @brief computes the mean probability for a given class over all trees
  146. * @param x x position
  147. * @param y y position
  148. * @param channel current class
  149. * @param currentfeats information about the nodes
  150. * @return double mean value
  151. **/
  152. inline double getMeanProb ( const int &x, const int &y, const int &channel, const NICE::MultiChannelImageT<unsigned short int> &currentfeats );
  153. /**
  154. * @brief load all data to is stream
  155. *
  156. * @param is input stream
  157. * @param format has no influence
  158. * @return void
  159. **/
  160. virtual void restore ( std::istream & is, int format = 0 );
  161. /**
  162. * @brief save all data to is stream
  163. *
  164. * @param os output stream
  165. * @param format has no influence
  166. * @return void
  167. **/
  168. virtual void store ( std::ostream & os, int format = 0 ) const;
  169. /**
  170. * @brief clean up
  171. *
  172. * @return void
  173. **/
  174. virtual void clear () {}
  175. };
  176. } // namespace
  177. #endif