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.

407 lines
12 KiB

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