DTBRandom.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /**
  2. * @file DTBRandom.cpp
  3. * @brief random decision tree
  4. * @author Erik Rodner
  5. * @date 05/06/2008
  6. */
  7. #include <iostream>
  8. #include "vislearning/classifier/fpclassifier/randomforest/DTBRandom.h"
  9. using namespace OBJREC;
  10. #undef DEBUGTREE
  11. #undef DETAILTREE
  12. using namespace std;
  13. using namespace NICE;
  14. DTBRandom::DTBRandom( const Config *conf, std::string section )
  15. {
  16. random_split_tests = conf->gI(section, "random_split_tests", 10 );
  17. random_features = conf->gI(section, "random_features", 500 );
  18. max_depth = conf->gI(section, "max_depth", 10 );
  19. minimum_information_gain = conf->gD(section, "minimum_information_gain", 10e-7 );
  20. minimum_entropy = conf->gD(section, "minimum_entropy", 10e-5 );
  21. use_shannon_entropy = conf->gB(section, "use_shannon_entropy", false );
  22. min_examples = conf->gI(section, "min_examples", 50);
  23. save_indices = conf->gB(section, "save_indices", false);
  24. if ( conf->gB(section, "start_random_generator", false ) )
  25. srand(time(NULL));
  26. }
  27. DTBRandom::~DTBRandom()
  28. {
  29. }
  30. bool DTBRandom::entropyLeftRight ( const FeatureValuesUnsorted & values,
  31. double threshold,
  32. double* stat_left,
  33. double* stat_right,
  34. double & entropy_left,
  35. double & entropy_right,
  36. double & count_left,
  37. double & count_right,
  38. int maxClassNo )
  39. {
  40. count_left = 0;
  41. count_right = 0;
  42. for ( FeatureValuesUnsorted::const_iterator i = values.begin(); i != values.end(); i++ )
  43. {
  44. int classno = i->second;
  45. double value = i->first;
  46. if ( value < threshold ) {
  47. stat_left[classno] += i->fourth;
  48. count_left+=i->fourth;
  49. }
  50. else
  51. {
  52. stat_right[classno] += i->fourth;
  53. count_right+=i->fourth;
  54. }
  55. }
  56. if ( (count_left == 0) || (count_right == 0) )
  57. return false;
  58. entropy_left = 0.0;
  59. for ( int j = 0 ; j <= maxClassNo ; j++ )
  60. if ( stat_left[j] != 0 )
  61. entropy_left -= stat_left[j] * log(stat_left[j]);
  62. entropy_left /= count_left;
  63. entropy_left += log(count_left);
  64. entropy_right = 0.0;
  65. for ( int j = 0 ; j <= maxClassNo ; j++ )
  66. if ( stat_right[j] != 0 )
  67. entropy_right -= stat_right[j] * log(stat_right[j]);
  68. entropy_right /= count_right;
  69. entropy_right += log (count_right);
  70. return true;
  71. }
  72. DecisionNode *DTBRandom::buildRecursive ( const FeaturePool & fp,
  73. const Examples & examples,
  74. vector<int> & examples_selection,
  75. FullVector & distribution,
  76. double e,
  77. int maxClassNo,
  78. int depth )
  79. {
  80. #ifdef DEBUGTREE
  81. fprintf (stderr, "Examples: %d (depth %d)\n", (int)examples_selection.size(),
  82. (int)depth);
  83. #endif
  84. DecisionNode *node = new DecisionNode ();
  85. node->distribution = distribution;
  86. if ( depth > max_depth ) {
  87. #ifdef DEBUGTREE
  88. fprintf (stderr, "DTBRandom: maxmimum depth reached !\n");
  89. #endif
  90. node->trainExamplesIndices = examples_selection;
  91. return node;
  92. }
  93. if ( (int)examples_selection.size() < min_examples ) {
  94. #ifdef DEBUGTREE
  95. fprintf (stderr, "DTBRandom: minimum examples reached %d < %d !\n",
  96. (int)examples_selection.size(), min_examples );
  97. #endif
  98. node->trainExamplesIndices = examples_selection;
  99. return node;
  100. }
  101. // REALLY BAD FIXME
  102. if ( (e <= minimum_entropy) && (e != 0.0) ) {
  103. //if ( e <= minimum_entropy ) {
  104. #ifdef DEBUGTREE
  105. fprintf (stderr, "DTBRandom: minimum entropy reached !\n");
  106. #endif
  107. node->trainExamplesIndices = examples_selection;
  108. return node;
  109. }
  110. Feature *best_feature = NULL;
  111. double best_threshold = 0.0;
  112. double best_ig = -1.0;
  113. FeatureValuesUnsorted best_values;
  114. FeatureValuesUnsorted values;
  115. double *best_distribution_left = new double [maxClassNo+1];
  116. double *best_distribution_right = new double [maxClassNo+1];
  117. double *distribution_left = new double [maxClassNo+1];
  118. double *distribution_right = new double [maxClassNo+1];
  119. double best_entropy_left = 0.0;
  120. double best_entropy_right = 0.0;
  121. for ( int k = 0 ; k < random_features ; k++ )
  122. {
  123. #ifdef DETAILTREE
  124. fprintf (stderr, "calculating random feature %d\n", k );
  125. #endif
  126. Feature *f = fp.getRandomFeature ();
  127. values.clear();
  128. f->calcFeatureValues ( examples, examples_selection, values );
  129. double minValue = (min_element ( values.begin(), values.end() ))->first;
  130. double maxValue = (max_element ( values.begin(), values.end() ))->first;
  131. #ifdef DETAILTREE
  132. fprintf (stderr, "max %f min %f\n", maxValue, minValue );
  133. #endif
  134. if ( maxValue - minValue < 1e-7 ) continue;
  135. for ( int i = 0 ; i < random_split_tests ; i++ )
  136. {
  137. double threshold;
  138. threshold = rand() * (maxValue - minValue ) / RAND_MAX + minValue;
  139. #ifdef DETAILTREE
  140. fprintf (stderr, "calculating split f/s(f) %d/%d %f\n", k, i, threshold );
  141. #endif
  142. double el, er;
  143. // clear distribution
  144. for ( int k = 0 ; k <= maxClassNo ; k++ )
  145. {
  146. distribution_left[k] = 0;
  147. distribution_right[k] = 0;
  148. }
  149. double count_left;
  150. double count_right;
  151. if ( ! entropyLeftRight ( values, threshold,
  152. distribution_left, distribution_right,
  153. el, er, count_left, count_right, maxClassNo ) )
  154. continue;
  155. double pl = (count_left) / (count_left + count_right);
  156. double ig = e - pl*el - (1-pl)*er;
  157. if ( use_shannon_entropy )
  158. {
  159. double esplit = - ( pl*log(pl) + (1-pl)*log(1-pl) );
  160. ig = 2*ig / ( e + esplit );
  161. }
  162. #ifdef DETAILTREE
  163. fprintf (stderr, "ig %f el %f er %f e %f\n", ig, el, er, e );
  164. assert ( ig >= -1e-7 );
  165. #endif
  166. if ( ig > best_ig )
  167. {
  168. best_ig = ig;
  169. best_threshold = threshold;
  170. #ifdef DETAILTREE
  171. fprintf (stderr, "t %f\n", best_threshold );
  172. #endif
  173. best_feature = f;
  174. for ( int k = 0 ; k <= maxClassNo ; k++ )
  175. {
  176. best_distribution_left[k] = distribution_left[k];
  177. best_distribution_right[k] = distribution_right[k];
  178. }
  179. best_entropy_left = el;
  180. best_entropy_right = er;
  181. }
  182. }
  183. }
  184. delete [] distribution_left;
  185. delete [] distribution_right;
  186. if ( best_ig < minimum_information_gain )
  187. {
  188. #ifdef DEBUGTREE
  189. fprintf (stderr, "DTBRandom: minimum information gain reached !\n");
  190. #endif
  191. delete [] best_distribution_left;
  192. delete [] best_distribution_right;
  193. node->trainExamplesIndices = examples_selection;
  194. return node;
  195. }
  196. node->f = best_feature->clone();
  197. node->threshold = best_threshold;
  198. // re calculating examples_left and examples_right
  199. vector<int> best_examples_left;
  200. vector<int> best_examples_right;
  201. values.clear();
  202. best_feature->calcFeatureValues ( examples, examples_selection, values );
  203. best_examples_left.reserve ( values.size() / 2 );
  204. best_examples_right.reserve ( values.size() / 2 );
  205. for ( FeatureValuesUnsorted::const_iterator i = values.begin();
  206. i != values.end();
  207. i++ )
  208. {
  209. double value = i->first;
  210. if ( value < best_threshold ) {
  211. best_examples_left.push_back ( i->third );
  212. } else {
  213. best_examples_right.push_back ( i->third );
  214. }
  215. }
  216. #ifdef DEBUGTREE
  217. node->f->store(cerr);
  218. cerr << endl;
  219. fprintf (stderr, "mutual information / shannon entropy %f entropy %f, left entropy %f right entropy %f\n", best_ig, e, best_entropy_left,
  220. best_entropy_right );
  221. #endif
  222. FullVector best_distribution_left_sparse ( distribution.size() );
  223. FullVector best_distribution_right_sparse ( distribution.size() );
  224. for ( int k = 0 ; k <= maxClassNo ; k++ )
  225. {
  226. double l = best_distribution_left[k];
  227. double r = best_distribution_right[k];
  228. if ( l != 0 )
  229. best_distribution_left_sparse[k] = l;
  230. if ( r != 0 )
  231. best_distribution_right_sparse[k] = r;
  232. #ifdef DEBUGTREE
  233. if ( (l>0)||(r>0) )
  234. fprintf (stderr, "DTBRandom: split of class %d (%f <-> %f)\n", k, l, r );
  235. #endif
  236. }
  237. delete [] best_distribution_left;
  238. delete [] best_distribution_right;
  239. node->left = buildRecursive ( fp, examples, best_examples_left,
  240. best_distribution_left_sparse, best_entropy_left, maxClassNo, depth+1 );
  241. node->right = buildRecursive ( fp, examples, best_examples_right,
  242. best_distribution_right_sparse, best_entropy_right, maxClassNo, depth+1 );
  243. return node;
  244. }
  245. DecisionNode *DTBRandom::build ( const FeaturePool & fp,
  246. const Examples & examples,
  247. int maxClassNo )
  248. {
  249. int index = 0;
  250. fprintf (stderr, "Feature Statistics (Geurts et al.): N=%d sqrt(N)=%lf K=%d\n",
  251. (int)fp.size(), sqrt((double)fp.size()), random_split_tests*random_features );
  252. FullVector distribution ( maxClassNo+1 );
  253. vector<int> all;
  254. all.reserve ( examples.size() );
  255. for ( Examples::const_iterator j = examples.begin();
  256. j != examples.end();
  257. j++ )
  258. {
  259. int classno = j->first;
  260. distribution[classno] += j->second.weight;
  261. all.push_back ( index );
  262. index++;
  263. }
  264. double entropy = 0.0;
  265. double sum = 0.0;
  266. for ( int i = 0 ; i < distribution.size(); i++ )
  267. {
  268. double val = distribution[i];
  269. if ( val <= 0.0 ) continue;
  270. entropy -= val*log(val);
  271. sum += val;
  272. }
  273. entropy /= sum;
  274. entropy += log(sum);
  275. return buildRecursive ( fp, examples, all, distribution, entropy, maxClassNo, 0 );
  276. }