KMeansHeuristic.cpp 6.6 KB

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