DTBRandomOblique.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /**
  2. * @file DTBRandomOblique.cpp
  3. * @brief random oblique decision tree
  4. * @author Sven Sickert
  5. * @date 10/15/2014
  6. */
  7. #include <iostream>
  8. #include <time.h>
  9. #include "DTBRandomOblique.h"
  10. #include "vislearning/features/fpfeatures/ConvolutionFeature.h"
  11. #include "core/vector/Algorithms.h"
  12. using namespace OBJREC;
  13. #define DEBUGTREE
  14. #undef DETAILTREE
  15. using namespace std;
  16. using namespace NICE;
  17. DTBRandomOblique::DTBRandomOblique ( const Config *conf, string section )
  18. {
  19. random_split_tests = conf->gI(section, "random_split_tests", 10 );
  20. max_depth = conf->gI(section, "max_depth", 10 );
  21. minimum_information_gain = conf->gD(section, "minimum_information_gain", 10e-7 );
  22. minimum_entropy = conf->gD(section, "minimum_entropy", 10e-5 );
  23. use_shannon_entropy = conf->gB(section, "use_shannon_entropy", false );
  24. min_examples = conf->gI(section, "min_examples", 50);
  25. save_indices = conf->gB(section, "save_indices", false);
  26. lambda = conf->gD(section, "lambda", 0.5 );
  27. }
  28. DTBRandomOblique::~DTBRandomOblique()
  29. {
  30. }
  31. bool DTBRandomOblique::entropyLeftRight (
  32. const FeatureValuesUnsorted & values,
  33. double threshold,
  34. double* stat_left,
  35. double* stat_right,
  36. double & entropy_left,
  37. double & entropy_right,
  38. double & count_left,
  39. double & count_right,
  40. int maxClassNo )
  41. {
  42. count_left = 0;
  43. count_right = 0;
  44. for ( FeatureValuesUnsorted::const_iterator i = values.begin(); i != values.end(); i++ )
  45. {
  46. int classno = i->second;
  47. double value = i->first;
  48. if ( value < threshold ) {
  49. stat_left[classno] += i->fourth;
  50. count_left+=i->fourth;
  51. }
  52. else
  53. {
  54. stat_right[classno] += i->fourth;
  55. count_right+=i->fourth;
  56. }
  57. }
  58. if ( (count_left == 0) || (count_right == 0) )
  59. return false;
  60. entropy_left = 0.0;
  61. for ( int j = 0 ; j <= maxClassNo ; j++ )
  62. if ( stat_left[j] != 0 )
  63. entropy_left -= stat_left[j] * log(stat_left[j]);
  64. entropy_left /= count_left;
  65. entropy_left += log(count_left);
  66. entropy_right = 0.0;
  67. for ( int j = 0 ; j <= maxClassNo ; j++ )
  68. if ( stat_right[j] != 0 )
  69. entropy_right -= stat_right[j] * log(stat_right[j]);
  70. entropy_right /= count_right;
  71. entropy_right += log (count_right);
  72. return true;
  73. }
  74. /** refresh data matrix X and label vector y */
  75. void DTBRandomOblique::getDataAndLabel(
  76. const FeaturePool &fp,
  77. const Examples &examples,
  78. const std::vector<int> &examples_selection,
  79. NICE::Matrix & matX,
  80. NICE::Vector & vecY )
  81. {
  82. ConvolutionFeature *f = (ConvolutionFeature*)fp.begin()->second;
  83. int amountParams = f->getParameterLength();
  84. int amountExamples = examples_selection.size();
  85. NICE::Matrix X(amountExamples, amountParams, 0.0 );
  86. NICE::Vector y(amountExamples, 0.0);
  87. int matIndex = 0;
  88. for ( vector<int>::const_iterator si = examples_selection.begin();
  89. si != examples_selection.end();
  90. si++ )
  91. {
  92. const pair<int, Example> & p = examples[*si];
  93. int classno = p.first;
  94. const Example & ce = p.second;
  95. NICE::Vector pixelRepr = f->getFeatureVector( &ce );
  96. pixelRepr /= pixelRepr.Max();
  97. // TODO for multiclass scenarios we need ONEvsALL!
  98. // {0,1} -> {-1,+1}
  99. double label = 2*classno-1;
  100. label *= ce.weight;
  101. pixelRepr *= ce.weight;
  102. y.set( matIndex, label );
  103. X.setRow(matIndex,pixelRepr);
  104. matIndex++;
  105. }
  106. matX = X;
  107. vecY = y;
  108. }
  109. /** recursive building method */
  110. DecisionNode *DTBRandomOblique::buildRecursive(
  111. const FeaturePool & fp,
  112. const Examples & examples,
  113. std::vector<int> & examples_selection,
  114. FullVector & distribution,
  115. double e,
  116. int maxClassNo,
  117. int depth)
  118. {
  119. #ifdef DEBUGTREE
  120. std::cerr << "Examples: " << (int)examples_selection.size()
  121. << " (depth " << (int)depth << ")" << std::endl;
  122. #endif
  123. // initialize new node
  124. DecisionNode *node = new DecisionNode ();
  125. node->distribution = distribution;
  126. // stop criteria: max_depth, min_examples, min_entropy
  127. if ( depth > max_depth
  128. || (int)examples_selection.size() < min_examples
  129. || ( (e <= minimum_entropy) && (e != 0.0) ) ) // FIXME
  130. {
  131. #ifdef DEBUGTREE
  132. std::cerr << "DTBRandomOblique: Stopping criteria applied!" << std::endl;
  133. #endif
  134. node->trainExamplesIndices = examples_selection;
  135. return node;
  136. }
  137. // refresh/set X and y
  138. NICE::Matrix X;
  139. NICE::Vector y;
  140. getDataAndLabel( fp, examples, examples_selection, X, y );
  141. NICE::Matrix XTX = X.transpose()*X;
  142. XTX.addDiagonal ( NICE::Vector( XTX.rows(), lambda) );
  143. NICE::Matrix G;
  144. NICE::Vector beta;
  145. choleskyDecomp(XTX, G);
  146. choleskyInvert(G, XTX);
  147. NICE::Matrix temp = XTX * X.transpose();
  148. beta.multiply(temp,y,false);
  149. // choleskySolve(G, y, beta );
  150. // variables
  151. double best_threshold = 0.0;
  152. double best_ig = -1.0;
  153. FeatureValuesUnsorted values;
  154. double *best_distribution_left = new double [maxClassNo+1];
  155. double *best_distribution_right = new double [maxClassNo+1];
  156. double *distribution_left = new double [maxClassNo+1];
  157. double *distribution_right = new double [maxClassNo+1];
  158. double best_entropy_left = 0.0;
  159. double best_entropy_right = 0.0;
  160. // Setting Convolutional Feature
  161. ConvolutionFeature *f = (ConvolutionFeature*)fp.begin()->second;
  162. f->setParameterVector( beta );
  163. // Feature Values
  164. values.clear();
  165. f->calcFeatureValues( examples, examples_selection, values);
  166. double minValue = (min_element ( values.begin(), values.end() ))->first;
  167. double maxValue = (max_element ( values.begin(), values.end() ))->first;
  168. if ( maxValue - minValue < 1e-7 )
  169. std::cerr << "DTBRandomOblique: Difference between min and max of features values to small!" << std::endl;
  170. // randomly chosen thresholds
  171. for ( int i = 0; i < random_split_tests; i++ )
  172. {
  173. double threshold = (i * (maxValue - minValue ) / (double)random_split_tests)
  174. + minValue;
  175. // preparations
  176. double el, er;
  177. for ( int k = 0 ; k <= maxClassNo ; k++ )
  178. {
  179. distribution_left[k] = 0.0;
  180. distribution_right[k] = 0.0;
  181. }
  182. /** Test the current split */
  183. // Does another split make sense?
  184. double count_left;
  185. double count_right;
  186. if ( ! entropyLeftRight ( values, threshold,
  187. distribution_left, distribution_right,
  188. el, er, count_left, count_right, maxClassNo ) )
  189. continue;
  190. // information gain and entropy
  191. double pl = (count_left) / (count_left + count_right);
  192. double ig = e - pl*el - (1-pl)*er;
  193. if ( use_shannon_entropy )
  194. {
  195. double esplit = - ( pl*log(pl) + (1-pl)*log(1-pl) );
  196. ig = 2*ig / ( e + esplit );
  197. }
  198. #ifdef DETAILTREE
  199. std::cerr << "Testing split #" << i << ": t=" << threshold
  200. << " ig=" << ig << std::endl;
  201. #endif
  202. if ( ig > best_ig )
  203. {
  204. best_ig = ig;
  205. best_threshold = threshold;
  206. for ( int k = 0 ; k <= maxClassNo ; k++ )
  207. {
  208. best_distribution_left[k] = distribution_left[k];
  209. best_distribution_right[k] = distribution_right[k];
  210. }
  211. best_entropy_left = el;
  212. best_entropy_right = er;
  213. }
  214. }
  215. //cleaning up
  216. delete [] distribution_left;
  217. delete [] distribution_right;
  218. // stop criteria: minimum information gain
  219. if ( best_ig < minimum_information_gain )
  220. {
  221. #ifdef DEBUGTREE
  222. std::cerr << "DTBRandomOblique: Minimum information gain reached!" << std::endl;
  223. #endif
  224. delete [] best_distribution_left;
  225. delete [] best_distribution_right;
  226. node->trainExamplesIndices = examples_selection;
  227. return node;
  228. }
  229. /** Save the best split to current node */
  230. node->f = f->clone();
  231. node->threshold = best_threshold;
  232. /** Split examples according to split function */
  233. vector<int> examples_left;
  234. vector<int> examples_right;
  235. examples_left.reserve ( values.size() / 2 );
  236. examples_right.reserve ( values.size() / 2 );
  237. for ( FeatureValuesUnsorted::const_iterator i = values.begin();
  238. i != values.end(); i++ )
  239. {
  240. double value = i->first;
  241. if ( value < best_threshold )
  242. examples_left.push_back ( i->third );
  243. else
  244. examples_right.push_back ( i->third );
  245. }
  246. #ifdef DEBUGTREE
  247. node->f->store( std::cerr );
  248. std::cerr << std::endl;
  249. std::cerr << "mutual information / shannon entropy " << best_ig << " entropy "
  250. << e << " left entropy " << best_entropy_left << " right entropy "
  251. << best_entropy_right << std::endl;
  252. #endif
  253. FullVector distribution_left_sparse ( distribution.size() );
  254. FullVector distribution_right_sparse ( distribution.size() );
  255. for ( int k = 0 ; k <= maxClassNo ; k++ )
  256. {
  257. double l = best_distribution_left[k];
  258. double r = best_distribution_right[k];
  259. if ( l != 0 )
  260. distribution_left_sparse[k] = l;
  261. if ( r != 0 )
  262. distribution_right_sparse[k] = r;
  263. #ifdef DEBUGTREE
  264. if ( (l>0)||(r>0) )
  265. {
  266. std::cerr << "DTBRandomOblique: split of class " << k << " ("
  267. << l << " <-> " << r << ") " << std::endl;
  268. }
  269. #endif
  270. }
  271. delete [] best_distribution_left;
  272. delete [] best_distribution_right;
  273. /** Recursion */
  274. // left child
  275. node->left = buildRecursive ( fp, examples, examples_left,
  276. distribution_left_sparse, best_entropy_left,
  277. maxClassNo, depth+1 );
  278. // right child
  279. node->right = buildRecursive ( fp, examples, examples_right,
  280. distribution_right_sparse, best_entropy_right,
  281. maxClassNo, depth+1 );
  282. return node;
  283. }
  284. /** initial building method */
  285. DecisionNode *DTBRandomOblique::build ( const FeaturePool & fp,
  286. const Examples & examples,
  287. int maxClassNo )
  288. {
  289. int index = 0;
  290. FullVector distribution ( maxClassNo+1 );
  291. vector<int> all;
  292. all.reserve ( examples.size() );
  293. for ( Examples::const_iterator j = examples.begin();
  294. j != examples.end(); j++ )
  295. {
  296. int classno = j->first;
  297. distribution[classno] += j->second.weight;
  298. all.push_back ( index );
  299. index++;
  300. }
  301. double entropy = 0.0;
  302. double sum = 0.0;
  303. for ( int i = 0 ; i < distribution.size(); i++ )
  304. {
  305. double val = distribution[i];
  306. if ( val <= 0.0 ) continue;
  307. entropy -= val*log(val);
  308. sum += val;
  309. }
  310. entropy /= sum;
  311. entropy += log(sum);
  312. return buildRecursive ( fp, examples, all, distribution, entropy, maxClassNo, 0 );
  313. }