DTBRandomOblique.cpp 11 KB

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