SemSegContextTree.h 8.3 KB

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