ArrayFire: machine_learning/kmeans.cpp (original) (raw)

#include <stdio.h>

#include

#include

#include

using namespace af;

int n = data.dims(0);

int k = means.dims(1);

array data2 = tile(data, 1, k, 1);

array means2 = tile(means, n, 1, 1);

return sum(abs(data2 - means2), 2);

}

array dists = distance(data, means);

min(val, idx, dists, 1);

return idx;

}

int d = data.dims(2);

array clustersd = tile(clusters, 1, 1, d);

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) {

unsigned n = in.dims(0);

unsigned d = in.dims(2);

data(span, span, ii) =

(in(span, span, ii) - minimum(ii).scalar<float>()) /

maximum(ii).scalar<float>();

}

means = randu(1, k, d);

for (int i = 0; i < iter; i++) {

prev_clusters = curr_clusters;

curr_clusters = clusterize(data, means);

unsigned num_changed = count(prev_clusters != curr_clusters);

if (num_changed < (n / 1000) + 1) break;

means = new_means(data, curr_clusters, k);

}

means(span, span, ii) =

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;

int w = img.dims(0), h = img.dims(1), c = img.dims(2);

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) {

moddims(means_full(span, clusters_full, span), img.dims());

moddims(means_half(span, clusters_half, span), img.dims());

moddims(means_dbl(span, clusters_dbl, span), img.dims());

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 =

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]) : 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 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.

AFAPI void setDevice(const int device)

Sets the current device.

AFAPI array loadImage(const char *filename, const bool is_color=false)

C++ Interface for loading an image.

AFAPI array moddims(const array &in, const dim4 &dims)

C++ Interface to modify the dimensions of an input array to a specified shape.

AFAPI 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.

AFAPI array randu(const dim4 &dims, const dtype ty, randomEngine &r)

C++ Interface to create an array of random numbers uniformly distributed.

AFAPI array max(const array &in, const int dim=-1)

C++ Interface to return the maximum along a given dimension.

AFAPI array min(const array &in, const int dim=-1)

C++ Interface to return the minimum along a given dimension.

AFAPI array sum(const array &in, const int dim=-1)

C++ Interface to sum array elements over a given dimension.