#include <iostream>
#include <stdio.h>
#include <cstdlib>
{
return sum(
abs(data2 - means2), 2);
}
{
array dists = distance(data, means);
return idx;
}
{
means(
span, ii,
span) =
sum(data * (clustersd == ii)) / (
sum(clusters == ii) + 1e-5);
}
return means;
}
void kmeans(
array &means,
array &clusters,
const array in,
int k,
int iter=100)
{
}
for (int i = 0; i < iter; i++) {
prev_clusters = curr_clusters;
curr_clusters = clusterize(data, means);
unsigned num_changed = count<unsigned>(prev_clusters != curr_clusters);
if (num_changed < (n/1000) + 1) break;
means = new_means(data, curr_clusters, k);
}
}
clusters = prev_clusters;
}
int kmeans_demo(int k, bool console)
{
printf("** ArrayFire K-Means Demo (k = %d) **\n\n", k);
array img =
loadImage(ASSETS_DIR
"/examples/images/vegetable-woman.jpg",
true) / 255;
array means_full, clusters_full;
kmeans(means_full, clusters_full, vec, k);
array means_half, clusters_half;
kmeans(means_half, clusters_half, vec, k / 2);
array means_dbl, clusters_dbl;
kmeans(means_dbl, clusters_dbl, vec, k * 2);
if (!console) {
#if 0
char str_full[32], str_half[32], str_dbl[32];
sprintf(str_full, "%2d clusters", k);
sprintf(str_half, "%2d clusters", k/2);
sprintf(str_dbl , "%2d clusters", k*2);
fig("color","default");
fig("sub",2,2,1); image(img); fig("title","input");
fig("sub",2,2,2); image(out_full); fig("title", str_full);
fig("sub",2,2,3); image(out_half); fig("title", str_half);
fig("sub",2,2,4); image(out_dbl ); fig("title", str_dbl );
printf("Hit enter to finish\n");
getchar();
#else
printf("Graphics not implemented yet\n");
#endif
} else {
means_half =
moddims(means_half, means_half.dims(1), means_half.dims(2));
means_dbl =
moddims(means_dbl , means_dbl.dims(1) , means_dbl.dims(2) );
}
return 0;
}
int main(int argc, char** argv)
{
int device = argc > 1 ? atoi(argv[1]) : 0;
bool console = argc > 2 ? argv[2][0] == '-' : false;
int k = argc > 3 ? atoi(argv[3]) : 16;
try {
return kmeans_demo(k, console);
std::cerr << ae.
what() << std::endl;
}
return 0;
}