File: kmeans.cpp

package info (click to toggle)
arrayfire 3.3.2%2Bdfsg1-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 109,016 kB
  • sloc: cpp: 127,909; lisp: 6,878; python: 3,923; ansic: 1,051; sh: 347; makefile: 338; xml: 175
file content (180 lines) | stat: -rw-r--r-- 5,222 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*******************************************************
 * Copyright (c) 2014, ArrayFire
 * All rights reserved.
 *
 * This file is distributed under 3-clause BSD license.
 * The complete license agreement can be obtained at:
 * http://arrayfire.com/licenses/BSD-3-Clause
 ********************************************************/

#include <iostream>
#include <stdio.h>
#include <arrayfire.h>
#include <af/util.h>
#include <cstdlib>

using namespace af;

array distance(array data, array means)
{
    int n = data.dims(0); // Number of features
    int k = means.dims(1); // Number of means

    array data2  = tile(data , 1, k, 1);
    array means2 = tile(means, n, 1, 1);

    // Currently using manhattan distance
    // Can be replaced with other distance measures
    return sum(abs(data2 - means2), 2);
}

// Get cluster id of each location in data
array clusterize(const array data, const array means)
{
    // Get manhattan distance
    array dists = distance(data, means);

    // get the locations of minimum distance
    array idx, val;
    min(val, idx, dists, 1);

    // Return cluster IDs
    return idx;
}

array new_means(array data, array clusters, int k)
{
    int d = data.dims(2);
    array means = constant(0, 1, k, d);
    array clustersd = tile(clusters, 1, 1, d);

    gfor (seq ii, k) {
        means(span, ii, span) = sum(data * (clustersd == ii)) / (sum(clusters == ii) + 1e-5);
    }

    return means;
}

// kmeans(means, clusters, data, k)
// data:  input,  1D or 2D (range > [0-1])
// k:     input,  # desired means (k > 1)
// means: output, vector of means
void kmeans(array &means, array &clusters, const array in, int k, int iter=100)
{
    unsigned n = in.dims(0); // Num features
    unsigned d = in.dims(2); // feature length

    // reshape input
    array data = in * 0;

    // re-center and scale down data to [0, 1]
    array minimum = min(in);
    array maximum = max(in);

    gfor(seq ii, d) {
        data(span, span, ii) = (in(span, span, ii) - minimum(ii).scalar<float>()) / maximum(ii).scalar<float>();
    }

    // Initial guess of means
    means = randu(1, k, d);
    array curr_clusters = constant(0, data.dims(0)) - 1;
    array prev_clusters;

    // Stop updating after specified number of iterations
    for (int i = 0; i < iter; i++) {
        // Store previous cluster ids
        prev_clusters = curr_clusters;

        // Get cluster ids for current means
        curr_clusters = clusterize(data, means);

        // Break early if clusters not changing
        unsigned num_changed = count<unsigned>(prev_clusters != curr_clusters);

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

        // Update current means for new clusters
        means = new_means(data, curr_clusters, k);
    }

    // Scale up means
    gfor(seq ii, d) {
        means(span, span, ii) = maximum(ii) * means(span, span, ii) + minimum(ii);
    }

    clusters = prev_clusters;

}

// K-Means image recoloring.
// Shifts the hues of an image to the k mean hues.
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; // [0-255]

    int w = img.dims(0), h = img.dims(1), c = img.dims(2);
    array vec = moddims(img, w * h, 1, c);

    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
        array out_full = moddims(means_full(span, clusters_full, span), img.dims());
        array out_half = moddims(means_half(span, clusters_half, span), img.dims());
        array out_dbl  = moddims(means_dbl (span, clusters_dbl , span), img.dims());

        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_full = moddims(means_full, means_full.dims(1), means_full.dims(2));
        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) );

        af_print(means_full);
        af_print(means_half);
        af_print(means_dbl );
    }

    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 {

        af::setDevice(device);
        af::info();
        return kmeans_demo(k, console);

    } catch (af::exception &ae) {
        std::cerr << ae.what() << std::endl;
    }

    return 0;
}