KMeansHeuristic.cpp 6.9 KB

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