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.

435 lines
13 KiB

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