SemSegContextTree.h 8.7 KB

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