You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

297 lines
7.7 KiB

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