/usr/include/vl/kmeans.h is in libvlfeat-dev 0.9.20+dfsg0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 | /** @file kmeans.h
** @brief K-means (@ref kmeans)
** @author Andrea Vedaldi
** @author David Novotny
**/
/*
Copyright (C) 2007-12 Andrea Vedaldi and Brian Fulkerson.
Copyright (C) 2013 Andrea Vedaldi and David Novotny.
All rights reserved.
This file is part of the VLFeat library and is made available under
the terms of the BSD license (see the COPYING file).
*/
#ifndef VL_KMEANS_H
#define VL_KMEANS_H
#include "generic.h"
#include "random.h"
#include "mathop.h"
#include "kdtree.h"
/* ---------------------------------------------------------------- */
/** @brief K-means algorithms */
typedef enum _VlKMeansAlgorithm {
VlKMeansLloyd, /**< Lloyd algorithm */
VlKMeansElkan, /**< Elkan algorithm */
VlKMeansANN /**< Approximate nearest neighbors */
} VlKMeansAlgorithm ;
/** @brief K-means initialization algorithms */
typedef enum _VlKMeansInitialization {
VlKMeansRandomSelection, /**< Randomized selection */
VlKMeansPlusPlus /**< Plus plus raondomized selection */
} VlKMeansInitialization ;
/** ------------------------------------------------------------------
** @brief K-means quantizer
**/
typedef struct _VlKMeans
{
vl_type dataType ; /**< Data type. */
vl_size dimension ; /**< Data dimensionality. */
vl_size numCenters ; /**< Number of centers. */
vl_size numTrees ; /**< Number of trees in forest when using ANN-kmeans. */
vl_size maxNumComparisons ; /**< Maximum number of comparisons when using ANN-kmeans. */
VlKMeansInitialization initialization ; /**< Initalization algorithm. */
VlKMeansAlgorithm algorithm ; /**< Clustring algorithm. */
VlVectorComparisonType distance ; /**< Distance. */
vl_size maxNumIterations ; /**< Maximum number of refinement iterations. */
double minEnergyVariation ; /**< Minimum energy variation. */
vl_size numRepetitions ; /**< Number of clustering repetitions. */
int verbosity ; /**< Verbosity level. */
void * centers ; /**< Centers */
void * centerDistances ; /**< Centers inter-distances. */
double energy ; /**< Current solution energy. */
VlFloatVectorComparisonFunction floatVectorComparisonFn ;
VlDoubleVectorComparisonFunction doubleVectorComparisonFn ;
} VlKMeans ;
/** @name Create and destroy
** @{
**/
VL_EXPORT VlKMeans * vl_kmeans_new (vl_type dataType, VlVectorComparisonType distance) ;
VL_EXPORT VlKMeans * vl_kmeans_new_copy (VlKMeans const * kmeans) ;
VL_EXPORT void vl_kmeans_delete (VlKMeans * self) ;
/** @} */
/** @name Basic data processing
** @{
**/
VL_EXPORT void vl_kmeans_reset (VlKMeans * self) ;
VL_EXPORT double vl_kmeans_cluster (VlKMeans * self,
void const * data,
vl_size dimension,
vl_size numData,
vl_size numCenters) ;
VL_EXPORT void vl_kmeans_quantize (VlKMeans * self,
vl_uint32 * assignments,
void * distances,
void const * data,
vl_size numData) ;
VL_EXPORT void vl_kmeans_quantize_ANN (VlKMeans * self,
vl_uint32 * assignments,
void * distances,
void const * data,
vl_size numData,
vl_size iteration );
/** @} */
/** @name Advanced data processing
** @{
**/
VL_EXPORT void vl_kmeans_set_centers (VlKMeans * self,
void const * centers,
vl_size dimension,
vl_size numCenters) ;
VL_EXPORT void vl_kmeans_init_centers_with_rand_data
(VlKMeans * self,
void const * data,
vl_size dimensions,
vl_size numData,
vl_size numCenters) ;
VL_EXPORT void vl_kmeans_init_centers_plus_plus
(VlKMeans * self,
void const * data,
vl_size dimensions,
vl_size numData,
vl_size numCenters) ;
VL_EXPORT double vl_kmeans_refine_centers (VlKMeans * self,
void const * data,
vl_size numData) ;
/** @} */
/** @name Retrieve data and parameters
** @{
**/
VL_INLINE vl_type vl_kmeans_get_data_type (VlKMeans const * self) ;
VL_INLINE VlVectorComparisonType vl_kmeans_get_distance (VlKMeans const * self) ;
VL_INLINE VlKMeansAlgorithm vl_kmeans_get_algorithm (VlKMeans const * self) ;
VL_INLINE VlKMeansInitialization vl_kmeans_get_initialization (VlKMeans const * self) ;
VL_INLINE vl_size vl_kmeans_get_num_repetitions (VlKMeans const * self) ;
VL_INLINE vl_size vl_kmeans_get_dimension (VlKMeans const * self) ;
VL_INLINE vl_size vl_kmeans_get_num_centers (VlKMeans const * self) ;
VL_INLINE int vl_kmeans_get_verbosity (VlKMeans const * self) ;
VL_INLINE vl_size vl_kmeans_get_max_num_iterations (VlKMeans const * self) ;
VL_INLINE double vl_kmeans_get_min_energy_variation (VlKMeans const * self) ;
VL_INLINE vl_size vl_kmeans_get_max_num_comparisons (VlKMeans const * self) ;
VL_INLINE vl_size vl_kmeans_get_num_trees (VlKMeans const * self) ;
VL_INLINE double vl_kmeans_get_energy (VlKMeans const * self) ;
VL_INLINE void const * vl_kmeans_get_centers (VlKMeans const * self) ;
/** @} */
/** @name Set parameters
** @{
**/
VL_INLINE void vl_kmeans_set_algorithm (VlKMeans * self, VlKMeansAlgorithm algorithm) ;
VL_INLINE void vl_kmeans_set_initialization (VlKMeans * self, VlKMeansInitialization initialization) ;
VL_INLINE void vl_kmeans_set_num_repetitions (VlKMeans * self, vl_size numRepetitions) ;
VL_INLINE void vl_kmeans_set_max_num_iterations (VlKMeans * self, vl_size maxNumIterations) ;
VL_INLINE void vl_kmeans_set_min_energy_variation (VlKMeans * self, double minEnergyVariation) ;
VL_INLINE void vl_kmeans_set_verbosity (VlKMeans * self, int verbosity) ;
VL_INLINE void vl_kmeans_set_max_num_comparisons (VlKMeans * self, vl_size maxNumComparisons) ;
VL_INLINE void vl_kmeans_set_num_trees (VlKMeans * self, vl_size numTrees) ;
/** @} */
/** ------------------------------------------------------------------
** @brief Get data type
** @param self KMeans object instance.
** @return data type.
**/
VL_INLINE vl_type
vl_kmeans_get_data_type (VlKMeans const * self)
{
return self->dataType ;
}
/** @brief Get data dimension
** @param self KMeans object instance.
** @return data dimension.
**/
VL_INLINE vl_size
vl_kmeans_get_dimension (VlKMeans const * self)
{
return self->dimension ;
}
/** @brief Get data type
** @param self KMeans object instance.
** @return data type.
**/
VL_INLINE VlVectorComparisonType
vl_kmeans_get_distance (VlKMeans const * self)
{
return self->distance ;
}
/** @brief Get the number of centers (K)
** @param self KMeans object instance.
** @return number of centers.
**/
VL_INLINE vl_size
vl_kmeans_get_num_centers (VlKMeans const * self)
{
return self->numCenters ;
}
/** @brief Get the number energy of the current fit
** @param self KMeans object instance.
** @return energy.
**/
VL_INLINE double
vl_kmeans_get_energy (VlKMeans const * self)
{
return self->energy ;
}
/** ------------------------------------------------------------------
** @brief Get verbosity level
** @param self KMeans object instance.
** @return verbosity level.
**/
VL_INLINE int
vl_kmeans_get_verbosity (VlKMeans const * self)
{
return self->verbosity ;
}
/** @brief Set verbosity level
** @param self KMeans object instance.
** @param verbosity verbosity level.
**/
VL_INLINE void
vl_kmeans_set_verbosity (VlKMeans * self, int verbosity)
{
self->verbosity = verbosity ;
}
/** ------------------------------------------------------------------
** @brief Get centers
** @param self KMeans object instance.
** @return cluster centers.
**/
VL_INLINE void const *
vl_kmeans_get_centers (VlKMeans const * self)
{
return self->centers ;
}
/** ------------------------------------------------------------------
** @brief Get maximum number of iterations
** @param self KMeans object instance.
** @return maximum number of iterations.
**/
VL_INLINE vl_size
vl_kmeans_get_max_num_iterations (VlKMeans const * self)
{
return self->maxNumIterations ;
}
/** @brief Set maximum number of iterations
** @param self KMeans filter.
** @param maxNumIterations maximum number of iterations.
**/
VL_INLINE void
vl_kmeans_set_max_num_iterations (VlKMeans * self, vl_size maxNumIterations)
{
self->maxNumIterations = maxNumIterations ;
}
/** ------------------------------------------------------------------
** @brief Get maximum number of repetitions.
** @param self KMeans object instance.
** @return current number of repretitions for quantization.
**/
VL_INLINE vl_size
vl_kmeans_get_num_repetitions (VlKMeans const * self)
{
return self->numRepetitions ;
}
/** @brief Set maximum number of repetitions
** @param self KMeans object instance.
** @param numRepetitions maximum number of repetitions.
** The number of repetitions cannot be smaller than 1.
**/
VL_INLINE void
vl_kmeans_set_num_repetitions (VlKMeans * self,
vl_size numRepetitions)
{
assert (numRepetitions >= 1) ;
self->numRepetitions = numRepetitions ;
}
/** ------------------------------------------------------------------
** @brief Get the minimum relative energy variation for convergence.
** @param self KMeans object instance.
** @return minimum energy variation.
**/
VL_INLINE double
vl_kmeans_get_min_energy_variation (VlKMeans const * self)
{
return self->minEnergyVariation ;
}
/** @brief Set the maximum relative energy variation for convergence.
** @param self KMeans object instance.
** @param minEnergyVariation maximum number of repetitions.
** The variation cannot be negative.
**
** The relative energy variation is calculated after the $t$-th update
** to the parameters as:
**
** \[ \epsilon_t = \frac{E_{t-1} - E_t}{E_0 - E_t} \]
**
** Note that this quantitiy is non-negative since $E_{t+1} \leq E_t$.
** Hence, $\epsilon_t$ is the improvement to the energy made in the last
** iteration compared to the total improvement so far. The algorithm
** stops if this value is less or equal than @a minEnergyVariation.
**
** This test is applied only to the LLoyd and ANN algorithms.
**/
VL_INLINE void
vl_kmeans_set_min_energy_variation (VlKMeans * self,
double minEnergyVariation)
{
assert (minEnergyVariation >= 0) ;
self->minEnergyVariation = minEnergyVariation ;
}
/** ------------------------------------------------------------------
** @brief Get K-means algorithm
** @param self KMeans object.
** @return algorithm.
**/
VL_INLINE VlKMeansAlgorithm
vl_kmeans_get_algorithm (VlKMeans const * self)
{
return self->algorithm ;
}
/** @brief Set K-means algorithm
** @param self KMeans object.
** @param algorithm K-means algorithm.
**/
VL_INLINE void
vl_kmeans_set_algorithm (VlKMeans * self, VlKMeansAlgorithm algorithm)
{
self->algorithm = algorithm ;
}
/** ------------------------------------------------------------------
** @brief Get K-means initialization algorithm
** @param self KMeans object.
** @return algorithm.
**/
VL_INLINE VlKMeansInitialization
vl_kmeans_get_initialization (VlKMeans const * self)
{
return self->initialization ;
}
/** @brief Set K-means initialization algorithm
** @param self KMeans object.
** @param initialization initialization.
**/
VL_INLINE void
vl_kmeans_set_initialization (VlKMeans * self,
VlKMeansInitialization initialization)
{
self->initialization = initialization ;
}
/** ------------------------------------------------------------------
** @brief Get the maximum number of comparisons in the KD-forest ANN algorithm.
** @param self KMeans object instance.
** @return maximum number of comparisons.
**/
VL_INLINE vl_size
vl_kmeans_get_max_num_comparisons (VlKMeans const * self)
{
return self->maxNumComparisons ;
}
/** @brief Set maximum number of comparisons in ANN-KD-Tree.
** @param self KMeans filter.
** @param maxNumComparisons maximum number of comparisons.
**/
VL_INLINE void
vl_kmeans_set_max_num_comparisons (VlKMeans * self,
vl_size maxNumComparisons)
{
self->maxNumComparisons = maxNumComparisons;
}
/** ------------------------------------------------------------------
** @brief Set the number of trees in the KD-forest ANN algorithm
** @param self KMeans object instance.
** @param numTrees number of trees to use.
**/
VL_INLINE void
vl_kmeans_set_num_trees (VlKMeans * self, vl_size numTrees)
{
self->numTrees = numTrees;
}
VL_INLINE vl_size
vl_kmeans_get_num_trees (VlKMeans const * self)
{
return self->numTrees;
}
/* VL_IKMEANS_H */
#endif
|