Debian Codegraph @

src/thirdparty/VLFeat/vlad.c

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