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.

587 lines
19 KiB

8 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /** @file fisher.c
  2. ** @brief Fisher - Declaration
  3. ** @author David Novotny
  4. **/
  5. /*
  6. Copyright (C) 2013 David Novotny and Andrea Vedaldi.
  7. All rights reserved.
  8. This file is part of the VLFeat library and is made available under
  9. the terms of the BSD license (see the COPYING file).
  10. */
  11. /**
  12. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  13. @page fisher Fisher Vector encoding (FV)
  14. @author David Novotny
  15. @author Andrea Vedaldi
  16. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  17. @ref fisher.h implements the Fisher Vectors (FV) image representation
  18. @cite{perronnin06fisher} @cite{perronnin10improving}. A FV is a
  19. statistics capturing the distribution of a set of vectors, usually a
  20. set of local image descriptors.
  21. @ref fisher-starting demonstrates how to use the C API to compute the
  22. FV representation of an image. For further details refer to:
  23. - @subpage fisher-fundamentals - Fisher Vector definition.
  24. - @subpage fisher-derivation - Deriving the Fisher Vectors as a Fisher Kernel.
  25. - @subpage fisher-kernel - The Fisher Kernel in general.
  26. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  27. @section fisher-starting Getting started
  28. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  29. The Fisher Vector encoding of a set of features is obtained by using
  30. the function ::vl_fisher_encode. Note that the function requires a
  31. @ref gmm "Gaussian Mixture Model" (GMM) of the encoded feature
  32. distribution. In the following code, the result of the coding process
  33. is stored in the @c enc array and the improved fisher vector
  34. normalization is used.
  35. @code
  36. float * means ;
  37. float * covariances ;
  38. float * priors ;
  39. float * posteriors ;
  40. float * enc;
  41. // create a GMM object and cluster input data to get means, covariances
  42. // and priors of the estimated mixture
  43. gmm = vl_gmm_new (VL_TYPE_FLOAT) ;
  44. vl_gmm_cluster (gmm, data, dimension, numData, numClusters);
  45. // allocate space for the encoding
  46. enc = vl_malloc(sizeof(float) * 2 * dimension * numClusters);
  47. // run fisher encoding
  48. vl_fisher_encode
  49. (enc, VL_F_TYPE,
  50. vl_gmm_get_means(gmm), dimension, numClusters,
  51. vl_gmm_get_covariances(gmm),
  52. vl_gmm_get_priors(gmm),
  53. dataToEncode, numDataToEncode,
  54. VL_FISHER_FLAG_IMPROVED
  55. ) ;
  56. @endcode
  57. The performance of the standard Fisher Vector can be significantly
  58. improved @cite{perronnin10improving} by using appropriate @ref
  59. fisher-normalization normalizations. These are controlled by the @c
  60. flag parameter of ::vl_fisher_encode.
  61. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  62. @page fisher-fundamentals Fisher vector fundamentals
  63. @tableofcontents
  64. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  65. This page describes the *Fisher Vector* (FV) of
  66. @cite{perronnin06fisher} @cite{perronnin10improving}. See @ref fisher
  67. for an overview of the C API and @ref fisher-kernel for its relation
  68. to the more general notion of Fisher kernel.
  69. The FV is an image representation obtained by pooling local image
  70. features. It is frequently used as a global image descriptor in visual
  71. classification.
  72. While the FV can be @ref fisher-kernel "derived" as a special,
  73. approximate, and improved case of the general Fisher Kernel framework,
  74. it is easy to describe directly. Let $I = (\bx_1,\dots,\bx_N)$ be a
  75. set of $D$ dimensional feature vectors (e.g. SIFT descriptors)
  76. extracted from an image. Let
  77. $\Theta=(\mu_k,\Sigma_k,\pi_k:k=1,\dots,K)$ be the parameters of a
  78. @ref gmm "Gaussian Mixture Model" fitting the distribution of
  79. descriptors. The GMM associates each vector $\bx_i$ to a mode $k$ in
  80. the mixture with a strength given by the posterior probability:
  81. \[
  82. q_{ik} =
  83. \frac
  84. {\exp\left[-\frac{1}{2}(\bx_i - \mu_k)^T \Sigma_k^{-1} (\bx_i - \mu_k)\right]}
  85. {\sum_{t=1}^K \exp\left[-\frac{1}{2}(\bx_i - \mu_t)^T \Sigma_k^{-1} (\bx_i - \mu_t)\right]}.
  86. \]
  87. For each mode $k$, consider the mean and covariance deviation vectors
  88. @f{align*}
  89. u_{jk} &=
  90. {1 \over {N \sqrt{\pi_k}}}
  91. \sum_{i=1}^{N}
  92. q_{ik} \frac{x_{ji} - \mu_{jk}}{\sigma_{jk}},
  93. \\
  94. v_{jk} &=
  95. {1 \over {N \sqrt{2 \pi_k}}}
  96. \sum_{i=1}^{N}
  97. q_{ik} \left[ \left(\frac{x_{ji} - \mu_{jk}}{\sigma_{jk}}\right)^2 - 1 \right].
  98. @f}
  99. where $j=1,2,\dots,D$ spans the vector dimensions. The FV of image $I$
  100. is the stacking of the vectors $\bu_k$ and then of the vectors
  101. $\bv_k$ for each of the $K$ modes in the Gaussian mixtures:
  102. \[
  103. \Phi(I) = \begin{bmatrix} \vdots \\ \bu_k \\ \vdots \\ \bv_k \\ \vdots \end{bmatrix}.
  104. \]
  105. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  106. @section fisher-normalization Normalization and improved Fisher vectors
  107. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  108. The *improved* Fisher Vector @cite{perronnin10improving} (IFV) improves the
  109. classification performance of the representation by using to ideas:
  110. 1. *Non-linear additive kernel.* The Hellinger's kernel (or
  111. Bhattacharya coefficient) can be used instead of the linear one at
  112. no cost by signed squared rooting. This is obtained by applying the
  113. function $|z| \sign z$ to each dimension of the vector $\Phi(I)$.
  114. Other @ref homkermap "additive kernels" can also be used at an
  115. increased space or time cost.
  116. 2. *Normalization.* Before using the representation in a linear model
  117. (e.g. a @ref svm "support vector machine"), the vector $\Phi(I)$ is
  118. further normalized by the $l^2$ norm (note that the standard Fisher
  119. vector is normalized by the number of encoded feature vectors).
  120. After square-rooting and normalization, the IFV is often used in a
  121. linear classifier such as an @ref svm "SVM".
  122. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  123. @section fisher-fast Faster computations
  124. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  125. In practice, several data to cluster assignments $q_{ik}$ are likely
  126. to be very small or even negligible. The *fast* version of the FV sets
  127. to zero all but the largest assignment for each input feature $\bx_i$.
  128. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  129. @page fisher-derivation Fisher vector derivation
  130. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  131. The FV of @cite{perronnin06fisher} is a special case of the @ref
  132. fisher-kernel "Fisher kernel" construction. It is designed to encode
  133. local image features in a format that is suitable for learning and
  134. comparison with simple metrics such as the Euclidean. In this
  135. construction, an image is modeled as a collection of $D$-dimensional
  136. feature vectors $I=(\bx_1,\dots,\bx_n)$ generated by a GMM with $K$
  137. components $\Theta=(\mu_k,\Sigma_k,\pi_k:k=1,\dots,K)$. The covariance
  138. matrices are assumed to be diagonal, i.e. $\Sigma_k = \diag
  139. \bsigma_k^2$, $\bsigma_k \in \real^D_+$.
  140. The generative model of *one* feature vector $\bx$ is given by the GMM
  141. density function:
  142. \[
  143. p(\bx|\Theta) =
  144. \sum_{k=1}^K \pi_k p(\bx|\Theta_k),
  145. \quad
  146. p(\bx|\Theta_k)
  147. =
  148. \frac{1}{(2\pi)^\frac{D}{2} (\det \Sigma_k)^{\frac{1}{2}}}
  149. \exp
  150. \left[
  151. -\frac{1}{2}
  152. (\bx - \mu_k)^\top \Sigma_k^{-1} (\bx - \mu_k)
  153. \right]
  154. \]
  155. where $\Theta_k = (\mu_k,\Sigma_k)$. The Fisher Vector requires
  156. computing the derivative of the log-likelihood function with respect
  157. to the various model parameters. Consider in particular the parameters
  158. $\Theta_k$ of a mode. Due to the exponent in the Gaussian density
  159. function, the derivative can be written as
  160. \[
  161. \nabla_{\Theta_k} p(\bx|\Theta_k) =
  162. p(\bx|\Theta_k)
  163. g(\bx|\Theta_k)
  164. \]
  165. for a simple vector function $g$. The derivative of the log-likelihood
  166. function is then
  167. \[
  168. \nabla_{\Theta_k} \log p(\bx|\Theta)
  169. =
  170. \frac{\pi_k p(\bx|\Theta_k)}{\sum_{t=1}^K \pi_k p(\bx|\Theta_k)}
  171. g(\bx|\Theta_k)
  172. =
  173. q_k(\bx) g(\bx|\Theta_k)
  174. \]
  175. where $q_k(\bx)$ is the soft-assignment of the point $\bx$ to the mode
  176. $k$. We make the approximation that $q_k(\bx)\approx 1$ if $\bx$ is
  177. sampled from mode $k$ and $\approx 0$ otherwise
  178. @cite{perronnin06fisher}. Hence one gets:
  179. \[
  180. E_{\bx \sim p(\bx|\Theta)}
  181. [
  182. \nabla_{\Theta_k} \log p(\bx|\Theta)
  183. \nabla_{\Theta_t} \log p(\bx|\Theta)^\top
  184. ]
  185. \approx
  186. \begin{cases}
  187. \pi_k E_{\bx \sim p(\bx|\Theta_k)} [ g(\bx|\Theta_k) g(\bx|\Theta_k)^\top], & t = k, \\
  188. 0, & t\not=k.
  189. \end{cases}
  190. \]
  191. Thus under this approximation there is no correlation between the
  192. parameters of the various Gaussian modes.
  193. The function $g$ can be further broken down as the stacking of the
  194. derivative w.r.t. the mean and the diagonal covariance.
  195. \[
  196. g(\bx|\Theta_k)
  197. =
  198. \begin{bmatrix}
  199. g(\bx|\mu_k) \\
  200. g(\bx|\bsigma_k)
  201. \end{bmatrix},
  202. \quad
  203. [g(\bx|\mu_k)]_j
  204. =
  205. \frac{x_j - \mu_{jk}}{\sigma_{jk}^2},
  206. \quad
  207. [g(\bx|\bsigma_k^2)]_j
  208. =
  209. \frac{1}{2\sigma_{jk}^2}
  210. \left(
  211. \left(\frac{x_j - \mu_{jk}}{\sigma_{jk}}\right)^2
  212. -
  213. 1
  214. \right)
  215. \]
  216. Thus the covariance of the model (Fisher information) is diagonal and
  217. the diagonal entries are given by
  218. \[
  219. H_{\mu_{jk}} = \pi_k E[g(\bx|\mu_{jk})g(\bx|\mu_{jk})]
  220. = \frac{\pi_k}{\sigma_{jk}^2},
  221. \quad
  222. H_{\sigma_{jk}^2} = \frac{\pi_k}{2 \sigma_{jk}^4}.
  223. \]
  224. where in the calculation it was used the fact that the fourth moment
  225. of the standard Gaussian distribution is 3. Multiplying the inverse
  226. square root of the matrix $H$ by the derivative of the log-likelihood
  227. function results in the Fisher vector encoding of one image feature
  228. $\bx$:
  229. \[
  230. \Phi_{\mu_{jk}}(\bx) = H_{\mu_{jk}}^{-\frac{1}{2}} q_k(\bx) g(\bx|\mu_{jk})
  231. = q_k(\bx) \frac{x_j - \mu_{jk}}{\sqrt{\pi_k}\sigma_{jk}},
  232. \qquad
  233. \Phi_{\sigma^2_{jk}}(\bx) =
  234. \frac{q_k(\bx)}{\sqrt{2 \pi_k}}
  235. \left(
  236. \left(\frac{x_j - \mu_{jk}}{\sigma_{jk}}\right)^2
  237. -
  238. 1
  239. \right)
  240. \]
  241. Assuming that features are sampled i.i.d. from the GMM results in the
  242. formulas given in @ref fisher-fundamentals (note the normalization
  243. factor). Note that:
  244. * The Fisher components relative to the prior probabilities $\pi_k$
  245. have been ignored. This is because they have little effect on the
  246. representation @cite{perronnin10improving}.
  247. * Technically, the derivation of the Fisher Vector for multiple image
  248. features requires the number of features to be the same in both
  249. images. Ultimately, however, the representation can be computed by
  250. using any number of features.
  251. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  252. @page fisher-kernel Fisher kernel
  253. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  254. This page discusses the Fisher Kernels (FK) of
  255. @cite{jaakkola98exploiting} and shows how the FV of
  256. @cite{perronnin06fisher} can be derived from it as a special case. The
  257. FK induces a similarity measures between data points $\bx$ and $\bx'$
  258. from a parametric generative model $p(\bx|\Theta)$ of the data. The
  259. parameter $\Theta$ of the model is selected to fit the a-priori
  260. distribution of the data, and is usually the Maximum Likelihood (MLE)
  261. estimate obtained from a set of training examples. Once the generative
  262. model is learned, each particular datum $\bx$ is represented by
  263. looking at how it affects the MLE parameter estimate. This effect is
  264. measured by computing the gradient of the log-likelihood term
  265. corresponding to $\bx$:
  266. \[
  267. \hat\Phi(\bx) = \nabla_\Theta \log p(\bx|\Theta)
  268. \]
  269. The vectors $\hat\Phi(\bx)$ should be appropriately scaled before they
  270. can be meaningfully compared. This is obtained by *whitening* the data
  271. by multiplying the vectors by the inverse of the square root of their
  272. *covariance matrix*. The covariance matrix can be obtained from the
  273. generative model $p(\bx|\Theta)$ itself. Since $\Theta$ is the ML
  274. parameter and $\hat\Phi(\bx)$ is the gradient of the log-likelihood
  275. function, its expected value $E[\hat\Phi(\bx)]$ is zero. Thus, since
  276. the vectors are already centered, their covariance matrix is simply:
  277. \[
  278. H = E_{\bx \sim p(\bx|\Theta)} [\hat\Phi(\bx) \hat\Phi(\bx)^\top]
  279. \]
  280. Note that $H$ is also the *Fisher information matrix* of the
  281. model. The final FV encoding $\Phi(\bx)$ is given by the whitened
  282. gradient of the log-likelihood function, i.e.:
  283. \[
  284. \Phi(\bx) = H^{-\frac{1}{2}} \nabla_\Theta \log p(\bx|\Theta).
  285. \]
  286. Taking the inner product of two such vectors yields the *Fisher
  287. kernel*:
  288. \[
  289. K(\bx,\bx')
  290. = \langle \Phi(\bx),\Phi(\bx') \rangle
  291. = \nabla_\Theta \log p(\bx|\Theta)^\top H^{-1} \nabla_\Theta \log p(\bx'|\Theta).
  292. \]
  293. **/
  294. #include "fisher.h"
  295. #include "gmm.h"
  296. #include "mathop.h"
  297. #include <stdio.h>
  298. #include <stdlib.h>
  299. #include <string.h>
  300. #ifdef VL_FISHER_INSTANTIATING
  301. static vl_size
  302. VL_XCAT(_vl_fisher_encode_, SFX)
  303. (TYPE * enc,
  304. TYPE const * means, vl_size dimension, vl_size numClusters,
  305. TYPE const * covariances,
  306. TYPE const * priors,
  307. TYPE const * data, vl_size numData,
  308. int flags)
  309. {
  310. vl_size dim;
  311. vl_index i_cl, i_d;
  312. vl_size numTerms = 0 ;
  313. TYPE * posteriors ;
  314. TYPE * sqrtInvSigma;
  315. assert(numClusters >= 1) ;
  316. assert(dimension >= 1) ;
  317. posteriors = (TYPE*)vl_malloc(sizeof(TYPE) * numClusters * numData);
  318. sqrtInvSigma = (TYPE*)vl_malloc(sizeof(TYPE) * dimension * numClusters);
  319. memset(enc, 0, sizeof(TYPE) * 2 * dimension * numClusters) ;
  320. for (i_cl = 0 ; i_cl < (signed)numClusters ; ++i_cl) {
  321. for(dim = 0; dim < dimension; dim++) {
  322. sqrtInvSigma[i_cl*dimension + dim] = sqrt(1.0 / covariances[i_cl*dimension + dim]);
  323. }
  324. }
  325. VL_XCAT(vl_get_gmm_data_posteriors_, SFX)(posteriors, numClusters, numData,
  326. priors,
  327. means, dimension,
  328. covariances,
  329. data) ;
  330. /* sparsify posterior assignments with the FAST option */
  331. if (flags & VL_FISHER_FLAG_FAST) {
  332. for(i_d = 0; i_d < (signed)numData; i_d++) {
  333. /* find largest posterior assignment for datum i_d */
  334. vl_index best = 0 ;
  335. TYPE bestValue = posteriors[i_d * numClusters] ;
  336. for (i_cl = 1 ; i_cl < (signed)numClusters; ++ i_cl) {
  337. TYPE p = posteriors[i_cl + i_d * numClusters] ;
  338. if (p > bestValue) {
  339. bestValue = p ;
  340. best = i_cl ;
  341. }
  342. }
  343. /* make all posterior assignments zero but the best one */
  344. for (i_cl = 0 ; i_cl < (signed)numClusters; ++ i_cl) {
  345. posteriors[i_cl + i_d * numClusters] =
  346. (TYPE)(i_cl == best) ;
  347. }
  348. }
  349. }
  350. #if defined(_OPENMP)
  351. #pragma omp parallel for default(shared) private(i_cl, i_d, dim) num_threads(vl_get_max_threads()) reduction(+:numTerms)
  352. #endif
  353. for(i_cl = 0; i_cl < (signed)numClusters; ++ i_cl) {
  354. TYPE uprefix;
  355. TYPE vprefix;
  356. TYPE * uk = enc + i_cl*dimension ;
  357. TYPE * vk = enc + i_cl*dimension + numClusters * dimension ;
  358. /*
  359. If the GMM component is degenerate and has a null prior, then it
  360. must have null posterior as well. Hence it is safe to skip it. In
  361. practice, we skip over it even if the prior is very small; if by
  362. any chance a feature is assigned to such a mode, then its weight
  363. would be very high due to the division by priors[i_cl] below.
  364. */
  365. if (priors[i_cl] < 1e-6) { continue ; }
  366. for(i_d = 0; i_d < (signed)numData; i_d++) {
  367. TYPE p = posteriors[i_cl + i_d * numClusters] ;
  368. if (p < 1e-6) continue ;
  369. numTerms += 1;
  370. for(dim = 0; dim < dimension; dim++) {
  371. TYPE diff = data[i_d*dimension + dim] - means[i_cl*dimension + dim] ;
  372. diff *= sqrtInvSigma[i_cl*dimension + dim] ;
  373. *(uk + dim) += p * diff ;
  374. *(vk + dim) += p * (diff * diff - 1);
  375. }
  376. }
  377. if (numData > 0) {
  378. uprefix = 1/(numData*sqrt(priors[i_cl]));
  379. vprefix = 1/(numData*sqrt(2*priors[i_cl]));
  380. for(dim = 0; dim < dimension; dim++) {
  381. *(uk + dim) = *(uk + dim) * uprefix;
  382. *(vk + dim) = *(vk + dim) * vprefix;
  383. }
  384. }
  385. }
  386. vl_free(posteriors);
  387. vl_free(sqrtInvSigma) ;
  388. if (flags & VL_FISHER_FLAG_SQUARE_ROOT) {
  389. for(dim = 0; dim < 2 * dimension * numClusters ; dim++) {
  390. TYPE z = enc [dim] ;
  391. if (z >= 0) {
  392. enc[dim] = VL_XCAT(vl_sqrt_, SFX)(z) ;
  393. } else {
  394. enc[dim] = - VL_XCAT(vl_sqrt_, SFX)(- z) ;
  395. }
  396. }
  397. }
  398. if (flags & VL_FISHER_FLAG_NORMALIZED) {
  399. TYPE n = 0 ;
  400. for(dim = 0 ; dim < 2 * dimension * numClusters ; dim++) {
  401. TYPE z = enc [dim] ;
  402. n += z * z ;
  403. }
  404. n = VL_XCAT(vl_sqrt_, SFX)(n) ;
  405. n = VL_MAX(n, 1e-12) ;
  406. for(dim = 0 ; dim < 2 * dimension * numClusters ; dim++) {
  407. enc[dim] /= n ;
  408. }
  409. }
  410. return numTerms ;
  411. }
  412. #else
  413. /* not VL_FISHER_INSTANTIATING */
  414. #ifndef __DOXYGEN__
  415. #define FLT VL_TYPE_FLOAT
  416. #define TYPE float
  417. #define SFX f
  418. #define VL_FISHER_INSTANTIATING
  419. #include "fisher.c"
  420. #define FLT VL_TYPE_DOUBLE
  421. #define TYPE double
  422. #define SFX d
  423. #define VL_FISHER_INSTANTIATING
  424. #include "fisher.c"
  425. #endif
  426. /* not VL_FISHER_INSTANTIATING */
  427. #endif
  428. /* ================================================================ */
  429. #ifndef VL_FISHER_INSTANTIATING
  430. /** @brief Fisher vector encoding of a set of vectors.
  431. ** @param dataType the type of the input data (::VL_TYPE_DOUBLE or ::VL_TYPE_FLOAT).
  432. ** @param enc Fisher vector (output).
  433. ** @param means Gaussian mixture means.
  434. ** @param dimension dimension of the data.
  435. ** @param numClusters number of Gaussians mixture components.
  436. ** @param covariances Gaussian mixture diagonal covariances.
  437. ** @param priors Gaussian mixture prior probabilities.
  438. ** @param data vectors to encode.
  439. ** @param numData number of vectors to encode.
  440. ** @param flags options.
  441. ** @return number of averaging operations.
  442. **
  443. ** @a means and @a covariances have @a dimension rows and @a
  444. ** numCluster columns. @a priors is a vector of size @a
  445. ** numCluster. @a data has @a dimension rows and @a numData
  446. ** columns. @a enc is a vecotr of size equal to twice the product of
  447. ** @a dimension and @a numClusters. All these vectors and matrices
  448. ** have the same class, as specified by @a dataType, and must be
  449. ** stored in column-major format.
  450. **
  451. ** @a flag can be used to control several options:
  452. ** ::VL_FISHER_FLAG_SQUARE_ROOT, ::VL_FISHER_FLAG_NORMALIZED,
  453. ** ::VL_FISHER_FLAG_IMPROVED, and ::VL_FISHER_FLAG_FAST.
  454. **
  455. ** The function returns the number of averaging operations actually
  456. ** performed. The upper bound is the number of input features by the
  457. ** number of GMM modes; however, assignments are usually failry
  458. ** sparse, so this number is often much smaller. In particular, with
  459. ** the ::VL_FISHER_FLAG_FAST, is equal to the number of input
  460. ** features. This information can be used for diagnostic purposes.
  461. **
  462. ** @sa @ref fisher
  463. **/
  464. VL_EXPORT vl_size
  465. vl_fisher_encode
  466. (void * enc, vl_type dataType,
  467. void const * means, vl_size dimension, vl_size numClusters,
  468. void const * covariances,
  469. void const * priors,
  470. void const * data, vl_size numData,
  471. int flags
  472. )
  473. {
  474. switch(dataType) {
  475. case VL_TYPE_FLOAT:
  476. return _vl_fisher_encode_f
  477. ((float *) enc,
  478. (float const *) means, dimension, numClusters,
  479. (float const *) covariances,
  480. (float const *) priors,
  481. (float const *) data, numData,
  482. flags);
  483. case VL_TYPE_DOUBLE:
  484. return _vl_fisher_encode_d
  485. ((double *) enc,
  486. (double const *) means, dimension, numClusters,
  487. (double const *) covariances,
  488. (double const *) priors,
  489. (double const *) data, numData,
  490. flags);
  491. break;
  492. default:
  493. abort();
  494. }
  495. return 0;
  496. }
  497. /* not VL_FISHER_INSTANTIATING */
  498. #endif
  499. #undef SFX
  500. #undef TYPE
  501. #undef FLT
  502. #undef VL_FISHER_INSTANTIATING