KMeans.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /**
  2. * @file KMeans.cpp
  3. * @brief K-Means
  4. * @author Erik Rodner, Alexander Freytag
  5. * @date 29-10-2007 (dd-mm-yyyy)
  6. */
  7. #include <iostream>
  8. #include "vislearning/math/cluster/KMeans.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_KMEANS
  15. KMeans::KMeans(const int & _noClasses, const std::string & _distanceType) :
  16. noClasses(_noClasses), distanceType(_distanceType)
  17. {
  18. //srand(time(NULL));
  19. this->distancefunction = GenericDistanceSelection::selectDistance(distanceType);
  20. }
  21. KMeans::KMeans( 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->d_minDelta = conf->gD( _section, "minDelta", 1e-5 );
  26. this->i_maxIterations = conf->gI( _section, "maxIterations", 200);
  27. this->noClasses = conf->gI( _section, "noClasses", 20);
  28. }
  29. KMeans::~KMeans()
  30. {
  31. }
  32. void KMeans::initial_guess(const NICE::VVector & features, NICE::VVector & prototypes)
  33. {
  34. int j = 0;
  35. std::set<int, std::greater<int> > mark;
  36. for (VVector::iterator i = prototypes.begin(); i != prototypes.end(); i++, j++)
  37. {
  38. int k;
  39. do
  40. {
  41. k = rand() % features.size();
  42. } while (mark.find(k) != mark.end());
  43. mark.insert(mark.begin(), k);
  44. *i = features[k];
  45. }
  46. }
  47. int KMeans::compute_prototypes(const VVector & features, VVector & prototypes,
  48. std::vector<double> & weights, const std::vector<int> & assignment)
  49. {
  50. int j = 0;
  51. // fprintf (stderr, "KMeans::compute_prototypes: init noClasses=%d\n", noClasses);
  52. for (int k = 0; k < this->noClasses; k++)
  53. {
  54. prototypes[k].set(0);
  55. weights[k] = 0;
  56. }
  57. // fprintf (stderr, "KMeans::compute_prototypes: compute means\n");
  58. for (VVector::const_iterator i = features.begin(); i != features.end(); i++, j++)
  59. {
  60. int k = assignment[j];
  61. NICE::Vector & p = prototypes[k];
  62. const NICE::Vector & x = *i;
  63. #ifdef DEBUG_KMEANS
  64. fprintf(
  65. stderr,
  66. "KMeans::compute_prototypes: std::vector %d has assignment %d\n",
  67. j, k);
  68. #endif
  69. p += x;
  70. #ifdef DEBUG_KMEANS
  71. std::cerr << "vector was : " << x << std::endl;
  72. std::cerr << "prototype for this class is now : " << p << std::endl;
  73. #endif
  74. weights[k]++;
  75. }
  76. // fprintf (stderr, "KMeans::compute_prototypes: scaling\n");
  77. for (int k = 0; k < this->noClasses; k++)
  78. {
  79. NICE::Vector & p = prototypes[k];
  80. #ifdef DEBUG_KMEANS
  81. std::cerr << "prototype for this class before scaling : " << p << std::endl;
  82. #endif
  83. if (weights[k] <= 0)
  84. {
  85. return -1;
  86. }
  87. p *= (1.0 / weights[k]);
  88. weights[k] = weights[k] / features.size();
  89. #ifdef DEBUG_KMEANS
  90. std::cerr << "prototype for this class after scaling with " << weights[k]
  91. << " : " << p << std::endl;
  92. #endif
  93. }
  94. return 0;
  95. }
  96. double KMeans::compute_delta(const NICE::VVector & oldprototypes,
  97. const NICE::VVector & prototypes)
  98. {
  99. double distance = 0;
  100. for (uint k = 0; k < oldprototypes.size(); k++)
  101. {
  102. distance += this->distancefunction->calculate(oldprototypes[k], prototypes[k]);
  103. #ifdef DEBUG_KMEANS
  104. fprintf(stderr, "KMeans::compute_delta: Distance: %f",
  105. distancefunction->calculate(oldprototypes[k], prototypes[k]));
  106. #endif
  107. }
  108. return distance;
  109. }
  110. double KMeans::compute_assignments(const NICE::VVector & features,
  111. const NICE::VVector & prototypes, std::vector<int> & assignment)
  112. {
  113. int index = 0;
  114. for (VVector::const_iterator i = features.begin(); i != features.end(); i++, index++)
  115. {
  116. const NICE::Vector & x = *i;
  117. double mindist = std::numeric_limits<double>::max();
  118. int minclass = 0;
  119. int c = 0;
  120. #ifdef DEBUG_KMEANS
  121. fprintf(stderr, "computing nearest prototype for std::vector %d\n",
  122. index);
  123. #endif
  124. for (VVector::const_iterator j = prototypes.begin(); j
  125. != prototypes.end(); j++, c++)
  126. {
  127. const NICE::Vector & p = *j;
  128. double distance = this->distancefunction->calculate(p, x);
  129. #ifdef DEBUG_KMEANS
  130. fprintf(stderr, "KMeans::compute_delta: Distance: %f",
  131. this->distancefunction->calculate(p, x));
  132. #endif
  133. #ifdef DEBUG_KMEANS
  134. std::cerr << p << std::endl;
  135. std::cerr << x << std::endl;
  136. fprintf(stderr, "distance to prototype %d is %f\n", c, distance);
  137. #endif
  138. if (distance < mindist)
  139. {
  140. minclass = c;
  141. mindist = distance;
  142. }
  143. }
  144. assignment[index] = minclass;
  145. }
  146. return 0.0;
  147. }
  148. double KMeans::compute_weights(const NICE::VVector & features,
  149. std::vector<double> & weights, std::vector<int> & assignment)
  150. {
  151. for (int k = 0; k < this->noClasses; k++)
  152. weights[k] = 0;
  153. int j = 0;
  154. for (NICE::VVector::const_iterator i = features.begin(); i != features.end(); i++, j++)
  155. {
  156. int k = assignment[j];
  157. weights[k]++;
  158. }
  159. for (int k = 0; k < this->noClasses; k++)
  160. weights[k] = weights[k] / features.size();
  161. return 0.0;
  162. }
  163. void KMeans::cluster(const NICE::VVector & features, NICE::VVector & prototypes,
  164. std::vector<double> & weights, std::vector<int> & assignment)
  165. {
  166. NICE::VVector oldprototypes;
  167. prototypes.clear();
  168. weights.clear();
  169. assignment.clear();
  170. weights.resize(noClasses, 0);
  171. assignment.resize(features.size(), 0);
  172. int dimension;
  173. if ((int) features.size() >= this->noClasses)
  174. dimension = features[0].size();
  175. else
  176. {
  177. fprintf(stderr,
  178. "FATAL ERROR: Not enough feature vectors provided for kMeans\n");
  179. exit(-1);
  180. }
  181. for (int k = 0; k < this->noClasses; k++)
  182. {
  183. prototypes.push_back(Vector(dimension));
  184. prototypes[k].set(0);
  185. }
  186. bool successKMeans ( false );
  187. int iterations ( 0 );
  188. double delta ( std::numeric_limits<double>::max() );
  189. while ( !successKMeans )
  190. {
  191. //we assume that this run will be successful
  192. successKMeans = true;
  193. this->initial_guess(features, prototypes);
  194. iterations = 0;
  195. delta = std::numeric_limits<double>::max();
  196. do
  197. {
  198. iterations++;
  199. this->compute_assignments(features, prototypes, assignment);
  200. if (iterations > 1)
  201. oldprototypes = prototypes;
  202. #ifdef DEBUG_KMEANS
  203. fprintf(stderr, "KMeans::cluster compute_prototypes\n");
  204. #endif
  205. if ( this->compute_prototypes(features, prototypes, weights, assignment) < 0 )
  206. {
  207. fprintf(stderr, "KMeans::cluster restart\n");
  208. successKMeans = false;
  209. break;
  210. }
  211. #ifdef DEBUG_KMEANS
  212. fprintf(stderr, "KMeans::cluster compute_delta\n");
  213. #endif
  214. if (iterations > 1)
  215. delta = this->compute_delta(oldprototypes, prototypes);
  216. #ifdef DEBUG_KMEANS
  217. print_iteration(iterations, prototypes, delta);
  218. #endif
  219. } while ((delta > d_minDelta) && (iterations < i_maxIterations));
  220. }
  221. #ifdef DEBUG_KMEANS
  222. fprintf(stderr, "KMeans::cluster: iterations = %d, delta = %f\n", iterations, delta);
  223. #endif
  224. this->compute_weights(features, weights, assignment);
  225. }
  226. void KMeans::print_iteration(int iterations, NICE::VVector & prototypes, double delta)
  227. {
  228. if (iterations > 1)
  229. fprintf(stderr, "KMeans::cluster: iteration=%d delta=%f\n", iterations,
  230. delta);
  231. else
  232. fprintf(stderr, "KMeans::cluster: iteration=%d\n", iterations);
  233. int k = 0;
  234. for (NICE::VVector::const_iterator i = prototypes.begin(); i != prototypes.end(); i++, k++)
  235. {
  236. fprintf(stderr, "class (%d)\n", k);
  237. std::cerr << "prototype = " << (*i) << std::endl;
  238. }
  239. }