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