KMeansHeuristic.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /**
  2. * @file KMeansHeuristic.cpp
  3. * @brief K-Means
  4. * @author Erik Rodner, Michael Koch, Michael Trummer
  5. * @date 02/04/2011
  6. */
  7. #include <iostream>
  8. #include "vislearning/math/cluster/KMeansHeuristic.h"
  9. #include "vislearning/math/distances/genericDistance.h"
  10. #include <set>
  11. using namespace OBJREC;
  12. using namespace std;
  13. using namespace NICE;
  14. #undef DEBUG_KMeansHeuristic
  15. KMeansHeuristic::KMeansHeuristic(int _noClasses, string _distanceType) :
  16. noClasses(_noClasses), distanceType(_distanceType)
  17. {
  18. //srand(time(NULL));
  19. distancefunction = GenericDistanceSelection::selectDistance(distanceType);
  20. }
  21. KMeansHeuristic::~KMeansHeuristic()
  22. {
  23. }
  24. void KMeansHeuristic::initial_guess(const VVector & features,
  25. VVector & prototypes)
  26. {
  27. int j = 0;
  28. std::set<int, std::greater<int> > mark;
  29. for (VVector::iterator i = prototypes.begin(); i != prototypes.end(); i++, j++)
  30. {
  31. int k;
  32. do
  33. {
  34. k = rand() % features.size();
  35. } while (mark.find(k) != mark.end());
  36. mark.insert(mark.begin(), k);
  37. *i = features[k];
  38. }
  39. }
  40. // re-init cluster means
  41. int KMeansHeuristic::robust_prototypes(const VVector &features, VVector &prototypes,
  42. std::vector<double> & weights, const std::vector<int> & assignment)
  43. {
  44. if (features.size() > 0)
  45. {
  46. int dim = features[0].size();
  47. weights.assign(weights.size(), 0.0);
  48. int m = 0;
  49. for (VVector::iterator i = prototypes.begin(); i != prototypes.end(); i++, m++)
  50. {
  51. NICE::Vector & p = *i;
  52. if (isnan(p[0]))
  53. {
  54. continue;
  55. }
  56. int clustersize = 0;
  57. vector<int> clusterassign(features.size(), 0);
  58. for (int a = 0; a < (int) assignment.size(); a++)
  59. {
  60. if (assignment[a] == m)
  61. {
  62. clusterassign[a] = 1;
  63. clustersize++;
  64. }
  65. }
  66. //cout << "size " << clustersize << endl;
  67. int cnt = 0;
  68. while (cnt < clustersize/4)
  69. {
  70. //find feature with largest distance to the cluster mean
  71. double dist = 0, maxdist = 0;
  72. int maxdistind = 0;
  73. for (int a = 0; a < (int) clusterassign.size(); a++)
  74. {
  75. if (clusterassign[a] == 1)
  76. {
  77. dist = 0;
  78. dist += distancefunction->calculate(p, features[a]);
  79. if (dist > maxdist)
  80. {
  81. maxdist = dist;
  82. maxdistind = a;
  83. }
  84. }
  85. }
  86. //detach max-dist feature from the cluster
  87. clusterassign[maxdistind] = 0;
  88. cnt++;
  89. }
  90. //recalculate the cluster mean
  91. p=0.0;
  92. for (int a = 0; a < (int) clusterassign.size(); a++)
  93. {
  94. if (clusterassign[a] == 1)
  95. {
  96. if (isnan(features[a][0]))
  97. continue;
  98. p += features[a];
  99. weights[m]++;
  100. }
  101. }
  102. if (weights[m] <= 0)
  103. {
  104. return -1;
  105. }
  106. for (int d = 0; d < dim; d++)
  107. {
  108. p[d] /= weights[m];
  109. }
  110. #ifdef DEBUG_KMeansHeuristic
  111. cerr << "prototype for class" << m << ":" << p << endl;
  112. #endif
  113. }
  114. }
  115. return 0;
  116. }
  117. double KMeansHeuristic::compute_delta(const VVector & oldprototypes,
  118. const VVector & prototypes)
  119. {
  120. double distance = 0;
  121. for (uint k = 0; k < oldprototypes.size(); k++)
  122. distance
  123. += distancefunction->calculate(oldprototypes[k], prototypes[k]);
  124. return distance;
  125. }
  126. double KMeansHeuristic::compute_assignments(const VVector & features,
  127. const VVector & prototypes, std::vector<int> & assignment)
  128. {
  129. int index = 0;
  130. for (VVector::const_iterator i = features.begin(); i != features.end(); i++, index++)
  131. {
  132. const NICE::Vector & x = *i;
  133. double mindist = std::numeric_limits<double>::max();
  134. int minclass = 0;
  135. int c = 0;
  136. #ifdef DEBUG_KMeansHeuristic
  137. fprintf(stderr, "computing nearest prototype for std::vector %d\n",
  138. index);
  139. #endif
  140. for (VVector::const_iterator j = prototypes.begin(); j
  141. != prototypes.end(); j++, c++)
  142. {
  143. const NICE::Vector & p = *j;
  144. double distance = distancefunction->calculate(p, x);
  145. #ifdef DEBUG_KMeansHeuristic
  146. fprintf(stderr, "distance to prototype %d is %f\n", c, distance);
  147. #endif
  148. if (distance < mindist)
  149. {
  150. minclass = c;
  151. mindist = distance;
  152. }
  153. }
  154. assignment[index] = minclass;
  155. }
  156. return 0.0;
  157. }
  158. double KMeansHeuristic::compute_weights(const VVector & features, std::vector<
  159. double> & weights, std::vector<int> & assignment)
  160. {
  161. for (int k = 0; k < noClasses; k++)
  162. weights[k] = 0;
  163. int j = 0;
  164. for (VVector::const_iterator i = features.begin(); i != features.end(); i++, j++)
  165. {
  166. int k = assignment[j];
  167. weights[k]++;
  168. }
  169. for (int k = 0; k < noClasses; k++)
  170. weights[k] = weights[k] / features.size();
  171. return 0.0;
  172. }
  173. void KMeansHeuristic::cluster(const VVector & features, VVector & prototypes,
  174. std::vector<double> & weights, std::vector<int> & assignment)
  175. {
  176. VVector oldprototypes;
  177. prototypes.clear();
  178. weights.clear();
  179. assignment.clear();
  180. weights.resize(noClasses, 0);
  181. assignment.resize(features.size(), 0);
  182. int dimension;
  183. if ((int) features.size() >= noClasses)
  184. dimension = features[0].size();
  185. else
  186. {
  187. fprintf(stderr,
  188. "FATAL ERROR: Not enough feature vectors provided for KMeansHeuristic\n");
  189. exit(-1);
  190. }
  191. for (int k = 0; k < noClasses; k++)
  192. {
  193. prototypes.push_back(Vector(dimension));
  194. prototypes[k].set(0);
  195. }
  196. KMeansHeuristic_Restart:
  197. initial_guess(features, prototypes);
  198. int iterations = 0;
  199. double delta = std::numeric_limits<double>::max();
  200. const double minDelta = 1e-5;
  201. const int maxIterations = 200;
  202. do
  203. {
  204. iterations++;
  205. compute_assignments(features, prototypes, assignment);
  206. if (iterations > 1)
  207. oldprototypes = prototypes;
  208. #ifdef DEBUG_KMeansHeuristic
  209. fprintf(stderr, "KMeansHeuristic::cluster compute_prototypes\n");
  210. #endif
  211. //if (compute_prototypes(features, prototypes, weights, assignment) < 0)
  212. if (robust_prototypes(features, prototypes, weights, assignment) < 0)
  213. {
  214. fprintf(stderr, "KMeansHeuristic::cluster restart\n");
  215. goto KMeansHeuristic_Restart;
  216. }
  217. #ifdef DEBUG_KMeansHeuristic
  218. fprintf(stderr, "KMeansHeuristic::cluster compute_delta\n");
  219. #endif
  220. if (iterations > 1)
  221. delta = compute_delta(oldprototypes, prototypes);
  222. #ifdef DEBUG_KMeansHeuristic
  223. print_iteration(iterations, prototypes, delta);
  224. #endif
  225. } while ((delta > minDelta) && (iterations < maxIterations));
  226. #ifdef DEBUG_KMeansHeuristic
  227. fprintf(stderr, "KMeansHeuristic::cluster: iterations = %d, delta = %f\n",
  228. iterations, delta);
  229. #endif
  230. compute_weights(features, weights, assignment);
  231. }
  232. void KMeansHeuristic::print_iteration(int iterations, VVector & prototypes,
  233. double delta)
  234. {
  235. if (iterations > 1)
  236. fprintf(stderr, "KMeansHeuristic::cluster: iteration=%d delta=%f\n",
  237. iterations, delta);
  238. else
  239. fprintf(stderr, "KMeansHeuristic::cluster: iteration=%d\n", iterations);
  240. int k = 0;
  241. for (VVector::const_iterator i = prototypes.begin(); i != prototypes.end(); i++, k++)
  242. {
  243. fprintf(stderr, "class (%d)\n", k);
  244. cerr << "prototype = " << (*i) << endl;
  245. }
  246. }