| 1 |
/** @file ikmeans.c |
| 2 |
** @brief Integer K-Means clustering - Definition |
| 3 |
** @author Brian Fulkerson |
| 4 |
** @author Andrea Vedaldi |
| 5 |
**/ |
| 6 |
|
| 7 |
/* |
| 8 |
Copyright (C) 2007-12 Andrea Vedaldi and Brian Fulkerson. |
| 9 |
All rights reserved. |
| 10 |
|
| 11 |
This file is part of the VLFeat library and is made available under |
| 12 |
the terms of the BSD license (see the COPYING file). |
| 13 |
*/ |
| 14 |
|
| 15 |
/** @file ikmeans.h |
| 16 |
** |
| 17 |
** Integer K-means (IKM) is an implementation of K-means clustering |
| 18 |
** (or Vector Quantization, VQ) for integer data. This is |
| 19 |
** particularly useful for clustering large collections of visual |
| 20 |
** descriptors. |
| 21 |
** |
| 22 |
** Use the function ::vl_ikm_new() to create a IKM |
| 23 |
** quantizer. Initialize the IKM quantizer with @c K clusters by |
| 24 |
** ::vl_ikm_init() or similar function. Use ::vl_ikm_train() to train |
| 25 |
** the quantizer. Use ::vl_ikm_push() or ::vl_ikm_push_one() to |
| 26 |
** quantize new data. |
| 27 |
** |
| 28 |
** Given data @f$x_1,\dots,x_N\in R^d@f$ and a number of clusters |
| 29 |
** @f$K@f$, the goal is to find assignments @f$a_i\in\{1,\dots,K\},@f$ |
| 30 |
** and centers @f$c_1,\dots,c_K\in R^d@f$ so that the <em>expected |
| 31 |
** distortion</em> |
| 32 |
** |
| 33 |
** @f[ |
| 34 |
** E(\{a_{i}, c_j\}) = \frac{1}{N} \sum_{i=1}^N d(x_i, c_{a_i}) |
| 35 |
** @f] |
| 36 |
** |
| 37 |
** is minimized. Here @f$d(x_i, c_{a_i})@f$ is the |
| 38 |
** <em>distortion</em>, i.e. the cost we pay for representing @f$ x_i |
| 39 |
** @f$ by @f$ c_{a_i} @f$. IKM uses the squared distortion |
| 40 |
** @f$d(x,y)=\|x-y\|^2_2@f$. |
| 41 |
** |
| 42 |
** @section ikmeans-algo Algorithms |
| 43 |
** |
| 44 |
** @subsection ikmeans-alg-init Initialization |
| 45 |
** |
| 46 |
** Most K-means algorithms are iterative and needs an initialization |
| 47 |
** in the form of an initial choice of the centers |
| 48 |
** @f$c_1,\dots,c_K@f$. We include the following options: |
| 49 |
** |
| 50 |
** - User specified centers (::vl_ikm_init); |
| 51 |
** - Random centers (::vl_ikm_init_rand); |
| 52 |
** - Centers from @c K randomly selected data points (::vl_ikm_init_rand_data). |
| 53 |
** |
| 54 |
** @subsection ikmeans-alg-lloyd Lloyd |
| 55 |
** |
| 56 |
** The Lloyd (also known as Lloyd-Max and LBG) algorithm iteratively: |
| 57 |
** |
| 58 |
** - Fixes the centers, optimizing the assignments (minimizing by |
| 59 |
** exhaustive search the association of each data point to the |
| 60 |
** centers); |
| 61 |
** - Fixes the assignments and optimizes the centers (by descending |
| 62 |
** the distortion error function). For the squared distortion, this |
| 63 |
** step is in closed form. |
| 64 |
** |
| 65 |
** This algorithm is not particularly efficient because all data |
| 66 |
** points need to be compared to all centers, for a complexity |
| 67 |
** @f$O(dNKT)@f$, where <em>T</em> is the total number of iterations. |
| 68 |
** |
| 69 |
** @subsection ikmeans-alg-elkan Elkan |
| 70 |
** |
| 71 |
** The Elkan algorithm is an optimized variant of Lloyd. By making |
| 72 |
** use of the triangle inequality, many comparisons of data points |
| 73 |
** and centers are avoided, especially at later iterations. |
| 74 |
** Usually 4-5 times less comparisons than Lloyd are preformed, |
| 75 |
** providing a dramatic speedup in the execution time. |
| 76 |
** |
| 77 |
**/ |
| 78 |
|
| 79 |
#include "ikmeans.h" |
| 80 |
|
| 81 |
#include <stdlib.h> |
| 82 |
#include <stdio.h> |
| 83 |
#include <string.h> /* memset */ |
| 84 |
#include "assert.h" |
| 85 |
|
| 86 |
static void vl_ikm_init_lloyd (VlIKMFilt*) ; |
| 87 |
static void vl_ikm_init_elkan (VlIKMFilt*) ; |
| 88 |
static int vl_ikm_train_lloyd (VlIKMFilt*, vl_uint8 const*, vl_size) ; |
| 89 |
static int vl_ikm_train_elkan (VlIKMFilt*, vl_uint8 const*, vl_size) ; |
| 90 |
static void vl_ikm_push_lloyd (VlIKMFilt*, vl_uint32*, vl_uint8 const*, vl_size) ; |
| 91 |
static void vl_ikm_push_elkan (VlIKMFilt*, vl_uint32*, vl_uint8 const*, vl_size) ; |
| 92 |
|
| 93 |
/** @brief Create a new IKM quantizer |
| 94 |
** @param method Clustering algorithm. |
| 95 |
** @return new IKM quantizer. |
| 96 |
** |
| 97 |
** The function allocates initializes a new IKM quantizer to |
| 98 |
** operate based algorithm @a method. |
| 99 |
** |
| 100 |
** @a method has values in the enumerations ::VlIKMAlgorithms. |
| 101 |
**/ |
| 102 |
|
| 103 |
VlIKMFilt * |
| 104 |
vl_ikm_new (int method) |
| 105 |
{ |
| 106 |
VlIKMFilt *f = vl_calloc (sizeof(VlIKMFilt), 1) ; |
| 107 |
f -> method = method ; |
| 108 |
f -> max_niters = 200 ; |
| 109 |
return f ; |
| 110 |
} |
| 111 |
|
| 112 |
/** @brief Delete IKM quantizer |
| 113 |
** @param f IKM quantizer. |
| 114 |
**/ |
| 115 |
|
| 116 |
void |
| 117 |
vl_ikm_delete (VlIKMFilt* f) |
| 118 |
{ |
| 119 |
if (f) { |
| 120 |
if (f->centers) vl_free(f->centers) ; |
| 121 |
if (f->inter_dist) vl_free(f->inter_dist) ; |
| 122 |
vl_free(f) ; |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
/** @brief Train clusters |
| 127 |
** @param f IKM quantizer. |
| 128 |
** @param data data. |
| 129 |
** @param N number of data (@a N @c >= 1). |
| 130 |
** @return -1 if an overflow may have occurred. |
| 131 |
**/ |
| 132 |
|
| 133 |
int |
| 134 |
vl_ikm_train (VlIKMFilt *f, vl_uint8 const *data, vl_size N) |
| 135 |
{ |
| 136 |
int err ; |
| 137 |
|
| 138 |
if (f-> verb) { |
| 139 |
VL_PRINTF ("ikm: training with %d data\n", N) ; |
| 140 |
VL_PRINTF ("ikm: %d clusters\n", f -> K) ; |
| 141 |
} |
| 142 |
|
| 143 |
switch (f -> method) { |
| 144 |
case VL_IKM_LLOYD : err = vl_ikm_train_lloyd (f, data, N) ; break ; |
| 145 |
case VL_IKM_ELKAN : err = vl_ikm_train_elkan (f, data, N) ; break ; |
| 146 |
default : |
| 147 |
abort() ; |
| 148 |
} |
| 149 |
return err ; |
| 150 |
} |
| 151 |
|
| 152 |
/** @brief Project data to clusters |
| 153 |
** @param f IKM quantizer. |
| 154 |
** @param asgn Assignments (out). |
| 155 |
** @param data data. |
| 156 |
** @param N number of data (@a N @c >= 1). |
| 157 |
** |
| 158 |
** The function projects the data @a data on the integer K-means |
| 159 |
** clusters specified by the IKM quantizer @a f. Notice that the |
| 160 |
** quantizer must be initialized. |
| 161 |
**/ |
| 162 |
|
| 163 |
void |
| 164 |
vl_ikm_push (VlIKMFilt *f, vl_uint32 *asgn, vl_uint8 const *data, vl_size N) { |
| 165 |
switch (f -> method) { |
| 166 |
case VL_IKM_LLOYD : vl_ikm_push_lloyd (f, asgn, data, N) ; break ; |
| 167 |
case VL_IKM_ELKAN : vl_ikm_push_elkan (f, asgn, data, N) ; break ; |
| 168 |
default : |
| 169 |
abort() ; |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
/** @brief Project one datum to clusters |
| 174 |
** @param centers centers. |
| 175 |
** @param data datum to project. |
| 176 |
** @param K number of centers. |
| 177 |
** @param M dimensionality of the datum. |
| 178 |
** @return the cluster index. |
| 179 |
** |
| 180 |
** The function projects the specified datum @a data on the clusters |
| 181 |
** specified by the centers @a centers. |
| 182 |
**/ |
| 183 |
|
| 184 |
vl_uint32 |
| 185 |
vl_ikm_push_one (vl_ikmacc_t const *centers, |
| 186 |
vl_uint8 const *data, |
| 187 |
vl_size M, vl_size K) |
| 188 |
{ |
| 189 |
vl_uindex i,k ; |
| 190 |
|
| 191 |
/* assign data to centers */ |
| 192 |
vl_uindex best = (vl_uindex) -1 ; |
| 193 |
vl_ikmacc_t best_dist = 0 ; |
| 194 |
|
| 195 |
for(k = 0 ; k < K ; ++k) { |
| 196 |
vl_ikmacc_t dist = 0 ; |
| 197 |
|
| 198 |
/* compute distance with this center */ |
| 199 |
for(i = 0 ; i < M ; ++i) { |
| 200 |
vl_ikmacc_t delta = (vl_ikmacc_t)data[i] - centers[k*M + i] ; |
| 201 |
dist += delta * delta ; |
| 202 |
} |
| 203 |
|
| 204 |
/* compare with current best */ |
| 205 |
if (best == (vl_uindex) -1 || dist < best_dist) { |
| 206 |
best = k ; |
| 207 |
best_dist = dist ; |
| 208 |
} |
| 209 |
} |
| 210 |
return (vl_uint32)best; |
| 211 |
} |
| 212 |
|
| 213 |
/* ---------------------------------------------------------------- */ |
| 214 |
/* Getters and setters */ |
| 215 |
/* ---------------------------------------------------------------- */ |
| 216 |
|
| 217 |
/** @brief Get data dimensionality |
| 218 |
** @param f IKM filter. |
| 219 |
** @return data dimensionality. |
| 220 |
**/ |
| 221 |
|
| 222 |
vl_size |
| 223 |
vl_ikm_get_ndims (VlIKMFilt const* f) |
| 224 |
{ |
| 225 |
return f->M ; |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
/** @brief Get the number of centers K |
| 230 |
** @param f IKM filter. |
| 231 |
** @return number of centers K. |
| 232 |
**/ |
| 233 |
|
| 234 |
vl_size |
| 235 |
vl_ikm_get_K (VlIKMFilt const* f) |
| 236 |
{ |
| 237 |
return f->K ; |
| 238 |
} |
| 239 |
|
| 240 |
/** @brief Get verbosity level |
| 241 |
** @param f IKM filter. |
| 242 |
** @return verbosity level. |
| 243 |
**/ |
| 244 |
|
| 245 |
int |
| 246 |
vl_ikm_get_verbosity (VlIKMFilt const* f) |
| 247 |
{ |
| 248 |
return f->verb ; |
| 249 |
} |
| 250 |
|
| 251 |
/** @brief Get maximum number of iterations |
| 252 |
** @param f IKM filter. |
| 253 |
** @return maximum number of iterations. |
| 254 |
**/ |
| 255 |
|
| 256 |
vl_size |
| 257 |
vl_ikm_get_max_niters (VlIKMFilt const* f) |
| 258 |
{ |
| 259 |
return f->max_niters ; |
| 260 |
} |
| 261 |
|
| 262 |
/** @brief Get maximum number of iterations |
| 263 |
** @param f IKM filter. |
| 264 |
** @return maximum number of iterations. |
| 265 |
**/ |
| 266 |
|
| 267 |
vl_ikmacc_t const * |
| 268 |
vl_ikm_get_centers (VlIKMFilt const* f) |
| 269 |
{ |
| 270 |
return f-> centers ; |
| 271 |
} |
| 272 |
|
| 273 |
/** @brief Set verbosity level |
| 274 |
** @param f IKM filter. |
| 275 |
** @param verb verbosity level. |
| 276 |
**/ |
| 277 |
|
| 278 |
void |
| 279 |
vl_ikm_set_verbosity (VlIKMFilt *f, int verb) |
| 280 |
{ |
| 281 |
f-> verb = VL_MAX(0,verb) ; |
| 282 |
} |
| 283 |
|
| 284 |
/** @brief Set maximum number of iterations |
| 285 |
** @param f IKM filter. |
| 286 |
** @param max_niters maximum number of iterations. |
| 287 |
**/ |
| 288 |
|
| 289 |
void |
| 290 |
vl_ikm_set_max_niters (VlIKMFilt *f, vl_size max_niters) |
| 291 |
{ |
| 292 |
f-> max_niters = max_niters ; |
| 293 |
} |
| 294 |
|
| 295 |
#include "ikmeans_init.tc" |
| 296 |
#include "ikmeans_lloyd.tc" |
| 297 |
#include "ikmeans_elkan.tc" |