SemSegContextTree.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. /**
  119. * the main training method
  120. * @param md training data
  121. */
  122. void train ( const MultiDataset *md );
  123. /**
  124. * @brief computes integral image of given feats
  125. *
  126. * @param currentfeats input features
  127. * @param integralImage output image (must be initilized)
  128. * @return void
  129. **/
  130. void computeIntegralImage ( const NICE::MultiChannelImageT<unsigned short int> &currentfeats, NICE::MultiChannelImageT<double> &lfeats,int firstChannel );
  131. /**
  132. * @brief reads image and does some simple convertions
  133. *
  134. * @param feats output image
  135. * @param currentFile image filename
  136. * @return void
  137. **/
  138. void extractBasicFeatures ( NICE::MultiChannelImageT<double> &feats, const NICE::ColorImage &img, const std::string &currentFile, int &amountRegions);
  139. /**
  140. * compute best split for current settings
  141. * @param feats features
  142. * @param currentfeats matrix with current node for each feature
  143. * @param labels labels for each feature
  144. * @param node current node
  145. * @param splitfeat output feature position
  146. * @param splitval
  147. * @return best information gain
  148. */
  149. 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 );
  150. /**
  151. * @brief computes the mean probability for a given class over all trees
  152. * @param x x position
  153. * @param y y position
  154. * @param channel current class
  155. * @param currentfeats information about the nodes
  156. * @return double mean value
  157. **/
  158. inline double getMeanProb ( const int &x, const int &y, const int &channel, const NICE::MultiChannelImageT<unsigned short int> &currentfeats );
  159. /**
  160. * @brief load all data to is stream
  161. *
  162. * @param is input stream
  163. * @param format has no influence
  164. * @return void
  165. **/
  166. virtual void restore ( std::istream & is, int format = 0 );
  167. /**
  168. * @brief save all data to is stream
  169. *
  170. * @param os output stream
  171. * @param format has no influence
  172. * @return void
  173. **/
  174. virtual void store ( std::ostream & os, int format = 0 ) const;
  175. /**
  176. * @brief clean up
  177. *
  178. * @return void
  179. **/
  180. virtual void clear () {}
  181. };
  182. } // namespace
  183. #endif