SemSegContextTree.h 8.0 KB

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