Debian Codegraph @

src/thirdparty/VLFeat/svmdataset.c

raw
1 /** @file svmdataset.c
2 ** @brief SVM Dataset - Definition
3 ** @author Daniele Perrone
4 ** @author Andrea Vedaldi
5 **/
6
7 /*
8 Copyright (C) 2012 Daniele Perrone.
9 Copyright (C) 2013 Andrea Vedaldi.
10 All rights reserved.
11
12 This file is part of the VLFeat library and is made available under
13 the terms of the BSD license (see the COPYING file).
14 */
15
16 /**
17 @file svmdataset.h
18 @tableofcontents
19 @author Daniele Perrone
20 @author Andrea Vedaldi
21
22 The SVM solver object ::VlSvm, supporting SVM learning in VLFeat,
23 uses an abstraction mechanism to work on arbitrary data types.
24 This module provides an helper object, ::VlSvmDataset,
25 that simplify taking advantage of this functionality, supporting for example
26 different data types and the computation of feature maps out of the box.
27
28 <!-- ------------------------------------------------------------- -->
29 @section svmdataset-starting Getting started
30 <!-- ------------------------------------------------------------- -->
31
32 As discussed in @ref svm-advanced, most linear SVM solvers,
33 such as the ones implemented in VLFeat in @ref svm, require only two
34 operations to be defined on the data:
35
36 - *Inner product* between a data point $\bx$ and the model vector $\bw$.
37 This is implemented by a function of type ::VlSvmInnerProductFunction.
38 - *Accumulation* of a dataobint $\bx$ to the model vector $\bw$:
39 $\bw \leftarrow \bw + \alpha \bx$. This is implemented
40 by a function of the type ::VlSvmAccumulateFunction .
41
42 The SVM solver needs to know nothing about the data once these two
43 operations are defined. These functions can do any number of things,
44 such as supporting different formats for the data (dense or sparse,
45 float or double), computing feature maps, or expanding compressed
46 representations such as Product Quantization.
47
48 VLFeat provides the helper object ::VlSvmDataset to support some
49 of these functionalities out of the box (it is important to remark
50 that its use with the SVM solver ::VlSvm is entirely optional).
51
52 Presently, ::VlSvmDataset supports:
53
54 - @c float and @c double dense arrays.
55 - The on-the-fly application of the homogeneous kernel map to implement
56 additive non-linear kernels (see @ref homkermap).
57
58 For example, to learn a linear SVM on SINGLE data:
59
60 @code
61 int main()
62 {
63 vl_size const numData = 4 ;
64 vl_size const dimension = 2 ;
65 single x [dimension * numData] = {
66 0.0, -0.5,
67 0.6, -0.3,
68 0.0, 0.5,
69 0.6, 0.0} ;
70 double y [numData] = {1, 1, -1, 1} ;
71 double lambda = 0.01;
72 double * const model ;
73 double bias ;
74
75 VlSvmDataset * dataset = vl_svmdataset_new (VL_TYPE_SINGLE, x, dimension, numData) ;
76 VlSvm * svm = vl_svm_new_with_dataset (VlSvmSolverSgd, dataset, y, lambda) ;
77
78 vl_svm_train(svm) ;
79
80 model = vl_svm_get_model(svm) ;
81 bias = vl_svm_get_bias(svm) ;
82
83 printf("model w = [ %f , %f ] , bias b = %f \n",
84 model[0],
85 model[1],
86 bias);
87
88 vl_svm_delete(svm) ;
89 vl_svmdataset_delete(dataset) ;
90 return 0;
91 }
92 @endcode
93
94 **/
95
96 /* ---------------------------------------------------------------- */
97 #ifndef VL_SVMDATASET_INSTANTIATING
98 /* ---------------------------------------------------------------- */
99
100 #include "svmdataset.h"
101 #include <string.h>
102 #include <math.h>
103
104 struct VlSvmDataset_ {
105 vl_type dataType ; /**< Data type. */
106 void * data ; /**< Pointer to data. */
107 vl_size numData ; /**< Number of wrapped data. */
108 vl_size dimension ; /**< Data point dimension. */
109 VlHomogeneousKernelMap * hom ; /**< Homogeneous kernel map (optional). */
110 void * homBuffer ; /**< Homogeneous kernel map buffer. */
111 vl_size homDimension ; /**< Homogeneous kernel map dimension. */
112 } ;
113
114 /* templetized parts of the implementation */
115 #define FLT VL_TYPE_FLOAT
116 #define VL_SVMDATASET_INSTANTIATING
117 #include "svmdataset.c"
118
119 #define FLT VL_TYPE_DOUBLE
120 #define VL_SVMDATASET_INSTANTIATING
121 #include "svmdataset.c"
122
123 /** @brief Create a new object wrapping a dataset.
124 ** @param dataType of data (@c float and @c double supported).
125 ** @param data pointer to the data.
126 ** @param dimension the dimension of a data vector.
127 ** @param numData number of wrapped data vectors.
128 ** @return new object.
129 **
130 ** The function allocates and returns a new SVM dataset object
131 ** wrapping the data pointed by @a data. Note that no copy is made
132 ** of data, so the caller should keep the data allocated as the object exists.
133 **
134 ** @sa ::vl_svmdataset_delete
135 **/
136
137 VlSvmDataset*
138 vl_svmdataset_new (vl_type dataType, void *data, vl_size dimension, vl_size numData)
139 {
140 VlSvmDataset * self ;
141 assert(dataType == VL_TYPE_DOUBLE || dataType == VL_TYPE_FLOAT) ;
142 assert(data) ;
143
144 self = vl_calloc(1, sizeof(VlSvmDataset)) ;
145 if (self == NULL) return NULL ;
146
147 self->dataType = dataType ;
148 self->data = data ;
149 self->dimension = dimension ;
150 self->numData = numData ;
151 self->hom = NULL ;
152 self->homBuffer = NULL ;
153 return self ;
154 }
155
156 /** @brief Delete the object.
157 ** @param self object to delete.
158 **
159 ** The function frees the resources allocated by
160 ** ::vl_svmdataset_new(). Notice that the wrapped data will *not*
161 ** be freed as it is not owned by the object.
162 **/
163
164 void vl_svmdataset_delete (VlSvmDataset *self)
165 {
166 if (self->homBuffer) {
167 vl_free(self->homBuffer) ;
168 self->homBuffer = 0 ;
169 }
170 vl_free (self) ;
171 }
172
173 /** @brief Get the wrapped data.
174 ** @param self object.
175 ** @return a pointer to the wrapped data.
176 **/
177
178 void*
179 vl_svmdataset_get_data (VlSvmDataset const *self)
180 {
181 return self->data ;
182 }
183
184 /** @brief Get the number of wrapped data elements.
185 ** @param self object.
186 ** @return number of wrapped data elements.
187 **/
188
189 vl_size
190 vl_svmdataset_get_num_data (VlSvmDataset const *self)
191 {
192 return self->numData ;
193 }
194
195 /** @brief Get the dimension of the wrapped data.
196 ** @param self object.
197 ** @return dimension of the wrapped data.
198 **/
199
200 vl_size
201 vl_svmdataset_get_dimension (VlSvmDataset const *self)
202 {
203 if (self->hom) {
204 return self->dimension * vl_homogeneouskernelmap_get_dimension(self->hom) ;
205 }
206 return self->dimension ;
207 }
208
209 /** @brief Get the homogeneous kernel map object.
210 ** @param self object.
211 ** @return homogenoeus kernel map object (or @c NULL if any).
212 **/
213
214 VlHomogeneousKernelMap *
215 vl_svmdataset_get_homogeneous_kernel_map (VlSvmDataset const *self)
216 {
217 assert(self) ;
218 return self->hom ;
219 }
220
221 /** @brief Set the homogeneous kernel map object.
222 ** @param self object.
223 ** @param hom homogeneous kernel map object to use.
224 **
225 ** After changing the kernel map, the inner product and accumulator
226 ** function should be queried again (::vl_svmdataset_get_inner_product_function
227 ** adn ::vl_svmdataset_get_accumulate_function).
228 **
229 ** Set this to @c NULL to avoid using a kernel map.
230 **
231 ** Note that this does *not* transfer the ownership of the object
232 ** to the function. Furthermore, ::VlSvmDataset holds to the
233 ** object until it is destroyed or the object is replaced or removed
234 ** by calling this function again.
235 **/
236
237 void
238 vl_svmdataset_set_homogeneous_kernel_map (VlSvmDataset * self,
239 VlHomogeneousKernelMap * hom)
240 {
241 assert(self) ;
242 self->hom = hom ;
243 self->homDimension = 0 ;
244 if (self->homBuffer) {
245 vl_free (self->homBuffer) ;
246 self->homBuffer = 0 ;
247 }
248 if (self->hom) {
249 self->homDimension = vl_homogeneouskernelmap_get_dimension(self->hom) ;
250 self->homBuffer = vl_calloc(self->homDimension, vl_get_type_size(self->dataType)) ;
251 }
252 }
253
254 /** @brief Get the accumulate function
255 ** @param self object.
256 ** @return a pointer to the accumulate function to use with this data.
257 **/
258
259 VlSvmAccumulateFunction
260 vl_svmdataset_get_accumulate_function(VlSvmDataset const *self)
261 {
262 if (self->hom == NULL) {
263 switch (self->dataType) {
264 case VL_TYPE_FLOAT:
265 return (VlSvmAccumulateFunction) vl_svmdataset_accumulate_f ;
266 break ;
267 case VL_TYPE_DOUBLE:
268 return (VlSvmAccumulateFunction) vl_svmdataset_accumulate_d ;
269 break ;
270 }
271 } else {
272 switch (self->dataType) {
273 case VL_TYPE_FLOAT:
274 return (VlSvmAccumulateFunction) vl_svmdataset_accumulate_hom_f ;
275 break ;
276 case VL_TYPE_DOUBLE:
277 return (VlSvmAccumulateFunction) vl_svmdataset_accumulate_hom_d ;
278 break ;
279 }
280 }
281 assert(0) ;
282 return NULL ;
283 }
284
285 /** @brief Get the inner product function.
286 ** @param self object.
287 ** @return a pointer to the inner product function to use with this data.
288 **/
289
290 VlSvmInnerProductFunction
291 vl_svmdataset_get_inner_product_function (VlSvmDataset const *self)
292 {
293 if (self->hom == NULL) {
294 switch (self->dataType) {
295 case VL_TYPE_FLOAT:
296 return (VlSvmInnerProductFunction) _vl_svmdataset_inner_product_f ;
297 break ;
298 case VL_TYPE_DOUBLE:
299 return (VlSvmInnerProductFunction) _vl_svmdataset_inner_product_d ;
300 break ;
301 default:
302 assert(0) ;
303 }
304 } else {
305 switch (self->dataType) {
306 case VL_TYPE_FLOAT:
307 return (VlSvmInnerProductFunction) _vl_svmdataset_inner_product_hom_f ;
308 break ;
309 case VL_TYPE_DOUBLE:
310 return (VlSvmInnerProductFunction) _vl_svmdataset_inner_product_hom_d ;
311 break ;
312 default:
313 assert(0) ;
314 }
315 }
316
317 return NULL;
318 }
319
320 /* VL_SVMDATASET_INSTANTIATING */
321 #endif
322
323 /* ---------------------------------------------------------------- */
324 #ifdef VL_SVMDATASET_INSTANTIATING
325 /* ---------------------------------------------------------------- */
326
327 #include "float.h"
328
329 double
330 VL_XCAT(_vl_svmdataset_inner_product_,SFX) (VlSvmDataset const *self,
331 vl_uindex element,
332 double const *model)
333 {
334 double product = 0 ;
335 T* data = ((T*)self->data) + self->dimension * element ;
336 T* end = data + self->dimension ;
337 while (data != end) {
338 product += (*data++) * (*model++) ;
339 }
340 return product ;
341 }
342
343 void
344 VL_XCAT(vl_svmdataset_accumulate_,SFX)(VlSvmDataset const *self,
345 vl_uindex element,
346 double *model,
347 const double multiplier)
348 {
349 T* data = ((T*)self->data) + self->dimension * element ;
350 T* end = data + self->dimension ;
351 while (data != end) {
352 *model += (*data++) * multiplier ;
353 model++ ;
354 }
355 }
356
357 double
358 VL_XCAT(_vl_svmdataset_inner_product_hom_,SFX) (VlSvmDataset const *self,
359 vl_uindex element,
360 double const *model)
361 {
362 double product = 0 ;
363 T* data = ((T*)self->data) + self->dimension * element ;
364 T* end = data + self->dimension ;
365 T* bufEnd = ((T*)self->homBuffer)+ self->homDimension ;
366 while (data != end) {
367 /* TODO: zeros in data could be optimized by skipping over them */
368 T* buf = self->homBuffer ;
369 VL_XCAT(vl_homogeneouskernelmap_evaluate_,SFX)(self->hom,
370 self->homBuffer,
371 1,
372 (*data++)) ;
373 while (buf != bufEnd) {
374 product += (*buf++) * (*model++) ;
375 }
376 }
377 return product ;
378 }
379
380 void
381 VL_XCAT(vl_svmdataset_accumulate_hom_,SFX)(VlSvmDataset const *self,
382 vl_uindex element,
383 double *model,
384 const double multiplier)
385 {
386 T* data = ((T*)self->data) + self->dimension * element ;
387 T* end = data + self->dimension ;
388 T* bufEnd = ((T*)self->homBuffer)+ self->homDimension ;
389 while (data != end) {
390 /* TODO: zeros in data could be optimized by skipping over them */
391 T* buf = self->homBuffer ;
392 VL_XCAT(vl_homogeneouskernelmap_evaluate_,SFX)(self->hom,
393 self->homBuffer,
394 1,
395 (*data++)) ;
396 while (buf != bufEnd) {
397 *model += (*buf++) * multiplier ;
398 model++ ;
399 }
400 }
401 }
402
403 #undef FLT
404 #undef VL_SVMDATASET_INSTANTIATING
405
406 /* VL_SVMDATASET_INSTANTIATING */
407 #endif