SemSegContextTree.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. namespace OBJREC {
  14. class Operation;
  15. class TreeNode
  16. {
  17. public:
  18. /** left child node */
  19. int left;
  20. /** right child node */
  21. int right;
  22. /** position of feat for decision */
  23. Operation *feat;
  24. /** decision stamp */
  25. double decision;
  26. /** is the node a leaf or not */
  27. bool isleaf;
  28. /** distribution in current node */
  29. std::vector<double> dist;
  30. /** depth of the node in the tree */
  31. int depth;
  32. /** how many pixels are in this node */
  33. int featcounter;
  34. /** unique number */
  35. int nodeNumber;
  36. /** simple constructor */
  37. TreeNode() : left ( -1 ), right ( -1 ), feat ( NULL ), decision ( -1.0 ), isleaf ( false ) {}
  38. /** standard constructor */
  39. TreeNode ( int _left, int _right, Operation *_feat, double _decision ) : left ( _left ), right ( _right ), feat ( _feat ), decision ( _decision ), isleaf ( false ) {}
  40. };
  41. struct Features {
  42. NICE::MultiChannelImageT<double> *feats;
  43. MultiChannelImageT<unsigned short int> *cfeats;
  44. int cTree;
  45. std::vector<TreeNode> *tree;
  46. NICE::MultiChannelImageT<double> *integralImg;
  47. };
  48. enum ValueTypes
  49. {
  50. RAWFEAT,
  51. CONTEXT,
  52. SPARSE,
  53. NBVALUETYPES
  54. };
  55. class ValueAccess
  56. {
  57. public:
  58. virtual double getVal ( const Features &feats, const int &x, const int &y, const int &channel ) = 0;
  59. virtual std::string writeInfos() = 0;
  60. virtual ValueTypes getType() = 0;
  61. };
  62. enum OperationTypes {
  63. MINUS,
  64. MINUSABS,
  65. ADDITION,
  66. ONLY1,
  67. INTEGRAL,
  68. INTEGRALCENT,
  69. BIINTEGRALCENT,
  70. HAARHORIZ,
  71. HAARVERT,
  72. HAARDIAG,
  73. HAAR3HORIZ,
  74. HAAR3VERT,
  75. RELATIVEXPOSITION,
  76. RELATIVEYPOSITION,
  77. GLOBALFEATS,
  78. NBOPERATIONS
  79. };
  80. class Operation
  81. {
  82. protected:
  83. int x1, y1, x2, y2, channel1, channel2, maxtypes;
  84. ValueAccess *values;
  85. bool context;
  86. public:
  87. Operation()
  88. {
  89. values = NULL;
  90. maxtypes = 1000;
  91. }
  92. virtual void set ( int _x1, int _y1, int _x2, int _y2, int _channel1, int _channel2, ValueAccess *_values )
  93. {
  94. x1 = _x1;
  95. y1 = _y1;
  96. x2 = _x2;
  97. y2 = _y2;
  98. channel1 = _channel1;
  99. channel2 = _channel2;
  100. values = _values;
  101. }
  102. virtual void set ( int ws, int c1size, int c2size, int c3size, bool useGaussian );
  103. void setContext ( bool _context )
  104. {
  105. context = _context;
  106. }
  107. bool getContext()
  108. {
  109. return context;
  110. }
  111. /**
  112. * @brief abstract interface for feature computation
  113. * @param feats features
  114. * @param cfeats number of tree node for each pixel
  115. * @param tree current tree
  116. * @param x current x position
  117. * @param y current y position
  118. * @return double distance
  119. **/
  120. virtual double getVal ( const Features &feats, const int &x, const int &y ) = 0;
  121. virtual Operation* clone() = 0;
  122. virtual std::string writeInfos();
  123. inline void getXY ( const Features &feats, int &xsize, int &ysize )
  124. {
  125. xsize = feats.feats->width();
  126. ysize = feats.feats->height();
  127. }
  128. virtual OperationTypes getOps() = 0;
  129. virtual void store ( std::ostream & os )
  130. {
  131. os << x1 << " " << x2 << " " << y1 << " " << y2 << " " << channel1 << " " << channel2 << std::endl;
  132. if ( values == NULL )
  133. os << -1 << std::endl;
  134. else
  135. os << values->getType() << std::endl;
  136. }
  137. virtual void restore ( std::istream & is );
  138. };
  139. /** Localization system */
  140. class SemSegContextTree : public SemanticSegmentation, public NICE::Persistent
  141. {
  142. /** Segmentation Method */
  143. RegionSegmentationMethod *segmentation;
  144. /** tree -> saved as vector of nodes */
  145. std::vector<std::vector<TreeNode> > forest;
  146. /** local features */
  147. LFColorWeijer *lfcw;
  148. /** number of featuretype -> currently: local and context features = 2 */
  149. int ftypes;
  150. /** distance between features */
  151. int grid;
  152. /** maximum samples for tree */
  153. int maxSamples;
  154. /** size for neighbourhood */
  155. int windowSize;
  156. /** how many feats should be considered for a split */
  157. int featsPerSplit;
  158. /** count samples per label */
  159. std::map<int, int> labelcounter;
  160. /** map of labels */
  161. std::map<int, int> labelmap;
  162. /** map of labels inverse*/
  163. std::map<int, int> labelmapback;
  164. /** scalefactor for balancing for each class */
  165. std::vector<double> a;
  166. /** counter for used operations */
  167. std::vector<int> opOverview;
  168. /** relative use of context vs raw features per tree level*/
  169. std::vector<std::vector<double> > contextOverview;
  170. /** the minimum number of features allowed in a leaf */
  171. int minFeats;
  172. /** maximal depth of tree */
  173. int maxDepth;
  174. /** current depth for training */
  175. int depth;
  176. /** how many splittests */
  177. int randomTests;
  178. /** operations for pairwise features */
  179. std::vector<Operation*> ops;
  180. /** operations for pairwise context features */
  181. std::vector<Operation*> cops;
  182. std::vector<ValueAccess*> calcVal;
  183. /** vector of all possible features */
  184. std::vector<Operation*> featsel;
  185. /** use alternative calculation for information gain */
  186. bool useShannonEntropy;
  187. /** Classnames */
  188. ClassNames classnames;
  189. /** train selection */
  190. std::set<int> forbidden_classes;
  191. /** Configfile */
  192. const Config *conf;
  193. /** use pixelwise labeling or regionlabeling with additional segmenation */
  194. bool pixelWiseLabeling;
  195. /** use Gaussian distributed features based on the feature position */
  196. bool useGaussian;
  197. /** Number of trees used for the forest */
  198. int nbTrees;
  199. public:
  200. /** simple constructor */
  201. SemSegContextTree ( const NICE::Config *conf, const MultiDataset *md );
  202. /** simple destructor */
  203. virtual ~SemSegContextTree();
  204. /**
  205. * test a single image
  206. * @param ce input data
  207. * @param segresult segmentation results
  208. * @param probabilities probabilities for each pixel
  209. */
  210. void semanticseg ( CachedExample *ce, NICE::Image & segresult, NICE::MultiChannelImageT<double> & probabilities );
  211. /**
  212. * the main training method
  213. * @param md training data
  214. */
  215. void train ( const MultiDataset *md );
  216. /**
  217. * @brief computes integral image of given feats
  218. *
  219. * @param currentfeats input features
  220. * @param integralImage output image (must be initilized)
  221. * @return void
  222. **/
  223. void computeIntegralImage ( const NICE::MultiChannelImageT<unsigned short int> &currentfeats, const NICE::MultiChannelImageT<double> &lfeats, NICE::MultiChannelImageT<double> &integralImage );
  224. /**
  225. * @brief computes integral image for Sparse Multichannel Image
  226. *
  227. * @param currentfeats input features
  228. * @param integralImage output image (must be initilized)
  229. * @return void
  230. **/
  231. void computeIntegralImage ( const NICE::MultiChannelImageT<SparseVector> &infeats, NICE::MultiChannelImageT<SparseVector> &integralImage );
  232. /**
  233. * compute best split for current settings
  234. * @param feats features
  235. * @param currentfeats matrix with current node for each feature
  236. * @param labels labels for each feature
  237. * @param node current node
  238. * @param splitfeat output feature position
  239. * @param splitval
  240. * @return best information gain
  241. */
  242. double getBestSplit ( std::vector<NICE::MultiChannelImageT<double> > &feats, std::vector<NICE::MultiChannelImageT<unsigned short int> > &currentfeats, std::vector<NICE::MultiChannelImageT<double> > &integralImgs, const std::vector<NICE::MatrixT<int> > &labels, int node, Operation *&splitop, double &splitval, const int &tree );
  243. /**
  244. * @brief computes the mean probability for a given class over all trees
  245. * @param x x position
  246. * @param y y position
  247. * @param channel current class
  248. * @param currentfeats information about the nodes
  249. * @return double mean value
  250. **/
  251. inline double getMeanProb ( const int &x, const int &y, const int &channel, const MultiChannelImageT<unsigned short int> &currentfeats );
  252. /**
  253. * @brief load all data to is stream
  254. *
  255. * @param is input stream
  256. * @param format has no influence
  257. * @return void
  258. **/
  259. virtual void restore ( std::istream & is, int format = 0 );
  260. /**
  261. * @brief save all data to is stream
  262. *
  263. * @param os output stream
  264. * @param format has no influence
  265. * @return void
  266. **/
  267. virtual void store ( std::ostream & os, int format = 0 ) const;
  268. /**
  269. * @brief clean up
  270. *
  271. * @return void
  272. **/
  273. virtual void clear () {}
  274. };
  275. } // namespace
  276. #endif