#include <stdio.h>
#include <cstdlib>
#include <iostream>
#include <string>
return sum(
abs(data2 - means2), 2);
}
array dists = distance(data, means);
return idx;
}
sum(data * (clustersd == ii)) / (
sum(clusters == ii) + 1e-5);
}
return means;
}
int iter = 100) {
}
for (int i = 0; i < iter; i++) {
prev_clusters = curr_clusters;
curr_clusters = clusterize(data, means);
if (num_changed < (n / 1000) + 1) break;
means = new_means(data, curr_clusters, k);
}
maximum(ii) * means(
span,
span, ii) + minimum(ii);
}
clusters = prev_clusters;
}
int kmeans_demo(int k, bool console) {
printf("** ArrayFire K-Means Demo (k = %d) **\n\n", k);
loadImage(ASSETS_DIR
"/examples/images/spider.jpg") / 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) {
af::Window wnd(800, 800,
"ArrayFire K-Means Demo");
wnd.grid(2, 2);
std::stringstream out_full_caption, out_half_caption, out_dbl_caption;
out_full_caption << "k = " << k;
out_half_caption << "k = " << k / 2;
out_dbl_caption << "k = " << k * 2;
while (!wnd.close()) {
wnd(0, 0).image(img, "Input Image");
wnd(0, 1).image(out_full, out_full_caption.str().c_str());
wnd(1, 0).image(out_half, out_half_caption.str().c_str());
wnd(1, 1).image(out_dbl, out_dbl_caption.str().c_str());
wnd.show();
}
} else {
means_full =
means_half =
}
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]) : 8;
try {
return kmeans_demo(k, console);
return 0;
}
Window object to render af::arrays.
A multi dimensional data container.
T scalar() const
Get the first element of the array as a scalar.
dim4 dims() const
Get dimensions of the array.
An ArrayFire exception class.
virtual const char * what() const
Returns an error message for the exception in a string format.
seq is used to create sequences for indexing af::array
array abs(const array &in)
C++ Interface to calculate the absolute value.
array constant(T val, const dim4 &dims, const dtype ty=(af_dtype) dtype_traits< T >::ctype)
C++ Interface to generate an array with elements set to a specified value.
void setDevice(const int device)
Sets the current device.
array loadImage(const char *filename, const bool is_color=false)
C++ Interface for loading an image.
array moddims(const array &in, const dim4 &dims)
C++ Interface to modify the dimensions of an input array to a specified shape.
array tile(const array &in, const unsigned x, const unsigned y=1, const unsigned z=1, const unsigned w=1)
C++ Interface to generate a tiled array.
array randu(const dim4 &dims, const dtype ty, randomEngine &r)
C++ Interface to create an array of random numbers uniformly distributed.
array count(const array &in, const int dim=-1)
C++ Interface to count non-zero values in an array along a given dimension.
array max(const array &in, const int dim=-1)
C++ Interface to return the maximum along a given dimension.
array min(const array &in, const int dim=-1)
C++ Interface to return the minimum along a given dimension.
array sum(const array &in, const int dim=-1)
C++ Interface to sum array elements over a given dimension.
seq span
A special value representing the entire axis of an af::array.