| 1 |
/** @file ikmeans_lloyd.tc |
| 2 |
** @brief Integer K-Means - LLoyd Algorithm - 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 |
/** @internal |
| 16 |
** @brief Helper function to initialize a filter for Lloyd algorithm |
| 17 |
** |
| 18 |
** @param f filter. |
| 19 |
**/ |
| 20 |
|
| 21 |
static void |
| 22 |
vl_ikm_init_lloyd (VlIKMFilt * f VL_UNUSED) |
| 23 |
{ } |
| 24 |
|
| 25 |
/** @internal |
| 26 |
** @brief LLoyd algorithm |
| 27 |
** @param f IKM quantizer. |
| 28 |
** @param data Training data. |
| 29 |
** @param N Number of traning data. |
| 30 |
** @return error code. |
| 31 |
**/ |
| 32 |
|
| 33 |
static int |
| 34 |
vl_ikm_train_lloyd (VlIKMFilt* f, vl_uint8 const* data, vl_size N) |
| 35 |
{ |
| 36 |
int err = 0 ; |
| 37 |
vl_uindex iter, i, j, k ; |
| 38 |
vl_uint32 *asgn = vl_malloc (sizeof(vl_uint32) * N) ; |
| 39 |
vl_uint32 *counts = vl_malloc (sizeof(vl_uint32) * N) ; |
| 40 |
|
| 41 |
for (iter = 0 ; 1 ; ++ iter) { |
| 42 |
vl_bool done = 1 ; |
| 43 |
|
| 44 |
/* --------------------------------------------------------------- |
| 45 |
* Calc. assignments |
| 46 |
* ------------------------------------------------------------ */ |
| 47 |
|
| 48 |
for (j = 0 ; j < N ; ++j) { |
| 49 |
vl_ikmacc_t best_dist = 0 ; |
| 50 |
vl_index best = -1 ; |
| 51 |
|
| 52 |
for (k = 0; k < f->K ; ++k) { |
| 53 |
vl_ikmacc_t dist = 0 ; |
| 54 |
|
| 55 |
/* compute distance with this center */ |
| 56 |
for (i = 0; i < f->M ; ++i) { |
| 57 |
vl_ikmacc_t delta = |
| 58 |
data [j * f->M + i] - f->centers [k * f->M + i] ; |
| 59 |
dist += delta * delta ; |
| 60 |
} |
| 61 |
|
| 62 |
/* compare with current best */ |
| 63 |
if (best == -1 || dist < best_dist) { |
| 64 |
best = k ; |
| 65 |
best_dist = dist ; |
| 66 |
} |
| 67 |
} |
| 68 |
if (asgn [j] != best) { |
| 69 |
asgn [j] = (vl_uint32) best ; |
| 70 |
done = 0 ; |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
/* stopping condition */ |
| 75 |
if (done || iter == f->max_niters) break ; |
| 76 |
|
| 77 |
/* --------------------------------------------------------------- |
| 78 |
* Calc. centers |
| 79 |
* ------------------------------------------------------------ */ |
| 80 |
|
| 81 |
/* re-compute centers */ |
| 82 |
memset (f->centers, 0, sizeof(*f->centers) * f->M * f->K); |
| 83 |
memset (counts, 0, sizeof(*counts) * f->K); |
| 84 |
for (j = 0; j < N; ++j) { |
| 85 |
vl_uindex this_center = asgn [j] ; |
| 86 |
++ counts [this_center] ; |
| 87 |
for (i = 0; i < f->M ; ++i) { |
| 88 |
f->centers [this_center * f->M + i] += data[j * f->M + i] ; |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
for (k = 0; k < f->K; ++k) { |
| 93 |
vl_index n = counts [k]; |
| 94 |
if (n > 0xffffff) { |
| 95 |
err = 1 ; |
| 96 |
} |
| 97 |
if (n > 0) { |
| 98 |
for (i = 0; i < f->M; ++i) { |
| 99 |
f->centers [k * f->M + i] /= n; |
| 100 |
} |
| 101 |
} else { |
| 102 |
/* |
| 103 |
If no data are assigned to the center, it is not changed |
| 104 |
with respect to the previous iteration, so we do not do anything. |
| 105 |
*/ |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
vl_free (counts) ; |
| 111 |
vl_free (asgn) ; |
| 112 |
return err ; |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
/** @internal |
| 117 |
** @brief LLoyd algorithm |
| 118 |
** @param f IKM quantizer. |
| 119 |
** @param asgn Assignments (out). |
| 120 |
** @param data Data to quantize. |
| 121 |
** @param N Number of data. |
| 122 |
**/ |
| 123 |
|
| 124 |
static void |
| 125 |
vl_ikm_push_lloyd (VlIKMFilt *f, vl_uint32 *asgn, vl_uint8 const *data, vl_size N) |
| 126 |
{ |
| 127 |
vl_uindex j ; |
| 128 |
for(j = 0 ; j < N ; ++j) { |
| 129 |
asgn[j] = vl_ikm_push_one (f->centers, data + j * f->M, f->M, f->K); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
/* |
| 134 |
* Local Variables: * |
| 135 |
* mode: C * |
| 136 |
* End: * |
| 137 |
*/ |