SpectralCluster.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /**
  2. * @file SpectralCluster.cpp
  3. * @brief spectral clustering by kmeans-clustering of eigenvectors
  4. * @author Erik Rodner
  5. * @date 11/13/2007
  6. */
  7. #include <iostream>
  8. #include <core/vector/Distance.h>
  9. #include <core/vector/Eigen.h>
  10. #include <map>
  11. #include "vislearning/math/cluster/SpectralCluster.h"
  12. using namespace OBJREC;
  13. using namespace std;
  14. // refactor-nice.pl: check this substitution
  15. // old: using namespace ice;
  16. using namespace NICE;
  17. SpectralCluster::SpectralCluster ( int _noClasses, double alpha ) : noClasses(_noClasses), kmeans(_noClasses)
  18. {
  19. this->alpha = alpha;
  20. }
  21. SpectralCluster::~SpectralCluster()
  22. {
  23. }
  24. void SpectralCluster::getSimilarityMatrix ( const VVector & features,
  25. // refactor-nice.pl: check this substitution
  26. // old: Matrix & laplacian,
  27. NICE::Matrix & laplacian,
  28. double alpha )
  29. {
  30. // refactor-nice.pl: check this substitution
  31. // old: Matrix distances ( laplacian );
  32. NICE::Matrix distances ( laplacian );
  33. double mindist = numeric_limits<double>::max();
  34. double maxdist = - numeric_limits<double>::max();
  35. long int count = 0;
  36. double mean = 0.0;
  37. double stddev = 0.0;
  38. NICE::EuclidianDistance<double> distance;
  39. for ( int i = 0 ; i < (int)features.size() ; i++ )
  40. {
  41. // refactor-nice.pl: check this substitution
  42. // old: const Vector & xi = features[i];
  43. const NICE::Vector & xi = features[i];
  44. for ( int j = i ; j < (int)features.size() ; j++ )
  45. {
  46. // refactor-nice.pl: check this substitution
  47. // old: const Vector & xj = features[j];
  48. const NICE::Vector & xj = features[j];
  49. // double sim = xi * xj;
  50. double dist = distance.calculate ( xi, xj );
  51. // refactor-nice.pl: check this substitution
  52. // old: distances[i][j] = dist;
  53. distances(i, j) = dist;
  54. if ( dist < mindist )
  55. mindist = dist;
  56. if ( dist > maxdist )
  57. maxdist = dist;
  58. count++;
  59. mean += dist;
  60. }
  61. }
  62. mean /= count;
  63. for ( int i = 0 ; i < (int)features.size() ; i++ )
  64. for ( int j = i ; j < (int)features.size() ; j++ )
  65. {
  66. // refactor-nice.pl: check this substitution
  67. // old: double d = ( mean - distances[i][j] );
  68. double d = ( mean -distances(i, j) );
  69. stddev += d*d;
  70. }
  71. stddev /= count;
  72. double norm = alpha / (2.0 * stddev);
  73. for ( int i = 0 ; i < (int)features.size() ; i++ )
  74. for ( int j = i ; j < (int)features.size() ; j++ )
  75. {
  76. // refactor-nice.pl: check this substitution
  77. // old: double sim = exp(- distances[i][j] * norm );
  78. double sim = exp(-distances(i, j) * norm );
  79. // refactor-nice.pl: check this substitution
  80. // old: laplacian[i][j] = - sim;
  81. laplacian(i, j) = - sim;
  82. // refactor-nice.pl: check this substitution
  83. // old: laplacian[j][i] = - sim;
  84. laplacian(j, i) = - sim;
  85. }
  86. }
  87. void SpectralCluster::computeLaplacian ( const VVector & features,
  88. // refactor-nice.pl: check this substitution
  89. // old: Matrix & laplacian, int method, double alpha )
  90. NICE::Matrix & laplacian, int method, double alpha )
  91. {
  92. //unused:
  93. //int n = (int)features.size();
  94. laplacian.set(0.0);
  95. getSimilarityMatrix(features, laplacian, alpha);
  96. // refactor-nice.pl: check this substitution
  97. // old: Vector d ( laplacian.rows() );
  98. NICE::Vector d ( laplacian.rows() );
  99. d.set(0.0);
  100. for ( int i = 0 ; i < (int)laplacian.rows(); i++ )
  101. {
  102. for ( int j = 0 ; j < (int)laplacian.cols(); j++ )
  103. d[i] -=laplacian(i, j);
  104. }
  105. // Now we got the negative weight matrix laplacian : -W
  106. // and the degree matrix : D
  107. // calculate normalization
  108. // D^-1 * L
  109. if ( method == L_RW_NORMALIZED )
  110. {
  111. // L = D^-1 L_unnormalized = I - D^-1*W
  112. for ( int i = 0 ; i < (int)laplacian.rows() ; i++ )
  113. for ( int j = 0 ; j < (int)laplacian.cols() ; j++ )
  114. laplacian(i, j) *= (1.0/d[i]);
  115. for ( int i = 0 ; i < (int)laplacian.rows() ; i++ )
  116. laplacian(i, i) += 1.0;
  117. } else if ( method == L_UNNORMALIZED ) {
  118. // unnormalized version
  119. // L = D - W
  120. for ( int i = 0 ; i < (int)laplacian.rows() ; i++ )
  121. laplacian(i, i) += d[i];
  122. }
  123. }
  124. void SpectralCluster::cluster ( const VVector & features,
  125. VVector & prototypes,
  126. std::vector<double> & weights,
  127. std::vector<int> & assignment )
  128. {
  129. if ( features.size() <= 0 ) {
  130. fprintf (stderr, "FATAL ERROR: not enough features vectors provided\n");
  131. exit(-1);
  132. }
  133. // refactor-nice.pl: check this substitution
  134. // old: const Vector & x = features[0];
  135. const NICE::Vector & x = features[0];
  136. int dimension = x.size();
  137. // refactor-nice.pl: check this substitution
  138. // old: Matrix laplacian ( features.size(), features.size() );
  139. NICE::Matrix laplacian ( features.size(), features.size() );
  140. computeLaplacian ( features, laplacian, L_RW_NORMALIZED, alpha );
  141. //computeLaplacian ( features, laplacian, L_UNNORMALIZED, alpha );
  142. NICE::Matrix eigvect;
  143. NICE::Vector eigvals;
  144. NICE::eigenvectorvalues ( laplacian, eigvect, eigvals );
  145. // refactor-nice.pl: check this substitution
  146. // old: Matrix eigvals_sorted ( eigvals.size(), 1 );
  147. std::map<double, int> eigvals_sorted;
  148. for ( int i = 0 ; i < (int)eigvals.size(); i++ )
  149. {
  150. // refactor-nice.pl: check this substitution
  151. // old: eigvals_sorted[i][0] = i;
  152. eigvals_sorted.insert ( make_pair( eigvals[i], i ) );
  153. }
  154. // refactor: eigvals_sorted = eigvals_sorted || eigvals;
  155. // refactor: eigvals_sorted.Sort(1);
  156. VVector spectral_features;
  157. for ( int i = 0 ; i < (int)eigvect.rows() ; i++ )
  158. {
  159. // refactor-nice.pl: check this substitution
  160. // old: Vector eigvec_k ( noClasses );
  161. NICE::Vector eigvec_k ( noClasses );
  162. map<double, int>::const_iterator k = eigvals_sorted.begin();
  163. for ( int j = 0 ; j < noClasses ; j++ )
  164. {
  165. int eigval_index = k->second;
  166. // refactor-nice.pl: check this substitution
  167. // old: eigvec_k[j] = eigvect[i][ (int)eigvals_sorted[j][0] ] ;
  168. eigvec_k[j] = eigvect(i, eigval_index ) ;
  169. k++;
  170. }
  171. spectral_features.push_back ( eigvec_k );
  172. }
  173. kmeans.cluster ( spectral_features, prototypes, weights, assignment );
  174. // recompute prototypes
  175. for ( int k = 0 ; k < noClasses ; k++ )
  176. {
  177. prototypes[k].resize( dimension );
  178. prototypes[k].set(0);
  179. weights[k] = 0;
  180. }
  181. int j = 0;
  182. for ( VVector::const_iterator i = features.begin();
  183. i != features.end();
  184. i++, j++ )
  185. {
  186. int k = assignment[j];
  187. // refactor-nice.pl: check this substitution
  188. // old: Vector & p = prototypes[k];
  189. NICE::Vector & p = prototypes[k];
  190. // refactor-nice.pl: check this substitution
  191. // old: const Vector & x = *i;
  192. const NICE::Vector & x = *i;
  193. p += x;
  194. weights[k]++;
  195. }
  196. for ( int k = 0 ; k < noClasses ; k++ )
  197. {
  198. // refactor-nice.pl: check this substitution
  199. // old: Vector & p = prototypes[k];
  200. NICE::Vector & p = prototypes[k];
  201. if ( weights[k] <= 0 ) {
  202. fprintf (stderr, "FATAL ERROR: spectral clustering produced empty cluster !\n");
  203. exit(-1);
  204. }
  205. p *= ( 1.0 / weights[k] );
  206. weights[k] = weights[k] / features.size();
  207. }
  208. }