Debian Codegraph @

src/thirdparty/VLFeat/kmeans.h

raw
1 /** @file kmeans.h
2 ** @brief K-means (@ref kmeans)
3 ** @author Andrea Vedaldi
4 ** @author David Novotny
5 **/
6
7 /*
8 Copyright (C) 2007-12 Andrea Vedaldi and Brian Fulkerson.
9 Copyright (C) 2013 Andrea Vedaldi and David Novotny.
10 All rights reserved.
11
12 This file is part of the VLFeat library and is made available under
13 the terms of the BSD license (see the COPYING file).
14 */
15
16 #ifndef VL_KMEANS_H
17 #define VL_KMEANS_H
18
19 #include "generic.h"
20 #include "random.h"
21 #include "mathop.h"
22 #include "kdtree.h"
23
24 /* ---------------------------------------------------------------- */
25
26 /** @brief K-means algorithms */
27
28 typedef enum _VlKMeansAlgorithm {
29 VlKMeansLloyd, /**< Lloyd algorithm */
30 VlKMeansElkan, /**< Elkan algorithm */
31 VlKMeansANN /**< Approximate nearest neighbors */
32 } VlKMeansAlgorithm ;
33
34 /** @brief K-means initialization algorithms */
35
36 typedef enum _VlKMeansInitialization {
37 VlKMeansRandomSelection, /**< Randomized selection */
38 VlKMeansPlusPlus /**< Plus plus raondomized selection */
39 } VlKMeansInitialization ;
40
41 /** ------------------------------------------------------------------
42 ** @brief K-means quantizer
43 **/
44
45 typedef struct _VlKMeans
46 {
47
48 vl_type dataType ; /**< Data type. */
49 vl_size dimension ; /**< Data dimensionality. */
50 vl_size numCenters ; /**< Number of centers. */
51 vl_size numTrees ; /**< Number of trees in forest when using ANN-kmeans. */
52 vl_size maxNumComparisons ; /**< Maximum number of comparisons when using ANN-kmeans. */
53
54 VlKMeansInitialization initialization ; /**< Initalization algorithm. */
55 VlKMeansAlgorithm algorithm ; /**< Clustring algorithm. */
56 VlVectorComparisonType distance ; /**< Distance. */
57 vl_size maxNumIterations ; /**< Maximum number of refinement iterations. */
58 double minEnergyVariation ; /**< Minimum energy variation. */
59 vl_size numRepetitions ; /**< Number of clustering repetitions. */
60 int verbosity ; /**< Verbosity level. */
61
62 void * centers ; /**< Centers */
63 void * centerDistances ; /**< Centers inter-distances. */
64
65 double energy ; /**< Current solution energy. */
66 VlFloatVectorComparisonFunction floatVectorComparisonFn ;
67 VlDoubleVectorComparisonFunction doubleVectorComparisonFn ;
68 } VlKMeans ;
69
70 /** @name Create and destroy
71 ** @{
72 **/
73 VL_EXPORT VlKMeans * vl_kmeans_new (vl_type dataType, VlVectorComparisonType distance) ;
74 VL_EXPORT VlKMeans * vl_kmeans_new_copy (VlKMeans const * kmeans) ;
75 VL_EXPORT void vl_kmeans_delete (VlKMeans * self) ;
76 /** @} */
77
78 /** @name Basic data processing
79 ** @{
80 **/
81 VL_EXPORT void vl_kmeans_reset (VlKMeans * self) ;
82
83 VL_EXPORT double vl_kmeans_cluster (VlKMeans * self,
84 void const * data,
85 vl_size dimension,
86 vl_size numData,
87 vl_size numCenters) ;
88
89 VL_EXPORT void vl_kmeans_quantize (VlKMeans * self,
90 vl_uint32 * assignments,
91 void * distances,
92 void const * data,
93 vl_size numData) ;
94
95 VL_EXPORT void vl_kmeans_quantize_ANN (VlKMeans * self,
96 vl_uint32 * assignments,
97 void * distances,
98 void const * data,
99 vl_size numData,
100 vl_size iteration );
101 /** @} */
102
103 /** @name Advanced data processing
104 ** @{
105 **/
106 VL_EXPORT void vl_kmeans_set_centers (VlKMeans * self,
107 void const * centers,
108 vl_size dimension,
109 vl_size numCenters) ;
110
111 VL_EXPORT void vl_kmeans_init_centers_with_rand_data
112 (VlKMeans * self,
113 void const * data,
114 vl_size dimensions,
115 vl_size numData,
116 vl_size numCenters) ;
117
118 VL_EXPORT void vl_kmeans_init_centers_plus_plus
119 (VlKMeans * self,
120 void const * data,
121 vl_size dimensions,
122 vl_size numData,
123 vl_size numCenters) ;
124
125 VL_EXPORT double vl_kmeans_refine_centers (VlKMeans * self,
126 void const * data,
127 vl_size numData) ;
128
129 /** @} */
130
131 /** @name Retrieve data and parameters
132 ** @{
133 **/
134 VL_INLINE vl_type vl_kmeans_get_data_type (VlKMeans const * self) ;
135 VL_INLINE VlVectorComparisonType vl_kmeans_get_distance (VlKMeans const * self) ;
136
137 VL_INLINE VlKMeansAlgorithm vl_kmeans_get_algorithm (VlKMeans const * self) ;
138 VL_INLINE VlKMeansInitialization vl_kmeans_get_initialization (VlKMeans const * self) ;
139 VL_INLINE vl_size vl_kmeans_get_num_repetitions (VlKMeans const * self) ;
140
141 VL_INLINE vl_size vl_kmeans_get_dimension (VlKMeans const * self) ;
142 VL_INLINE vl_size vl_kmeans_get_num_centers (VlKMeans const * self) ;
143
144 VL_INLINE int vl_kmeans_get_verbosity (VlKMeans const * self) ;
145 VL_INLINE vl_size vl_kmeans_get_max_num_iterations (VlKMeans const * self) ;
146 VL_INLINE double vl_kmeans_get_min_energy_variation (VlKMeans const * self) ;
147 VL_INLINE vl_size vl_kmeans_get_max_num_comparisons (VlKMeans const * self) ;
148 VL_INLINE vl_size vl_kmeans_get_num_trees (VlKMeans const * self) ;
149 VL_INLINE double vl_kmeans_get_energy (VlKMeans const * self) ;
150 VL_INLINE void const * vl_kmeans_get_centers (VlKMeans const * self) ;
151 /** @} */
152
153 /** @name Set parameters
154 ** @{
155 **/
156 VL_INLINE void vl_kmeans_set_algorithm (VlKMeans * self, VlKMeansAlgorithm algorithm) ;
157 VL_INLINE void vl_kmeans_set_initialization (VlKMeans * self, VlKMeansInitialization initialization) ;
158 VL_INLINE void vl_kmeans_set_num_repetitions (VlKMeans * self, vl_size numRepetitions) ;
159 VL_INLINE void vl_kmeans_set_max_num_iterations (VlKMeans * self, vl_size maxNumIterations) ;
160 VL_INLINE void vl_kmeans_set_min_energy_variation (VlKMeans * self, double minEnergyVariation) ;
161 VL_INLINE void vl_kmeans_set_verbosity (VlKMeans * self, int verbosity) ;
162 VL_INLINE void vl_kmeans_set_max_num_comparisons (VlKMeans * self, vl_size maxNumComparisons) ;
163 VL_INLINE void vl_kmeans_set_num_trees (VlKMeans * self, vl_size numTrees) ;
164 /** @} */
165
166 /** ------------------------------------------------------------------
167 ** @brief Get data type
168 ** @param self KMeans object instance.
169 ** @return data type.
170 **/
171
172 VL_INLINE vl_type
173 vl_kmeans_get_data_type (VlKMeans const * self)
174 {
175 return self->dataType ;
176 }
177
178 /** @brief Get data dimension
179 ** @param self KMeans object instance.
180 ** @return data dimension.
181 **/
182
183 VL_INLINE vl_size
184 vl_kmeans_get_dimension (VlKMeans const * self)
185 {
186 return self->dimension ;
187 }
188
189 /** @brief Get data type
190 ** @param self KMeans object instance.
191 ** @return data type.
192 **/
193
194 VL_INLINE VlVectorComparisonType
195 vl_kmeans_get_distance (VlKMeans const * self)
196 {
197 return self->distance ;
198 }
199
200 /** @brief Get the number of centers (K)
201 ** @param self KMeans object instance.
202 ** @return number of centers.
203 **/
204
205 VL_INLINE vl_size
206 vl_kmeans_get_num_centers (VlKMeans const * self)
207 {
208 return self->numCenters ;
209 }
210
211 /** @brief Get the number energy of the current fit
212 ** @param self KMeans object instance.
213 ** @return energy.
214 **/
215
216 VL_INLINE double
217 vl_kmeans_get_energy (VlKMeans const * self)
218 {
219 return self->energy ;
220 }
221
222 /** ------------------------------------------------------------------
223 ** @brief Get verbosity level
224 ** @param self KMeans object instance.
225 ** @return verbosity level.
226 **/
227
228 VL_INLINE int
229 vl_kmeans_get_verbosity (VlKMeans const * self)
230 {
231 return self->verbosity ;
232 }
233
234 /** @brief Set verbosity level
235 ** @param self KMeans object instance.
236 ** @param verbosity verbosity level.
237 **/
238
239 VL_INLINE void
240 vl_kmeans_set_verbosity (VlKMeans * self, int verbosity)
241 {
242 self->verbosity = verbosity ;
243 }
244
245 /** ------------------------------------------------------------------
246 ** @brief Get centers
247 ** @param self KMeans object instance.
248 ** @return cluster centers.
249 **/
250
251 VL_INLINE void const *
252 vl_kmeans_get_centers (VlKMeans const * self)
253 {
254 return self->centers ;
255 }
256
257 /** ------------------------------------------------------------------
258 ** @brief Get maximum number of iterations
259 ** @param self KMeans object instance.
260 ** @return maximum number of iterations.
261 **/
262
263 VL_INLINE vl_size
264 vl_kmeans_get_max_num_iterations (VlKMeans const * self)
265 {
266 return self->maxNumIterations ;
267 }
268
269 /** @brief Set maximum number of iterations
270 ** @param self KMeans filter.
271 ** @param maxNumIterations maximum number of iterations.
272 **/
273
274 VL_INLINE void
275 vl_kmeans_set_max_num_iterations (VlKMeans * self, vl_size maxNumIterations)
276 {
277 self->maxNumIterations = maxNumIterations ;
278 }
279
280 /** ------------------------------------------------------------------
281 ** @brief Get maximum number of repetitions.
282 ** @param self KMeans object instance.
283 ** @return current number of repretitions for quantization.
284 **/
285
286 VL_INLINE vl_size
287 vl_kmeans_get_num_repetitions (VlKMeans const * self)
288 {
289 return self->numRepetitions ;
290 }
291
292 /** @brief Set maximum number of repetitions
293 ** @param self KMeans object instance.
294 ** @param numRepetitions maximum number of repetitions.
295 ** The number of repetitions cannot be smaller than 1.
296 **/
297
298 VL_INLINE void
299 vl_kmeans_set_num_repetitions (VlKMeans * self,
300 vl_size numRepetitions)
301 {
302 assert (numRepetitions >= 1) ;
303 self->numRepetitions = numRepetitions ;
304 }
305
306 /** ------------------------------------------------------------------
307 ** @brief Get the minimum relative energy variation for convergence.
308 ** @param self KMeans object instance.
309 ** @return minimum energy variation.
310 **/
311
312 VL_INLINE double
313 vl_kmeans_get_min_energy_variation (VlKMeans const * self)
314 {
315 return self->minEnergyVariation ;
316 }
317
318 /** @brief Set the maximum relative energy variation for convergence.
319 ** @param self KMeans object instance.
320 ** @param minEnergyVariation maximum number of repetitions.
321 ** The variation cannot be negative.
322 **
323 ** The relative energy variation is calculated after the $t$-th update
324 ** to the parameters as:
325 **
326 ** \[ \epsilon_t = \frac{E_{t-1} - E_t}{E_0 - E_t} \]
327 **
328 ** Note that this quantitiy is non-negative since $E_{t+1} \leq E_t$.
329 ** Hence, $\epsilon_t$ is the improvement to the energy made in the last
330 ** iteration compared to the total improvement so far. The algorithm
331 ** stops if this value is less or equal than @a minEnergyVariation.
332 **
333 ** This test is applied only to the LLoyd and ANN algorithms.
334 **/
335
336 VL_INLINE void
337 vl_kmeans_set_min_energy_variation (VlKMeans * self,
338 double minEnergyVariation)
339 {
340 assert (minEnergyVariation >= 0) ;
341 self->minEnergyVariation = minEnergyVariation ;
342 }
343
344 /** ------------------------------------------------------------------
345 ** @brief Get K-means algorithm
346 ** @param self KMeans object.
347 ** @return algorithm.
348 **/
349
350 VL_INLINE VlKMeansAlgorithm
351 vl_kmeans_get_algorithm (VlKMeans const * self)
352 {
353 return self->algorithm ;
354 }
355
356 /** @brief Set K-means algorithm
357 ** @param self KMeans object.
358 ** @param algorithm K-means algorithm.
359 **/
360
361 VL_INLINE void
362 vl_kmeans_set_algorithm (VlKMeans * self, VlKMeansAlgorithm algorithm)
363 {
364 self->algorithm = algorithm ;
365 }
366
367 /** ------------------------------------------------------------------
368 ** @brief Get K-means initialization algorithm
369 ** @param self KMeans object.
370 ** @return algorithm.
371 **/
372
373 VL_INLINE VlKMeansInitialization
374 vl_kmeans_get_initialization (VlKMeans const * self)
375 {
376 return self->initialization ;
377 }
378
379 /** @brief Set K-means initialization algorithm
380 ** @param self KMeans object.
381 ** @param initialization initialization.
382 **/
383
384 VL_INLINE void
385 vl_kmeans_set_initialization (VlKMeans * self,
386 VlKMeansInitialization initialization)
387 {
388 self->initialization = initialization ;
389 }
390
391 /** ------------------------------------------------------------------
392 ** @brief Get the maximum number of comparisons in the KD-forest ANN algorithm.
393 ** @param self KMeans object instance.
394 ** @return maximum number of comparisons.
395 **/
396
397 VL_INLINE vl_size
398 vl_kmeans_get_max_num_comparisons (VlKMeans const * self)
399 {
400 return self->maxNumComparisons ;
401 }
402
403 /** @brief Set maximum number of comparisons in ANN-KD-Tree.
404 ** @param self KMeans filter.
405 ** @param maxNumComparisons maximum number of comparisons.
406 **/
407
408 VL_INLINE void
409 vl_kmeans_set_max_num_comparisons (VlKMeans * self,
410 vl_size maxNumComparisons)
411 {
412 self->maxNumComparisons = maxNumComparisons;
413 }
414
415 /** ------------------------------------------------------------------
416 ** @brief Set the number of trees in the KD-forest ANN algorithm
417 ** @param self KMeans object instance.
418 ** @param numTrees number of trees to use.
419 **/
420
421 VL_INLINE void
422 vl_kmeans_set_num_trees (VlKMeans * self, vl_size numTrees)
423 {
424 self->numTrees = numTrees;
425 }
426
427 VL_INLINE vl_size
428 vl_kmeans_get_num_trees (VlKMeans const * self)
429 {
430 return self->numTrees;
431 }
432
433
434 /* VL_IKMEANS_H */
435 #endif