SemSegContextTree.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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-vislearning includes
  12. #include <vislearning/features/localfeatures/LocalFeatureColorWeijer.h>
  13. #include <vislearning/classifier/fpclassifier/gphik/FPCGPHIK.h>
  14. // nice-segmentation includes
  15. #include <segmentation/RegionSegmentationMethod.h>
  16. // nice-semseg includes
  17. #include "semseg/semseg/operations/Operations.h"
  18. #include "SemanticSegmentation.h"
  19. namespace OBJREC {
  20. /** Localization system */
  21. class SemSegContextTree : public SemanticSegmentation
  22. {
  23. /** Segmentation Method */
  24. RegionSegmentationMethod *segmentation;
  25. /** tree -> saved as vector of nodes */
  26. std::vector<std::vector<TreeNode> > forest;
  27. /** local features */
  28. LocalFeatureColorWeijer *lfcw;
  29. /** number of featuretype -> currently: local and context features = 2 */
  30. int ftypes;
  31. /** maximum samples for tree */
  32. int maxSamples;
  33. /** size for neighbourhood */
  34. int windowSize;
  35. /** how many feats should be considered for a split */
  36. int featsPerSplit;
  37. /** count samples per label */
  38. std::map<int, int> labelcounter;
  39. /** map of labels */
  40. std::map<int, int> labelmap;
  41. /** map of labels inverse*/
  42. std::map<int, int> labelmapback;
  43. /** scalefactor for balancing for each class */
  44. std::vector<double> a;
  45. /** counter for used operations */
  46. std::vector<int> opOverview;
  47. /** relative use of context vs raw features per tree level*/
  48. std::vector<std::vector<double> > contextOverview;
  49. /** the minimum number of features allowed in a leaf */
  50. int minFeats;
  51. /** maximal depth of tree */
  52. int maxDepth;
  53. /** current depth for training */
  54. int depth;
  55. /** how many splittests */
  56. int randomTests;
  57. /** operations for pairwise features */
  58. std::vector<std::vector<Operation*> > ops;
  59. std::vector<ValueAccess*> calcVal;
  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 Regions as extra feature channel or not */
  77. bool useRegionFeature;
  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: graycolor integral images
  86. * 3: context integral images
  87. * 4: context features (not in MultiChannelImageT encoded)
  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. /** directory of the geometric features */
  95. std::string hoiemDirectory;
  96. /** first iteration or not */
  97. bool firstiteration;
  98. /** which IntegralImage channel belongs to which raw value channel */
  99. std::vector<std::pair<int, int> > integralMap;
  100. /** amount of grayvalue Channels */
  101. int rawChannels;
  102. /** classifier for categorization */
  103. OBJREC::FPCGPHIK *fasthik;
  104. /** unique numbers for nodes */
  105. int uniquenumber;
  106. public:
  107. /** simple constructor */
  108. SemSegContextTree ( const NICE::Config *conf, const MultiDataset *md );
  109. /** simple destructor */
  110. virtual ~SemSegContextTree();
  111. /**
  112. * test a single image
  113. * @param ce input data
  114. * @param segresult segmentation results
  115. * @param probabilities probabilities for each pixel
  116. */
  117. void semanticseg ( CachedExample *ce, NICE::ImageT<int> & segresult, NICE::MultiChannelImageT<double> & probabilities );
  118. void semanticseg ( CachedExample *ce,
  119. NICE::MultiChannelImageT<int> & segresult,
  120. NICE::MultiChannelImage3DT<double> & probabilities )
  121. {}
  122. /**
  123. * the main training method
  124. * @param md training data
  125. */
  126. void train ( const MultiDataset *md );
  127. /**
  128. * @brief computes integral image of given feats
  129. *
  130. * @param currentfeats input features
  131. * @param integralImage output image (must be initilized)
  132. * @return void
  133. **/
  134. void computeIntegralImage ( const NICE::MultiChannelImageT<unsigned short int> &currentfeats, NICE::MultiChannelImageT<double> &lfeats,int firstChannel );
  135. /**
  136. * @brief reads image and does some simple convertions
  137. *
  138. * @param feats output image
  139. * @param currentFile image filename
  140. * @return void
  141. **/
  142. void extractBasicFeatures ( NICE::MultiChannelImageT<double> &feats, const NICE::ColorImage &img, const std::string &currentFile, int &amountRegions);
  143. /**
  144. * compute best split for current settings
  145. * @param feats features
  146. * @param currentfeats matrix with current node for each feature
  147. * @param labels labels for each feature
  148. * @param node current node
  149. * @param splitfeat output feature position
  150. * @param splitval
  151. * @return best information gain
  152. */
  153. 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 );
  154. /**
  155. * @brief computes the mean probability for a given class over all trees
  156. * @param x x position
  157. * @param y y position
  158. * @param channel current class
  159. * @param currentfeats information about the nodes
  160. * @return double mean value
  161. **/
  162. inline double getMeanProb ( const int &x, const int &y, const int &channel, const NICE::MultiChannelImageT<unsigned short int> &currentfeats );
  163. /**
  164. * @brief load all data to is stream
  165. *
  166. * @param is input stream
  167. * @param format has no influence
  168. * @return void
  169. **/
  170. virtual void restore ( std::istream & is, int format = 0 );
  171. /**
  172. * @brief save all data to is stream
  173. *
  174. * @param os output stream
  175. * @param format has no influence
  176. * @return void
  177. **/
  178. virtual void store ( std::ostream & os, int format = 0 ) const;
  179. /**
  180. * @brief clean up
  181. *
  182. * @return void
  183. **/
  184. virtual void clear () {}
  185. };
  186. } // namespace
  187. #endif