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.

323 lines
10 KiB

8 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /** @file vlad.c
  2. ** @brief VLAD - Declaration
  3. ** @author David Novotny
  4. ** @author Andrea Vedaldi
  5. **/
  6. /*
  7. Copyright (C) 2013 David Novotny and Andera Vedaldi.
  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. /**
  13. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  14. @page vlad Vector of Locally Aggregated Descriptors (VLAD) encoding
  15. @author David Novotny
  16. @author Andrea Vedaldi
  17. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  18. @ref vlad.h implements the *Vector of Linearly Aggregated Descriptors*
  19. (VLAD) image representation @cite{jegou10aggregating}
  20. @cite{arandjelovic13all-about}.
  21. @ref vlad-starting demonstreates how to use the C API to compute the
  22. VLAD representation of an image. For further details on the VLAD image
  23. representation refer to:
  24. - @subpage vlad-fundamentals - VLAD definition and computation.
  25. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  26. @section vlad-starting Getting started with VLAD
  27. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  28. The VLAD encoding of a set of features is obtained by using the
  29. function ::vl_vlad_encode. The function can be applied to both @c
  30. float or @c double data types.
  31. ::vl_vlad_encode requires a visual dictionary, for example obtained by
  32. using @ref kmeans. Furthermore, the assignments of features to
  33. dictionary elements must be pre-computed, for example by using @ref
  34. kdtree.
  35. In the following example code, the vocabulary is first created using
  36. the KMeans clustering, then the points, that are to be encoded are
  37. assigned to its corresponding nearest vocabulary words, after that the
  38. original vlad encoding routine without any normalization option takes place.
  39. At the end of the process the encoding is stored in the @c enc variable.
  40. @code
  41. vl_uint32 * indexes;
  42. float * assignments;
  43. float * enc
  44. int i;
  45. // create a KMeans object and run clustering to get vocabulary words (centers)
  46. kmeans = vl_kmeans_new (VLDistanceL2, VL_TYPE_FLOAT) ;
  47. vl_kmeans_cluster (kmeans,
  48. data,
  49. dimension,
  50. numData,
  51. numCenters) ;
  52. // find nearest cliuster centers for the data that should be encoded
  53. indexes = vl_malloc(sizeof(vl_uint32) * numDataToEncode);
  54. vl_kmeans_quantize(kmeans,indexes,dataToEncode,numDataToEncode);
  55. // convert indexes array to assignments array,
  56. // which can be processed by vl_vlad_encode
  57. assignments = vl_malloc(sizeof(float) * numDataToEncode * numCenters);
  58. memset(assignments, 0, sizeof(float) * numDataToEncode * numCenters);
  59. for(i = 0; i < numDataToEncode; i++) {
  60. assignments[i * numCenters + indexes[i]] = 1.;
  61. }
  62. // allocate space for vlad encoding
  63. enc = vl_malloc(sizeof(TYPE) * dimension * numCenters);
  64. // do the encoding job
  65. vl_vlad_encode (enc, VL_F_TYPE,
  66. vl_kmeans_get_centers(kmeans), dimension, numCenters,
  67. data, numData,
  68. assignments,
  69. 0) ;
  70. @endcode
  71. Various @ref vlad-normalization normalizations can be applied to the
  72. VLAD vectors. These are controlled by the parameter @a flag of
  73. ::vl_vlad_encode.
  74. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  75. @page vlad-fundamentals VLAD fundamentals
  76. @tableofcontents
  77. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  78. This page describes the *Vector of Locally Aggregated Descriptors*
  79. (VLAD) image encoding of @cite{jegou10aggregating}. See @ref vlad for
  80. an overview of the C API.
  81. VLAD is a *feature encoding and pooling* method, similar to @ref
  82. fisher "Fisher vectors". VLAD encodes a set of local feature
  83. descriptors $I=(\bx_1,\dots,\bx_n)$ extracted from an image using a
  84. dictionary built using a clustering method such as @ref gmm or @ref
  85. kmeans. Let $q_{ik}$ be the strength of the association of data vector
  86. $\bx_i$ to cluster $\mu_k$, such that $q_{ik} \geq 0$ and
  87. $\sum_{k=1}^K q_{ik} = 1$. The association may be either soft
  88. (e.g. obtained as the posterior probabilities of the GMM clusters) or
  89. hard (e.g. obtained by vector quantization with K-means).
  90. $\mu_k$ are the cluster *means*, vectors of the same dimension as the
  91. data $\bx_i$. VLAD encodes feature $\bx$ by considering the *residuals*
  92. \[
  93. \bv_k = \sum_{i=1}^{N} q_{ik} (\bx_{i} - \mu_k).
  94. \]
  95. The residulas are stacked together to obtain the vector
  96. \[
  97. \hat\Phi(I) =
  98. \begin{bmatrix}
  99. \vdots \\
  100. \bv_k \\
  101. \vdots
  102. \end{bmatrix}
  103. \]
  104. Before the VLAD encoding is used it is usually normalized, as
  105. explained @ref vlad-normalization next.
  106. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  107. @section vlad-normalization VLAD normalization
  108. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  109. VLFeat VLAD implementation supports a number of different
  110. normalization strategies. These are optionally applied in this order:
  111. - **Component-wise mass normalization.** Each vector $\bv_k$ is
  112. divided by the total mass of features associated to it $\sum_{i=1}^N
  113. q_{ik}$.
  114. - **Square-rooting.** The function $\sign(z)\sqrt{|z|}$ is applied to
  115. all scalar components of the VLAD descriptor.
  116. - **Component-wise $l^2$ normalization.** The vectors $\bv_k$ are
  117. divided by their norm $\|\bv_k\|_2$.
  118. - **Global $l^2$ normalization.** The VLAD descriptor $\hat\Phi(I)$ is
  119. divided by its norm $\|\hat\Phi(I)\|_2$.
  120. */
  121. #include "vlad.h"
  122. #include "mathop.h"
  123. #include <stdio.h>
  124. #include <stdlib.h>
  125. #include <string.h>
  126. #if defined(_OPENMP)
  127. #include <omp.h>
  128. #endif
  129. /* ================================================================ */
  130. #ifdef VL_VLAD_INSTANTIATING
  131. static void
  132. VL_XCAT(_vl_vlad_encode_, SFX)
  133. (TYPE * enc,
  134. TYPE const * means, vl_size dimension, vl_size numClusters,
  135. TYPE const * data, vl_size numData,
  136. TYPE const * assignments,
  137. int flags)
  138. {
  139. vl_uindex dim ;
  140. vl_index i_cl, i_d ;
  141. memset(enc, 0, sizeof(TYPE) * dimension * numClusters) ;
  142. #if defined(_OPENMP)
  143. #pragma omp parallel for default(shared) private(i_cl,i_d,dim) num_threads(vl_get_max_threads())
  144. #endif
  145. for (i_cl = 0; i_cl < (signed)numClusters; i_cl++) {
  146. double clusterMass = 0 ;
  147. for (i_d = 0; i_d < (signed)numData; i_d++) {
  148. if (assignments[i_d*numClusters + i_cl] > 0) {
  149. double q = assignments[i_d*numClusters+i_cl] ;
  150. clusterMass += q ;
  151. for(dim = 0; dim < dimension; dim++) {
  152. enc [i_cl * dimension + dim] += q * data [i_d * dimension + dim] ;
  153. }
  154. }
  155. }
  156. if (clusterMass > 0) {
  157. if (flags & VL_VLAD_FLAG_NORMALIZE_MASS) {
  158. for(dim = 0; dim < dimension; dim++) {
  159. enc[i_cl*dimension + dim] /= clusterMass ;
  160. enc[i_cl*dimension + dim] -= means[i_cl*dimension+dim];
  161. }
  162. } else {
  163. for(dim = 0; dim < dimension; dim++) {
  164. enc[i_cl*dimension + dim] -= clusterMass * means[i_cl*dimension+dim];
  165. }
  166. }
  167. }
  168. if (flags & VL_VLAD_FLAG_SQUARE_ROOT) {
  169. for(dim = 0; dim < dimension; dim++) {
  170. TYPE z = enc[i_cl*dimension + dim] ;
  171. if (z >= 0) {
  172. enc[i_cl*dimension + dim] = VL_XCAT(vl_sqrt_, SFX)(z) ;
  173. } else {
  174. enc[i_cl*dimension + dim] = - VL_XCAT(vl_sqrt_, SFX)(- z) ;
  175. }
  176. }
  177. }
  178. if (flags & VL_VLAD_FLAG_NORMALIZE_COMPONENTS) {
  179. TYPE n = 0 ;
  180. dim = 0 ;
  181. for(dim = 0; dim < dimension; dim++) {
  182. TYPE z = enc[i_cl*dimension + dim] ;
  183. n += z * z ;
  184. }
  185. n = VL_XCAT(vl_sqrt_, SFX)(n) ;
  186. n = VL_MAX(n, 1e-12) ;
  187. for(dim = 0; dim < dimension; dim++) {
  188. enc[i_cl*dimension + dim] /= n ;
  189. }
  190. }
  191. }
  192. if (! (flags & VL_VLAD_FLAG_UNNORMALIZED)) {
  193. TYPE n = 0 ;
  194. for(dim = 0 ; dim < dimension * numClusters ; dim++) {
  195. TYPE z = enc [dim] ;
  196. n += z * z ;
  197. }
  198. n = VL_XCAT(vl_sqrt_, SFX)(n) ;
  199. n = VL_MAX(n, 1e-12) ;
  200. for(dim = 0 ; dim < dimension * numClusters ; dim++) {
  201. enc[dim] /= n ;
  202. }
  203. }
  204. }
  205. /* VL_FISHER_INSTANTIATING */
  206. #else
  207. #ifndef __DOXYGEN__
  208. #define FLT VL_TYPE_FLOAT
  209. #define TYPE float
  210. #define SFX f
  211. #define VL_VLAD_INSTANTIATING
  212. #include "vlad.c"
  213. #define FLT VL_TYPE_DOUBLE
  214. #define TYPE double
  215. #define SFX d
  216. #define VL_VLAD_INSTANTIATING
  217. #include "vlad.c"
  218. #endif
  219. /* VL_VLAD_INSTANTIATING */
  220. #endif
  221. /* ================================================================ */
  222. #ifndef VL_VLAD_INSTANTIATING
  223. /** @brief VLAD encoding of a set of vectors.
  224. ** @param enc output VLAD encoding (out).
  225. ** @param dataType the type of the input data (::VL_TYPE_DOUBLE or ::VL_TYPE_FLOAT).
  226. ** @param numData number of data vectors to encode.
  227. ** @param means cluster means.
  228. ** @param numClusters number of clusters.
  229. ** @param data the data vectors to encode.
  230. ** @param dimension dimensionality of the data.
  231. ** @param assignments data to cluster soft assignments.
  232. ** @param flags options.
  233. **
  234. ** @a enc is the VLAD vector of size @a numClusters by
  235. ** @a dimension. @a means is a matrix with @a numClusters columns and
  236. ** @a dimension rows. @a data is the matrix of vectors to be encoded,
  237. ** with @a dimension rows and @a numData columns. @a assignments is a
  238. ** matrix with @a numClusters rows and @a numData columns.
  239. ** All the matrices should be stored in column-major order.
  240. **
  241. ** @a flag allows controlling further options:
  242. ** ::VL_VLAD_FLAG_NORMALIZE_COMPONENTS, ::VL_VLAD_FLAG_SQUARE_ROOT,
  243. ** ::VL_VLAD_FLAG_UNNORMALIZED, and ::VL_VLAD_FLAG_NORMALIZE_MASS.
  244. **
  245. ** @sa @ref vlad
  246. **/
  247. void
  248. vl_vlad_encode (void * enc, vl_type dataType,
  249. void const * means, vl_size dimension, vl_size numClusters,
  250. void const * data, vl_size numData,
  251. void const * assignments,
  252. int flags)
  253. {
  254. switch(dataType) {
  255. case VL_TYPE_FLOAT:
  256. _vl_vlad_encode_f ((float *) enc,
  257. (float const *) means, dimension, numClusters,
  258. (float const *) data, numData,
  259. (float const *) assignments, flags) ;
  260. break;
  261. case VL_TYPE_DOUBLE:
  262. _vl_vlad_encode_d ((double *) enc,
  263. (double const *) means, dimension, numClusters,
  264. (double const *) data, numData,
  265. (double const *) assignments, flags) ;
  266. break;
  267. default:
  268. abort();
  269. }
  270. }
  271. /* ! VL_VLAD_INSTANTIATING */
  272. #endif
  273. #undef SFX
  274. #undef TYPE
  275. #undef FLT
  276. #undef VL_VLAD_INSTANTIATING