VLFeat.org

API docs

  • Home
    • Download and Install
    • API docs
    • Matlab docs
    • About VLFeat
  • Tutorials
    • SIFT
    • MSER
    • IKM
    • HIKM
    • AIB
    • Utils
  • Main Page
  • Related Pages
  • Data Structures
  • Files
  • Examples

ikmeans_lloyd.tc

Go to the documentation of this file.
00001 
00008 /* AUTORIGHTS
00009 Copyright 2007 (c) Andrea Vedaldi and Brian Fulkerson
00010 
00011 This file is part of VLFeat, available in the terms of the GNU
00012 General Public License version 2.
00013 */
00014 
00021 static void 
00022 vl_ikm_init_lloyd (VlIKMFilt *f)
00023 { }
00024 
00032 static int
00033 vl_ikm_train_lloyd (VlIKMFilt* f, vl_uint8 const* data, int N)
00034 {
00035   int iter, i, j, k, 
00036     K = f-> K,
00037     M = f-> M,
00038     err =  0 ;  
00039   vl_ikm_acc *centers = f->centers ;  
00040   vl_uint    *asgn    = vl_malloc (sizeof(vl_uint) * N) ;
00041   vl_uint    *counts  = vl_malloc (sizeof(vl_uint) * N) ;
00042   
00043   for (iter = 0 ; 1 ; ++ iter) {
00044     vl_bool done = 1 ;
00045     
00046     /* ---------------------------------------------------------------
00047      *                                               Calc. assignments
00048      * ------------------------------------------------------------ */
00049     
00050     for (j = 0 ; j < N ; ++j) {
00051       vl_ikm_acc best_dist = 0 ;
00052       vl_uint    best = (vl_uint)-1 ;
00053       
00054       for (k = 0; k < K; ++k) {
00055         vl_ikm_acc dist = 0;
00056         
00057         /* compute distance with this center */
00058         for (i = 0; i < M; ++i) {
00059           vl_ikm_acc delta =
00060             data [j * M + i] - centers [k * M + i] ;
00061           dist += delta * delta ;
00062         }
00063         
00064         /* compare with current best */
00065         if (best == (vl_uint)-1 || dist < best_dist) {
00066           best = k ;
00067           best_dist = dist ;
00068         }
00069       }
00070       if (asgn [j] != best) {
00071         asgn [j] = best ;
00072         done = 0 ;
00073       }
00074     }
00075     
00076     /* stopping condition */
00077     if (done || iter == f->max_niters) break ;
00078     
00079     /* -----------------------------------------------------------------
00080      *                                                     Calc. centers
00081      * -------------------------------------------------------------- */
00082     
00083     /* re-compute centers */
00084     memset (centers, 0, sizeof (vl_int32) * M * K);
00085     memset (counts,  0, sizeof (vl_int32) * K);
00086     for (j = 0; j < N; ++j) {
00087       int this_center = asgn [j] ;      
00088       ++ counts [this_center] ;      
00089       for (i = 0; i < M; ++i)
00090         centers [this_center * M + i] += data[j * M + i];
00091     }
00092     
00093     for (k = 0; k < K; ++k) {
00094       vl_int32 n = counts [k];
00095       if (n > 0xffffff) {
00096         err = 1 ;
00097       }
00098       if (n > 0) {
00099         for (i = 0; i < M; ++i)
00100           centers [k * M + i] /= n;
00101       } else {
00102         /* 
00103            If no data are assigned to the center, it is not changed
00104            with respect to the previous iteration, so we do not do anything. 
00105         */
00106       }
00107     }
00108   }
00109   
00110   vl_free (counts) ;
00111   vl_free (asgn) ;
00112   return err ;
00113 }
00114 
00115 
00124 static void 
00125 vl_ikm_push_lloyd (VlIKMFilt *f, vl_uint *asgn, vl_uint8 const *data, int N)
00126 {
00127   int j ;
00128   for(j=0 ; j < N ; ++j) {
00129     asgn[j] = vl_ikm_push_one (f->centers, data + j * f->M, f->M, f->K);
00130   }
00131 }
00132 
00133 /* 
00134  * Local Variables: *
00135  * mode: C *
00136  * End: *
00137  */
Copyright © 2008 Andrea Vedaldi and Brian Fulkerson