ikmeans.c
Go to the documentation of this file.
00001 
00007 /*
00008 Copyright (C) 2007-12 Andrea Vedaldi and Brian Fulkerson.
00009 All rights reserved.
00010 
00011 This file is part of the VLFeat library and is made available under
00012 the terms of the BSD license (see the COPYING file).
00013 */
00014 
00079 #include "ikmeans.h"
00080 
00081 #include <stdlib.h>
00082 #include <stdio.h>
00083 #include <string.h> /* memset */
00084 #include "assert.h"
00085 
00086 static void vl_ikm_init_lloyd (VlIKMFilt*) ;
00087 static void vl_ikm_init_elkan (VlIKMFilt*) ;
00088 static int vl_ikm_train_lloyd (VlIKMFilt*, vl_uint8 const*, vl_size) ;
00089 static int vl_ikm_train_elkan (VlIKMFilt*, vl_uint8 const*, vl_size) ;
00090 static void vl_ikm_push_lloyd (VlIKMFilt*, vl_uint32*, vl_uint8 const*, vl_size) ;
00091 static void  vl_ikm_push_elkan  (VlIKMFilt*, vl_uint32*, vl_uint8 const*, vl_size) ;
00092 
00103 VlIKMFilt *
00104 vl_ikm_new (int method)
00105 {
00106   VlIKMFilt *f = vl_calloc (sizeof(VlIKMFilt), 1) ;
00107   f -> method = method ;
00108   f -> max_niters = 200 ;
00109   return f ;
00110 }
00111 
00116 void
00117 vl_ikm_delete (VlIKMFilt* f)
00118 {
00119   if (f) {
00120     if (f->centers) vl_free(f->centers) ;
00121     if (f->inter_dist) vl_free(f->inter_dist) ;
00122     vl_free(f) ;
00123   }
00124 }
00125 
00133 int
00134 vl_ikm_train (VlIKMFilt *f, vl_uint8 const *data, vl_size N)
00135 {
00136   int err ;
00137 
00138   if (f-> verb) {
00139     VL_PRINTF ("ikm: training with %d data\n",  N) ;
00140     VL_PRINTF ("ikm: %d clusters\n",  f -> K) ;
00141   }
00142 
00143   switch (f -> method) {
00144   case VL_IKM_LLOYD : err = vl_ikm_train_lloyd (f, data, N) ; break ;
00145   case VL_IKM_ELKAN : err = vl_ikm_train_elkan (f, data, N) ; break ;
00146   default :
00147     abort() ;
00148   }
00149   return err ;
00150 }
00151 
00163 void
00164 vl_ikm_push (VlIKMFilt *f, vl_uint32 *asgn, vl_uint8 const *data, vl_size N) {
00165   switch (f -> method) {
00166   case VL_IKM_LLOYD : vl_ikm_push_lloyd (f, asgn, data, N) ; break ;
00167   case VL_IKM_ELKAN : vl_ikm_push_elkan (f, asgn, data, N) ; break ;
00168   default :
00169     abort() ;
00170   }
00171 }
00172 
00184 vl_uint32
00185 vl_ikm_push_one (vl_ikmacc_t const *centers,
00186                  vl_uint8 const *data,
00187                  vl_size M, vl_size K)
00188 {
00189   vl_uindex i,k ;
00190 
00191   /* assign data to centers */
00192   vl_uindex best = (vl_uindex) -1 ;
00193   vl_ikmacc_t best_dist = 0 ;
00194 
00195   for(k = 0 ; k < K ; ++k) {
00196     vl_ikmacc_t dist = 0 ;
00197 
00198     /* compute distance with this center */
00199     for(i = 0 ; i < M ; ++i) {
00200       vl_ikmacc_t delta = (vl_ikmacc_t)data[i] - centers[k*M + i] ;
00201       dist += delta * delta ;
00202     }
00203 
00204     /* compare with current best */
00205     if (best == (vl_uindex) -1 || dist < best_dist) {
00206       best = k  ;
00207       best_dist = dist ;
00208     }
00209   }
00210   return (vl_uint32)best;
00211 }
00212 
00213 /* ---------------------------------------------------------------- */
00214 /*                                              Getters and setters */
00215 /* ---------------------------------------------------------------- */
00216 
00222 vl_size
00223 vl_ikm_get_ndims (VlIKMFilt const* f)
00224 {
00225   return f->M ;
00226 }
00227 
00228 
00234 vl_size
00235 vl_ikm_get_K (VlIKMFilt const* f)
00236 {
00237   return f->K ;
00238 }
00239 
00245 int
00246 vl_ikm_get_verbosity (VlIKMFilt const* f)
00247 {
00248   return f->verb ;
00249 }
00250 
00256 vl_size
00257 vl_ikm_get_max_niters (VlIKMFilt const* f)
00258 {
00259   return f->max_niters ;
00260 }
00261 
00267 vl_ikmacc_t const *
00268 vl_ikm_get_centers (VlIKMFilt const* f)
00269 {
00270   return f-> centers ;
00271 }
00272 
00278 void
00279 vl_ikm_set_verbosity (VlIKMFilt *f, int verb)
00280 {
00281   f-> verb = VL_MAX(0,verb) ;
00282 }
00283 
00289 void
00290 vl_ikm_set_max_niters (VlIKMFilt *f, vl_size max_niters)
00291 {
00292   f-> max_niters = max_niters ;
00293 }
00294 
00295 #include "ikmeans_init.tc"
00296 #include "ikmeans_lloyd.tc"
00297 #include "ikmeans_elkan.tc"


libvlfeat
Author(s): Andrea Vedaldi
autogenerated on Thu Jun 6 2019 20:25:51