|
@@ -16,8 +16,6 @@
|
|
|
using namespace OBJREC;
|
|
|
|
|
|
using namespace std;
|
|
|
-// refactor-nice.pl: check this substitution
|
|
|
-// old: using namespace ice;
|
|
|
using namespace NICE;
|
|
|
|
|
|
SpectralCluster::SpectralCluster ( int _noClasses, double alpha ) : noClasses(_noClasses), kmeans(_noClasses)
|
|
@@ -30,13 +28,9 @@ SpectralCluster::~SpectralCluster()
|
|
|
}
|
|
|
|
|
|
void SpectralCluster::getSimilarityMatrix ( const VVector & features,
|
|
|
- // refactor-nice.pl: check this substitution
|
|
|
- // old: Matrix & laplacian,
|
|
|
NICE::Matrix & laplacian,
|
|
|
double alpha )
|
|
|
{
|
|
|
- // refactor-nice.pl: check this substitution
|
|
|
- // old: Matrix distances ( laplacian );
|
|
|
NICE::Matrix distances ( laplacian );
|
|
|
double mindist = numeric_limits<double>::max();
|
|
|
double maxdist = - numeric_limits<double>::max();
|
|
@@ -47,18 +41,12 @@ void SpectralCluster::getSimilarityMatrix ( const VVector & features,
|
|
|
|
|
|
for ( int i = 0 ; i < (int)features.size() ; i++ )
|
|
|
{
|
|
|
- // refactor-nice.pl: check this substitution
|
|
|
- // old: const Vector & xi = features[i];
|
|
|
const NICE::Vector & xi = features[i];
|
|
|
for ( int j = i ; j < (int)features.size() ; j++ )
|
|
|
{
|
|
|
- // refactor-nice.pl: check this substitution
|
|
|
- // old: const Vector & xj = features[j];
|
|
|
const NICE::Vector & xj = features[j];
|
|
|
// double sim = xi * xj;
|
|
|
double dist = distance.calculate ( xi, xj );
|
|
|
- // refactor-nice.pl: check this substitution
|
|
|
- // old: distances[i][j] = dist;
|
|
|
distances(i, j) = dist;
|
|
|
|
|
|
if ( dist < mindist )
|
|
@@ -75,9 +63,7 @@ void SpectralCluster::getSimilarityMatrix ( const VVector & features,
|
|
|
for ( int i = 0 ; i < (int)features.size() ; i++ )
|
|
|
for ( int j = i ; j < (int)features.size() ; j++ )
|
|
|
{
|
|
|
- // refactor-nice.pl: check this substitution
|
|
|
- // old: double d = ( mean - distances[i][j] );
|
|
|
- double d = ( mean -distances(i, j) );
|
|
|
+ double d = ( mean - distances(i, j) );
|
|
|
stddev += d*d;
|
|
|
}
|
|
|
|
|
@@ -88,31 +74,19 @@ void SpectralCluster::getSimilarityMatrix ( const VVector & features,
|
|
|
for ( int i = 0 ; i < (int)features.size() ; i++ )
|
|
|
for ( int j = i ; j < (int)features.size() ; j++ )
|
|
|
{
|
|
|
- // refactor-nice.pl: check this substitution
|
|
|
- // old: double sim = exp(- distances[i][j] * norm );
|
|
|
double sim = exp(-distances(i, j) * norm );
|
|
|
|
|
|
- // refactor-nice.pl: check this substitution
|
|
|
- // old: laplacian[i][j] = - sim;
|
|
|
laplacian(i, j) = - sim;
|
|
|
- // refactor-nice.pl: check this substitution
|
|
|
- // old: laplacian[j][i] = - sim;
|
|
|
laplacian(j, i) = - sim;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
void SpectralCluster::computeLaplacian ( const VVector & features,
|
|
|
- // refactor-nice.pl: check this substitution
|
|
|
- // old: Matrix & laplacian, int method, double alpha )
|
|
|
NICE::Matrix & laplacian, int method, double alpha )
|
|
|
{
|
|
|
- //unused:
|
|
|
- //int n = (int)features.size();
|
|
|
laplacian.set(0.0);
|
|
|
getSimilarityMatrix(features, laplacian, alpha);
|
|
|
|
|
|
- // refactor-nice.pl: check this substitution
|
|
|
- // old: Vector d ( laplacian.rows() );
|
|
|
NICE::Vector d ( laplacian.rows() );
|
|
|
d.set(0.0);
|
|
|
for ( int i = 0 ; i < (int)laplacian.rows(); i++ )
|
|
@@ -155,13 +129,9 @@ void SpectralCluster::cluster ( const VVector & features,
|
|
|
exit(-1);
|
|
|
}
|
|
|
|
|
|
- // refactor-nice.pl: check this substitution
|
|
|
- // old: const Vector & x = features[0];
|
|
|
const NICE::Vector & x = features[0];
|
|
|
int dimension = x.size();
|
|
|
|
|
|
- // refactor-nice.pl: check this substitution
|
|
|
- // old: Matrix laplacian ( features.size(), features.size() );
|
|
|
NICE::Matrix laplacian ( features.size(), features.size() );
|
|
|
|
|
|
computeLaplacian ( features, laplacian, L_RW_NORMALIZED, alpha );
|
|
@@ -171,31 +141,21 @@ void SpectralCluster::cluster ( const VVector & features,
|
|
|
NICE::Vector eigvals;
|
|
|
NICE::eigenvectorvalues ( laplacian, eigvect, eigvals );
|
|
|
|
|
|
- // refactor-nice.pl: check this substitution
|
|
|
- // old: Matrix eigvals_sorted ( eigvals.size(), 1 );
|
|
|
std::map<double, int> eigvals_sorted;
|
|
|
for ( int i = 0 ; i < (int)eigvals.size(); i++ )
|
|
|
{
|
|
|
- // refactor-nice.pl: check this substitution
|
|
|
- // old: eigvals_sorted[i][0] = i;
|
|
|
eigvals_sorted.insert ( make_pair( eigvals[i], i ) );
|
|
|
}
|
|
|
- // refactor: eigvals_sorted = eigvals_sorted || eigvals;
|
|
|
- // refactor: eigvals_sorted.Sort(1);
|
|
|
|
|
|
VVector spectral_features;
|
|
|
|
|
|
for ( int i = 0 ; i < (int)eigvect.rows() ; i++ )
|
|
|
{
|
|
|
- // refactor-nice.pl: check this substitution
|
|
|
- // old: Vector eigvec_k ( noClasses );
|
|
|
NICE::Vector eigvec_k ( noClasses );
|
|
|
map<double, int>::const_iterator k = eigvals_sorted.begin();
|
|
|
for ( int j = 0 ; j < noClasses ; j++ )
|
|
|
{
|
|
|
int eigval_index = k->second;
|
|
|
- // refactor-nice.pl: check this substitution
|
|
|
- // old: eigvec_k[j] = eigvect[i][ (int)eigvals_sorted[j][0] ] ;
|
|
|
eigvec_k[j] = eigvect(i, eigval_index ) ;
|
|
|
k++;
|
|
|
}
|
|
@@ -221,11 +181,7 @@ void SpectralCluster::cluster ( const VVector & features,
|
|
|
{
|
|
|
int k = assignment[j];
|
|
|
|
|
|
- // refactor-nice.pl: check this substitution
|
|
|
- // old: Vector & p = prototypes[k];
|
|
|
NICE::Vector & p = prototypes[k];
|
|
|
- // refactor-nice.pl: check this substitution
|
|
|
- // old: const Vector & x = *i;
|
|
|
const NICE::Vector & x = *i;
|
|
|
p += x;
|
|
|
weights[k]++;
|
|
@@ -233,8 +189,6 @@ void SpectralCluster::cluster ( const VVector & features,
|
|
|
|
|
|
for ( int k = 0 ; k < noClasses ; k++ )
|
|
|
{
|
|
|
- // refactor-nice.pl: check this substitution
|
|
|
- // old: Vector & p = prototypes[k];
|
|
|
NICE::Vector & p = prototypes[k];
|
|
|
if ( weights[k] <= 0 ) {
|
|
|
fprintf (stderr, "FATAL ERROR: spectral clustering produced empty cluster !\n");
|