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.

371 lines
8.7 KiB

8 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /** @file hikmeans.c
  2. ** @brief Hierarchical Integer K-Means Clustering - Declaration
  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 hikmeans.h
  13. ** @brief Hierarchical integer K-Means clustering
  14. **
  15. ** Hierarchical integer K-Means clustering (HIKM) is a simple
  16. ** hierarchical version of integer K-Means (@ref ikmeans.h
  17. ** "IKM"). The algorithm recursively applies integer K-means to create
  18. ** more refined partitions of the data.
  19. **
  20. ** Create a tree with ::vl_hikm_new() and delete it with
  21. ** ::vl_hikm_delete(). Use ::vl_hikm_train() to build the tree
  22. ** from training data and ::vl_hikm_push() to project new data down
  23. ** a HIKM tree.
  24. **
  25. ** @section hikm-tree HIKM tree
  26. **
  27. ** The HIKM tree is represented by a ::VlHIKMTree structure, which
  28. ** contains a tree composed of ::VlHIKMNode. Each node is an
  29. ** integer K-means filter which partitions the data into @c K
  30. ** clusters.
  31. **/
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <string.h>
  35. #include <math.h>
  36. #include "hikmeans.h"
  37. /** ------------------------------------------------------------------
  38. ** @internal
  39. ** @brief Copy a subset of the data to a buffer
  40. ** @param data Data
  41. ** @param ids Data labels
  42. ** @param N Number of indices
  43. ** @param M Data dimensionality
  44. ** @param id Label of data to copy
  45. ** @param N2 Number of data copied (out)
  46. ** @return a new buffer with a copy of the selected data.
  47. **/
  48. vl_uint8*
  49. vl_hikm_copy_subset (vl_uint8 const * data,
  50. vl_uint32 *ids,
  51. vl_size N, vl_size M,
  52. vl_uint32 id, vl_size *N2)
  53. {
  54. vl_uindex i ;
  55. vl_size count = 0;
  56. /* count how many data points with this label there are */
  57. for (i = 0 ; i < N ; i++) {
  58. if (ids[i] == id) {
  59. count ++ ;
  60. }
  61. }
  62. *N2 = count ;
  63. /* copy each datum to the buffer */
  64. {
  65. vl_uint8 *new_data = (vl_uint8*) vl_malloc (sizeof(*new_data) * M * count);
  66. count = 0;
  67. for (i = 0 ; i < N ; i ++) {
  68. if (ids[i] == id) {
  69. memcpy(new_data + count * M,
  70. data + i * M,
  71. sizeof(*new_data) * M);
  72. count ++ ;
  73. }
  74. }
  75. *N2 = count ;
  76. return new_data ;
  77. }
  78. }
  79. /** ------------------------------------------------------------------
  80. ** @brief Compute HIKM clustering.
  81. **
  82. ** @param tree HIKM tree to initialize.
  83. ** @param data Data to cluster.
  84. ** @param N Number of data points.
  85. ** @param K Number of clusters for this node.
  86. ** @param height Tree height.
  87. **
  88. ** @remark height cannot be smaller than 1.
  89. **
  90. ** @return a new HIKM node representing a sub-clustering.
  91. **/
  92. static VlHIKMNode *
  93. xmeans (VlHIKMTree *tree,
  94. vl_uint8 const *data,
  95. vl_size N, vl_size K, vl_size height)
  96. {
  97. VlHIKMNode *node = (VlHIKMNode*)vl_malloc (sizeof(*node)) ;
  98. vl_uint32 *ids = (vl_uint32*)vl_malloc (sizeof(*ids) * N) ;
  99. node->filter = vl_ikm_new (tree -> method) ;
  100. node->children = (VlHIKMNode**)( (height == 1) ? 0 : vl_malloc (sizeof(*node->children) * K) );
  101. vl_ikm_set_max_niters (node->filter, tree->max_niters) ;
  102. vl_ikm_set_verbosity (node->filter, tree->verb - 1 ) ;
  103. vl_ikm_init_rand_data (node->filter, data, tree->M, N, K) ;
  104. vl_ikm_train (node->filter, data, N) ;
  105. vl_ikm_push (node->filter, ids, data, N) ;
  106. /* recursively process each child */
  107. if (height > 1) {
  108. vl_uindex k ;
  109. for (k = 0 ; k < K ; ++k) {
  110. vl_size partition_N ;
  111. vl_size partition_K ;
  112. vl_uint8 *partition ;
  113. partition = vl_hikm_copy_subset
  114. (data, ids, N, tree->M, (vl_uint32)k, &partition_N) ;
  115. partition_K = VL_MIN (K, partition_N) ;
  116. node->children [k] = xmeans
  117. (tree, partition, partition_N, partition_K, height - 1) ;
  118. vl_free (partition) ;
  119. if (tree->verb > (signed)tree->depth - (signed)height) {
  120. VL_PRINTF("hikmeans: branch at depth %d: %6.1f %% completed\n",
  121. tree->depth - height,
  122. (double) (k+1) / K * 100) ;
  123. }
  124. }
  125. }
  126. vl_free (ids) ;
  127. return node ;
  128. }
  129. /** ------------------------------------------------------------------
  130. ** @internal
  131. ** @brief Delete node
  132. **
  133. ** @param node to delete.
  134. **
  135. ** The function deletes recursively @a node and all its descendent.
  136. **/
  137. static void
  138. xdelete (VlHIKMNode *node)
  139. {
  140. if(node) {
  141. vl_uindex k ;
  142. if (node->children) {
  143. for(k = 0 ; k < vl_ikm_get_K (node->filter) ; ++k)
  144. xdelete (node->children[k]) ;
  145. vl_free (node->children) ;
  146. }
  147. if (node->filter) {
  148. vl_ikm_delete (node->filter) ;
  149. }
  150. vl_free(node);
  151. }
  152. }
  153. /** ------------------------------------------------------------------
  154. ** @brief New HIKM tree
  155. ** @param method clustering method.
  156. ** @return new HIKM tree.
  157. **/
  158. VlHIKMTree *
  159. vl_hikm_new (int method)
  160. {
  161. VlHIKMTree *f = (VlHIKMTree*)vl_calloc (sizeof(VlHIKMTree), 1) ;
  162. f->max_niters = 200 ;
  163. f->method = method ;
  164. return f ;
  165. }
  166. /** ------------------------------------------------------------------
  167. ** @brief Delete HIKM tree
  168. ** @param f HIKM tree.
  169. **/
  170. void
  171. vl_hikm_delete (VlHIKMTree *f)
  172. {
  173. if (f) {
  174. xdelete (f->root) ;
  175. vl_free (f) ;
  176. }
  177. }
  178. /** ------------------------------------------------------------------
  179. ** @brief Initialize HIKM tree
  180. ** @param f HIKM tree.
  181. ** @param M Data dimensionality.
  182. ** @param K Number of clusters per node.
  183. ** @param depth Tree depth.
  184. ** @return a new HIKM tree representing the clustering.
  185. **
  186. ** @remark @a depth cannot be smaller than 1.
  187. **/
  188. void
  189. vl_hikm_init (VlHIKMTree *f, vl_size M, vl_size K, vl_size depth)
  190. {
  191. assert(depth > 0) ;
  192. assert(M > 0) ;
  193. assert(K > 0) ;
  194. xdelete (f -> root) ;
  195. f->root = 0;
  196. f->M = M ;
  197. f->K = K ;
  198. f->depth = depth ;
  199. }
  200. /** ------------------------------------------------------------------
  201. ** @brief Train HIKM tree
  202. ** @param f HIKM tree.
  203. ** @param data Data to cluster.
  204. ** @param N Number of data.
  205. **/
  206. void
  207. vl_hikm_train (VlHIKMTree *f, vl_uint8 const *data, vl_size N)
  208. {
  209. f->root= xmeans (f, data, N, VL_MIN(f->K, N), f->depth) ;
  210. }
  211. /** ------------------------------------------------------------------
  212. ** @brief Project data down HIKM tree
  213. ** @param f HIKM tree.
  214. ** @param asgn Path down the tree (out).
  215. ** @param data Data to project.
  216. ** @param N Number of data.
  217. **
  218. ** The function writes to @a asgn the path of the data @a data
  219. ** down the HIKM tree @a f. The parameter @a asgn must point to
  220. ** an array of @c M by @c N elements, where @c M is the depth of
  221. ** the HIKM tree and @c N is the number of data point to process.
  222. **/
  223. void
  224. vl_hikm_push (VlHIKMTree *f, vl_uint32 *asgn, vl_uint8 const *data, vl_size N)
  225. {
  226. vl_uindex i, d ;
  227. vl_size M = vl_hikm_get_ndims (f) ;
  228. vl_size depth = vl_hikm_get_depth (f) ;
  229. /* for each datum */
  230. for(i = 0 ; i < N ; i++) {
  231. VlHIKMNode *node = f->root ;
  232. d = 0 ;
  233. while (node) {
  234. vl_uint32 best ;
  235. vl_ikm_push (node->filter,
  236. &best,
  237. data + i * M, 1) ;
  238. asgn[i * depth + d] = best ;
  239. ++ d ;
  240. if (!node->children) break ;
  241. node = node->children [best] ;
  242. }
  243. }
  244. }
  245. /* ---------------------------------------------------------------- */
  246. /* Setters and getters */
  247. /* ---------------------------------------------------------------- */
  248. /** @brief Get data dimensionality
  249. ** @param f HIKM tree.
  250. ** @return data dimensionality.
  251. **/
  252. vl_size
  253. vl_hikm_get_ndims (VlHIKMTree const* f)
  254. {
  255. return f->M ;
  256. }
  257. /** @brief Get K
  258. ** @param f HIKM tree.
  259. ** @return K.
  260. **/
  261. vl_size
  262. vl_hikm_get_K (VlHIKMTree const *f)
  263. {
  264. return f->K ;
  265. }
  266. /** @brief Get depth
  267. ** @param f HIKM tree.
  268. ** @return depth.
  269. **/
  270. vl_size
  271. vl_hikm_get_depth (VlHIKMTree const *f)
  272. {
  273. return f->depth ;
  274. }
  275. /** @brief Get verbosity level
  276. ** @param f HIKM tree.
  277. ** @return verbosity level.
  278. **/
  279. int
  280. vl_hikm_get_verbosity (VlHIKMTree const *f)
  281. {
  282. return f->verb ;
  283. }
  284. /** @brief Get maximum number of iterations
  285. ** @param f HIKM tree.
  286. ** @return maximum number of iterations.
  287. **/
  288. vl_size
  289. vl_hikm_get_max_niters (VlHIKMTree const *f)
  290. {
  291. return f-> max_niters ;
  292. }
  293. /** @brief Get maximum number of iterations
  294. ** @param f HIKM tree.
  295. ** @return maximum number of iterations.
  296. **/
  297. VlHIKMNode const *
  298. vl_hikm_get_root (VlHIKMTree const *f)
  299. {
  300. return f->root ;
  301. }
  302. /** @brief Set verbosity level
  303. ** @param f HIKM tree.
  304. ** @param verb verbosity level.
  305. **/
  306. void
  307. vl_hikm_set_verbosity (VlHIKMTree *f, int verb)
  308. {
  309. f->verb = verb ;
  310. }
  311. /** @brief Set maximum number of iterations
  312. ** @param f HIKM tree.
  313. ** @param max_niters maximum number of iterations.
  314. **/
  315. void
  316. vl_hikm_set_max_niters (VlHIKMTree *f, int max_niters)
  317. {
  318. f->max_niters = max_niters ;
  319. }