KMeans.cpp 8.6 KB

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