| 1 |
/** @file ikmeans_elkan.tc |
| 2 |
** @brief Integer K-Means - Elkan Algorithm - Definition |
| 3 |
** @author Andrea Vedaldi |
| 4 |
**/ |
| 5 |
|
| 6 |
/* |
| 7 |
Copyright (C) 2007-12 Andrea Vedaldi and Brian Fulkerson. |
| 8 |
All rights reserved. |
| 9 |
|
| 10 |
This file is part of the VLFeat library and is made available under |
| 11 |
the terms of the BSD license (see the COPYING file). |
| 12 |
*/ |
| 13 |
|
| 14 |
#include "mathop.h" |
| 15 |
|
| 16 |
/** @internal |
| 17 |
** Update inter cluster distance table. |
| 18 |
**/ |
| 19 |
|
| 20 |
static void |
| 21 |
vl_ikm_elkan_update_inter_dist (VlIKMFilt *f) |
| 22 |
{ |
| 23 |
vl_uindex i, k, kp ; |
| 24 |
|
| 25 |
/* inter cluster distances */ |
| 26 |
for(k = 0 ; k < f->K ; ++ k) { |
| 27 |
for(kp = 0 ; kp < f->K ; ++ kp) { |
| 28 |
vl_ikmacc_t dist = 0 ; |
| 29 |
if (k != kp) { |
| 30 |
for(i = 0 ; i < f->M ; ++i) { |
| 31 |
vl_ikmacc_t delta = f->centers [kp * f->M + i] - f->centers [k * f->M + i] ; |
| 32 |
dist += delta * delta ; |
| 33 |
} |
| 34 |
} |
| 35 |
f->inter_dist [k * f->K + kp] = f->inter_dist [kp * f->K + k] = dist >> 2 ; |
| 36 |
} |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
/** @internal |
| 41 |
** @brief Helper function to initialize filter for Triangle algorithm |
| 42 |
** @param f filter. |
| 43 |
**/ |
| 44 |
|
| 45 |
static void |
| 46 |
vl_ikm_init_elkan (VlIKMFilt *f) |
| 47 |
{ |
| 48 |
if (f->inter_dist) { |
| 49 |
vl_free (f-> inter_dist) ; |
| 50 |
} |
| 51 |
f->inter_dist = vl_malloc (sizeof(*f->inter_dist) * f->K * f->K) ; |
| 52 |
vl_ikm_elkan_update_inter_dist (f) ; |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
/** @internal |
| 57 |
** @brief Elkan algorithm |
| 58 |
** @param f IKM quantizer. |
| 59 |
** @param data Data to quantize. |
| 60 |
** @param N Number of data elements. |
| 61 |
**/ |
| 62 |
|
| 63 |
static int |
| 64 |
vl_ikm_train_elkan (VlIKMFilt* f, vl_uint8 const* data, vl_size N) |
| 65 |
{ |
| 66 |
/* REMARK !! All distances are squared !! */ |
| 67 |
vl_uindex i,pass,c,cp,x,cx ; |
| 68 |
vl_size dist_calc = 0 ; |
| 69 |
|
| 70 |
vl_ikmacc_t dist ; |
| 71 |
vl_ikmacc_t *m_pt = vl_malloc(sizeof(*m_pt) * f->M * f->K) ; /* new centers (temp) */ |
| 72 |
vl_ikmacc_t *u_pt = vl_malloc(sizeof(*u_pt) * N) ; /* upper bound (may str) */ |
| 73 |
char *r_pt = vl_malloc(sizeof(*r_pt) * 1 * N) ; /* flag: u is strict */ |
| 74 |
vl_ikmacc_t *s_pt = vl_malloc(sizeof(*s_pt) * f->K) ; /* min cluster dist. */ |
| 75 |
vl_ikmacc_t *l_pt = vl_malloc(sizeof(*l_pt) * N * f->K) ; /* lower bound */ |
| 76 |
vl_ikmacc_t *d_pt = f->inter_dist ; /* half inter clst dist */ |
| 77 |
vl_uint32 *asgn = vl_malloc (sizeof(*asgn) * N) ; |
| 78 |
vl_uint32 *counts =vl_malloc (sizeof(*counts) * N) ; |
| 79 |
|
| 80 |
int done = 0 ; |
| 81 |
|
| 82 |
/* do passes */ |
| 83 |
vl_ikm_elkan_update_inter_dist (f) ; |
| 84 |
|
| 85 |
/* init */ |
| 86 |
memset(l_pt, 0, sizeof(*l_pt) * N * f->K) ; |
| 87 |
memset(u_pt, 0, sizeof(*u_pt) * N) ; |
| 88 |
memset(r_pt, 0, sizeof(*r_pt) * N) ; |
| 89 |
for(x = 0 ; x < N ; ++x) { |
| 90 |
vl_ikmacc_t best_dist ; |
| 91 |
|
| 92 |
/* do first cluster `by hand' */ |
| 93 |
dist_calc ++ ; |
| 94 |
for(dist = 0, i = 0 ; i < f->M ; ++i) { |
| 95 |
vl_ikmacc_t delta = (vl_ikmacc_t)data[x * f->M + i] - f->centers[i] ; |
| 96 |
dist += delta*delta ; |
| 97 |
} |
| 98 |
cx = 0 ; |
| 99 |
best_dist = dist ; |
| 100 |
l_pt[x] = dist ; |
| 101 |
|
| 102 |
/* do other clusters */ |
| 103 |
for(c = 1 ; c < f->K ; ++c) { |
| 104 |
if(d_pt[f->K * cx + c] < best_dist) { |
| 105 |
/* might need to be updated */ |
| 106 |
|
| 107 |
dist_calc++ ; |
| 108 |
for(dist=0, i = 0 ; i < f->M ; ++i) { |
| 109 |
vl_ikmacc_t delta = |
| 110 |
(vl_ikmacc_t)data[x * f->M + i] |
| 111 |
- f->centers[c * f->M + i] ; |
| 112 |
dist += delta * delta ; |
| 113 |
} |
| 114 |
|
| 115 |
/* lower bound */ |
| 116 |
l_pt[N*c + x] = dist ; |
| 117 |
|
| 118 |
if(dist < best_dist) { |
| 119 |
best_dist = dist ; |
| 120 |
cx = c ; |
| 121 |
} |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
asgn[x] = (vl_uint32)cx ; |
| 126 |
u_pt[x] = best_dist ; |
| 127 |
} |
| 128 |
|
| 129 |
/* -------------------------------------------------------------------- |
| 130 |
* Passes |
| 131 |
* ------------------------------------------------------------------ */ |
| 132 |
|
| 133 |
for (pass = 0 ; 1 ; ++ pass) { |
| 134 |
|
| 135 |
/* ------------------------------------------------------------------ |
| 136 |
* Re-calculate means |
| 137 |
* ---------------------------------------------------------------- */ |
| 138 |
memset(m_pt, 0, sizeof(*m_pt) * f->M * f->K) ; |
| 139 |
memset(counts, 0, sizeof(*counts) * f->K) ; |
| 140 |
|
| 141 |
/* accumulate */ |
| 142 |
for(x = 0 ; x < N ; ++x) { |
| 143 |
int cx = asgn[x] ; |
| 144 |
++ counts[ cx ] ; |
| 145 |
for(i = 0 ; i < f->M ; ++i) { |
| 146 |
m_pt[cx * f->M + i] += data[x * f->M + i] ; |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
/* normalize */ |
| 151 |
for(c = 0 ; c < f->K ; ++c) { |
| 152 |
vl_ikmacc_t n = counts[c] ; |
| 153 |
if(n > 0) { |
| 154 |
for(i = 0 ; i < f->M ; ++i) { |
| 155 |
m_pt[c * f->M + i] /= n ; |
| 156 |
} |
| 157 |
} else { |
| 158 |
for(i = 0 ; i < f->M ; ++i) { |
| 159 |
/*m_pt[c*M + i] = data[pairs_pt[c].j*M + i] ;*/ |
| 160 |
} |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
/* ------------------------------------------------------------------ |
| 165 |
* Update bounds |
| 166 |
* --------------------------------------------------------------- */ |
| 167 |
for(c = 0 ; c < f->K ; ++c) { |
| 168 |
|
| 169 |
/* distance d(m(c),c) and update c */ |
| 170 |
dist_calc++ ; |
| 171 |
for(dist = 0, i = 0 ; i < f->M ; ++i) { |
| 172 |
vl_ikmacc_t delta = |
| 173 |
(vl_ikmacc_t)m_pt[c * f->M + i] |
| 174 |
- f->centers[c * f->M + i] ; |
| 175 |
f->centers[c * f->M + i] = m_pt[c * f->M +i] ; |
| 176 |
dist += delta * delta ; |
| 177 |
} |
| 178 |
for(x = 0 ; x < N ; ++x) { |
| 179 |
vl_ikmacc_t lxc = l_pt[c * N + x] ; |
| 180 |
vl_uindex cx = (int) asgn[x] ; |
| 181 |
|
| 182 |
/* lower bound */ |
| 183 |
if(dist < lxc) { |
| 184 |
lxc = (vl_ikmacc_t) (lxc + dist - 2*(vl_fast_sqrt_ui64(lxc)+1)*(vl_fast_sqrt_ui64(dist)+1)) ; |
| 185 |
} else { |
| 186 |
lxc = 0 ; |
| 187 |
} |
| 188 |
l_pt[c*N + x] = lxc ; |
| 189 |
|
| 190 |
/* upper bound */ |
| 191 |
if(c == cx) { |
| 192 |
vl_ikmacc_t ux = u_pt[x] ; |
| 193 |
u_pt[x] = (vl_ikmacc_t) (ux + dist + 2 * (vl_fast_sqrt_ui64(ux)+1)*(vl_fast_sqrt_ui64(dist)+1)) ; |
| 194 |
r_pt[x] = 1 ; |
| 195 |
} |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
/* inter cluster distances */ |
| 200 |
for(c = 0 ; c < f->K ; ++c) { |
| 201 |
for(cp = 0 ; cp < f->K ; ++cp) { |
| 202 |
dist = 0 ; |
| 203 |
if( c != cp ) { |
| 204 |
dist_calc++; |
| 205 |
for(i = 0 ; i < f->M ; ++i) { |
| 206 |
vl_ikmacc_t delta = f->centers[cp * f->M + i] - f->centers[ c * f->M + i] ; |
| 207 |
dist += delta*delta ; |
| 208 |
} |
| 209 |
} |
| 210 |
d_pt[c * f->K + cp] = d_pt[cp * f->K + c] = dist>>2 ; |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
/* closest cluster distance */ |
| 215 |
for(c = 0 ; c < f->K ; ++c) { |
| 216 |
vl_ikmacc_t best_dist = VL_IKMACC_MAX ; |
| 217 |
for(cp = 0 ; cp < f->K ; ++cp) { |
| 218 |
dist = d_pt[c * f->K + cp] ; |
| 219 |
if(c != cp && dist < best_dist) best_dist = dist ; |
| 220 |
} |
| 221 |
s_pt[c] = best_dist >> 2 ; |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
/* ------------------------------------------------------------------ |
| 226 |
* Assign data to centers |
| 227 |
* ---------------------------------------------------------------- */ |
| 228 |
done = 1 ; |
| 229 |
for(x = 0 ; x < N ; ++x) { |
| 230 |
vl_uindex cx = (vl_uindex) asgn[x] ; |
| 231 |
vl_ikmacc_t ux = u_pt[x] ; |
| 232 |
|
| 233 |
/* ux is an upper bound of the distance of x to its |
| 234 |
current center cx. s_pt[cx] is half of the minum distance |
| 235 |
between the cluster cx and any other cluster center. If |
| 236 |
ux <= s_pt[cx] then x remains attached to cx. */ |
| 237 |
|
| 238 |
if(ux <= s_pt[cx]) continue ; |
| 239 |
|
| 240 |
for(c = 0 ; c < f->K ; ++c) { |
| 241 |
vl_ikmacc_t dist = 0 ; |
| 242 |
/* so x might need to be re-associated from cx to c. We can |
| 243 |
exclude c if |
| 244 |
|
| 245 |
1 - cx = c (trivial) or |
| 246 |
2 - u(x) <= l(x,c) as this implies d(x,cx) <= d(x,c) or |
| 247 |
3 - u(x) <= d(cx,c)/2 as this implies d(x,cx) <= d(x,c). |
| 248 |
*/ |
| 249 |
if(c == cx || |
| 250 |
ux <= l_pt[N * c + x] || |
| 251 |
ux <= d_pt[f->K * c + cx]) |
| 252 |
continue ; |
| 253 |
|
| 254 |
/* we need to make a true comparison */ |
| 255 |
|
| 256 |
/* if u_pt[x] is stale (i.e. not strictly equal to |
| 257 |
d(x,cx)), then re-calcualte it. */ |
| 258 |
if( r_pt[x] ) { |
| 259 |
dist_calc++; |
| 260 |
for(dist = 0, i = 0 ; i < f->M ; ++i) { |
| 261 |
vl_ikmacc_t delta = (vl_ikmacc_t)data[x * f->M + i] - f->centers[cx * f->M + i] ; |
| 262 |
dist += delta*delta ; |
| 263 |
} |
| 264 |
ux = u_pt[x] = dist ; |
| 265 |
r_pt[x] = 0 ; |
| 266 |
|
| 267 |
/* now that u_pt[x] is updated, we check the conditions |
| 268 |
again */ |
| 269 |
if( |
| 270 |
ux <= l_pt[N * c + x] || |
| 271 |
ux <= d_pt[f->K * c + cx] ) |
| 272 |
continue ; |
| 273 |
} |
| 274 |
|
| 275 |
/* no way... we need to compute the distance d(x,c) */ |
| 276 |
dist_calc++ ; |
| 277 |
for(dist = 0, i = 0 ; i < f->M ; ++i) { |
| 278 |
vl_ikmacc_t delta = (vl_ikmacc_t)data[ x * f->M + i] - f->centers[c * f->M + i] ; |
| 279 |
dist += delta * delta ; |
| 280 |
} |
| 281 |
|
| 282 |
l_pt[N * c + x] = dist ; |
| 283 |
|
| 284 |
if (dist < ux) { |
| 285 |
ux = u_pt[x] = dist ; |
| 286 |
/* r_pt[x] already 0 */ |
| 287 |
asgn[x] = (vl_uint32)c ; |
| 288 |
done = 0 ; |
| 289 |
} |
| 290 |
} |
| 291 |
} /* next data point */ |
| 292 |
|
| 293 |
/* stopping condition */ |
| 294 |
if(done || pass == f->max_niters) { |
| 295 |
break ; |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
vl_free (counts) ; |
| 300 |
vl_free (asgn) ; |
| 301 |
vl_free (l_pt) ; |
| 302 |
vl_free (s_pt) ; |
| 303 |
vl_free (r_pt) ; |
| 304 |
vl_free (u_pt) ; |
| 305 |
vl_free (m_pt) ; |
| 306 |
|
| 307 |
if (f-> verb) { |
| 308 |
VL_PRINTF ("ikm: Elkan algorithm: total iterations: %d\n", pass) ; |
| 309 |
VL_PRINTF ("ikm: Elkan algorithm: distance calculations: %d (speedup: %.2f)\n", |
| 310 |
dist_calc, (float)N * f->K * (pass+2) / dist_calc - 1) ; |
| 311 |
} |
| 312 |
return 0 ; |
| 313 |
} |
| 314 |
|
| 315 |
/** @internal |
| 316 |
** @brief Elkan algorithm |
| 317 |
** @param f IKM quantizer. |
| 318 |
** @param asgn Assignment of data to centers (out). |
| 319 |
** @param data Data to quantize. |
| 320 |
** @param N Number of data elements. |
| 321 |
**/ |
| 322 |
|
| 323 |
static void |
| 324 |
vl_ikm_push_elkan (VlIKMFilt *f, vl_uint32 *asgn, vl_uint8 const *data, vl_size N) |
| 325 |
{ |
| 326 |
vl_uindex i,c,cx,x ; |
| 327 |
vl_size dist_calc = 0 ; |
| 328 |
vl_ikmacc_t dist, best_dist ; |
| 329 |
vl_ikmacc_t *d_pt = f->inter_dist ; |
| 330 |
|
| 331 |
/* assign data to centers */ |
| 332 |
for(x = 0 ; x < N ; ++x) { |
| 333 |
best_dist = VL_IKMACC_MAX ; |
| 334 |
cx = 0 ; |
| 335 |
|
| 336 |
for(c = 0 ; c < f->K ; ++c) { |
| 337 |
if(d_pt[f->K * cx + c] < best_dist) { |
| 338 |
/* might need to be updated */ |
| 339 |
dist_calc ++ ; |
| 340 |
for(dist=0, i = 0 ; i < f->M ; ++i) { |
| 341 |
vl_ikmacc_t delta = data[x * f->M + i] - f->centers[c * f->M + i] ; |
| 342 |
dist += delta * delta ; |
| 343 |
} |
| 344 |
|
| 345 |
/* u_pt is strict at the beginning */ |
| 346 |
if(dist < best_dist) { |
| 347 |
best_dist = dist ; |
| 348 |
cx = c ; |
| 349 |
} |
| 350 |
} |
| 351 |
} |
| 352 |
asgn[x] = (vl_uint32)cx ; |
| 353 |
} |
| 354 |
} |
| 355 |
|
| 356 |
/* |
| 357 |
* Local Variables: * |
| 358 |
* mode: C * |
| 359 |
* End: * |
| 360 |
*/ |