00001
00006 #include <vl/gmm.h>
00007 #include <vl/host.h>
00008 #include <vl/kmeans.h>
00009 #include <vl/fisher.h>
00010 #include <vl/vlad.h>
00011 #include <stdio.h>
00012
00013
00014
00015
00016
00017 #define TYPE float
00018 #define VL_F_TYPE VL_TYPE_FLOAT
00019
00020 void saveResults(const char * dataFileData, const char * dataFileResults, VlGMM * gmm, void * data, vl_size numData);
00021
00022 int main(int argc VL_UNUSED, char ** argv VL_UNUSED)
00023 {
00024 VlKMeans * kmeans = 0;
00025 VlRand rand ;
00026 vl_size dataIdx, d, cIdx;
00027 VlGMM * gmm;
00028
00029 double sigmaLowerBound = 0.000001;
00030
00031 vl_size numData = 1000;
00032 vl_size dimension = 3;
00033 vl_size numClusters = 20;
00034 vl_size maxiter = 5;
00035 vl_size maxrep = 1;
00036
00037 vl_size maxiterKM = 5;
00038 vl_size ntrees = 3;
00039 vl_size maxComp = 20;
00040
00041 typedef enum _init {
00042 KMeans,
00043 Rand,
00044 Custom
00045 } Init ;
00046
00047 vl_bool computeFisher = VL_TRUE;
00048 vl_bool computeVlad = VL_FALSE;
00049
00050 Init init = KMeans;
00051
00052
00053
00054
00055 TYPE * data = vl_malloc(sizeof(TYPE)*numData*dimension);
00056 TYPE * enc = vl_malloc(sizeof(TYPE)*2*dimension*numClusters);
00057 vl_uint32 * assign;
00058
00059 vl_set_num_threads(0) ;
00060
00061 vl_rand_init (&rand) ;
00062 vl_rand_seed (&rand, 49000) ;
00063
00064 for(dataIdx = 0; dataIdx < numData; dataIdx++) {
00065 for(d = 0; d < dimension; d++) {
00066 data[dataIdx*dimension+d] = (TYPE)vl_rand_real3(&rand);
00067
00068 }
00069
00070 }
00071
00072 gmm = vl_gmm_new (VL_F_TYPE, dimension, numClusters) ;
00073
00074 switch(init) {
00075 case KMeans:
00076 kmeans = vl_kmeans_new(VL_F_TYPE,VlDistanceL2);
00077 vl_kmeans_set_verbosity (kmeans,1);
00078 vl_kmeans_set_max_num_iterations (kmeans, maxiterKM) ;
00079 vl_kmeans_set_max_num_comparisons (kmeans, maxComp) ;
00080 vl_kmeans_set_num_trees (kmeans, ntrees);
00081 vl_kmeans_set_algorithm (kmeans, VlKMeansANN);
00082 vl_kmeans_set_initialization(kmeans, VlKMeansRandomSelection);
00083 vl_gmm_set_initialization (gmm,VlGMMKMeans);
00084 vl_gmm_set_kmeans_init_object(gmm,kmeans);
00085 break;
00086
00087 case Rand:
00088 vl_gmm_set_initialization (gmm,VlGMMRand);
00089 break;
00090
00091 case Custom: {
00092 TYPE * initSigmas;
00093 TYPE * initMeans;
00094 TYPE * initWeights;
00095
00096 initSigmas = vl_malloc(sizeof(TYPE) * numClusters * dimension);
00097 initWeights = vl_malloc(sizeof(TYPE) * numClusters);
00098 initMeans = vl_malloc(sizeof(TYPE) * numClusters * dimension);
00099
00100 vl_gmm_set_initialization (gmm,VlGMMCustom);
00101
00102 for(cIdx = 0; cIdx < numClusters; cIdx++) {
00103 for(d = 0; d < dimension; d++) {
00104 initMeans[cIdx*dimension+d] = (TYPE)vl_rand_real3(&rand);
00105 initSigmas[cIdx*dimension+d] = (TYPE)vl_rand_real3(&rand);
00106 }
00107 initWeights[cIdx] = (TYPE)vl_rand_real3(&rand);
00108 }
00109
00110 vl_gmm_set_priors(gmm,initWeights);
00111 vl_gmm_set_covariances(gmm,initSigmas);
00112 vl_gmm_set_means(gmm,initMeans);
00113
00114 break;
00115 }
00116
00117 default:
00118 abort();
00119 }
00120
00121 vl_gmm_set_max_num_iterations (gmm, maxiter) ;
00122 vl_gmm_set_num_repetitions(gmm, maxrep);
00123 vl_gmm_set_verbosity(gmm,1);
00124 vl_gmm_set_covariance_lower_bound (gmm,sigmaLowerBound);
00125
00126
00127
00128
00129 vl_gmm_cluster (gmm, data, numData);
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174 vl_free(data);
00175 numData = 2000;
00176 data = vl_malloc(numData*dimension*sizeof(TYPE));
00177 for(dataIdx = 0; dataIdx < numData; dataIdx++) {
00178 for(d = 0; d < dimension; d++) {
00179 data[dataIdx*dimension+d] = (TYPE)vl_rand_real3(&rand);
00180 }
00181 }
00182
00183 if(computeFisher) {
00184 vl_fisher_encode
00185 (enc, VL_F_TYPE,
00186 vl_gmm_get_means(gmm), dimension, numClusters,
00187 vl_gmm_get_covariances(gmm),
00188 vl_gmm_get_priors(gmm),
00189 data, numData,
00190 VL_FISHER_FLAG_IMPROVED
00191 ) ;
00192 }
00193
00194 assign = vl_malloc(numData*numClusters*sizeof(vl_uint32));
00195 for(dataIdx = 0; dataIdx < numData; dataIdx++) {
00196 for(cIdx = 0; cIdx < numClusters; cIdx++) {
00197 assign[cIdx*numData+dataIdx] = (vl_uint32)vl_rand_real3(&rand);
00198 }
00199 }
00200
00201 if(computeVlad) {
00202 vl_free(enc);
00203 enc = vl_malloc(sizeof(TYPE)*dimension*numClusters);
00204 vl_vlad_encode
00205 (enc, VL_F_TYPE,
00206 vl_gmm_get_means(gmm), dimension, numClusters,
00207 data, numData,
00208 assign,
00209 0) ;
00210 }
00211
00212 vl_gmm_delete(gmm);
00213 vl_free(data);
00214 if(enc){
00215 vl_free(enc);
00216 }
00217 if(kmeans) {
00218 vl_kmeans_delete(kmeans);
00219 }
00220 return 0 ;
00221 }
00222
00223 void saveResults(const char * dataFileData, const char * dataFileResults, VlGMM * gmm, void * data, vl_size numData)
00224 {
00225 char *mode = "w";
00226 FILE * ofp;
00227
00228 vl_size d, cIdx;
00229 vl_uindex i_d;
00230
00231 vl_size dimension = vl_gmm_get_dimension(gmm) ;
00232 vl_size numClusters = vl_gmm_get_num_clusters(gmm) ;
00233 vl_type dataType = vl_gmm_get_data_type(gmm) ;
00234 double const * sigmas = vl_gmm_get_covariances(gmm) ;
00235 double const * means = vl_gmm_get_means(gmm) ;
00236 double const * weights = vl_gmm_get_priors(gmm) ;
00237 double const * posteriors = vl_gmm_get_posteriors(gmm) ;
00238
00239 ofp = fopen(dataFileData, mode);
00240 for(i_d = 0; i_d < numData; i_d++) {
00241 if(vl_gmm_get_data_type(gmm) == VL_TYPE_DOUBLE) {
00242 for(d = 0; d < vl_gmm_get_dimension(gmm) ; d++) {
00243 fprintf(ofp, "%f ", ((double*)data)[i_d * vl_gmm_get_dimension(gmm) + d]);
00244 }
00245 } else {
00246 for(d = 0; d < vl_gmm_get_dimension(gmm); d++) {
00247 fprintf(ofp, "%f ", ((float*) data)[i_d * vl_gmm_get_dimension(gmm) + d]);
00248 }
00249 }
00250 fprintf(ofp, "\n");
00251 }
00252 fclose (ofp);
00253
00254 ofp = fopen(dataFileResults, mode);
00255 for(cIdx = 0; cIdx < numClusters; cIdx++) {
00256 if(dataType == VL_TYPE_DOUBLE) {
00257 for(d = 0; d < vl_gmm_get_dimension(gmm); d++) {
00258 fprintf(ofp, "%f ", ((double*)means)[cIdx*dimension+d]);
00259 }
00260 for(d = 0; d < dimension; d++) {
00261 fprintf(ofp, "%f ", ((double*)sigmas)[cIdx*dimension+d]);
00262 }
00263 fprintf(ofp, "%f ", ((double*)weights)[cIdx]);
00264 for(i_d = 0; i_d < numData; i_d++) {
00265 fprintf(ofp, "%f ", ((double*)posteriors)[cIdx*numData + i_d]);
00266 }
00267 fprintf(ofp, "\n");
00268 } else {
00269 for(d = 0; d < dimension; d++) {
00270 fprintf(ofp, "%f ", ((float*)means)[cIdx*dimension+d]);
00271 }
00272 for(d = 0; d < dimension; d++) {
00273 fprintf(ofp, "%f ", ((float*)sigmas)[cIdx*dimension+d]);
00274 }
00275 fprintf(ofp, "%f ", ((float*)weights)[cIdx]);
00276 for(i_d = 0; i_d < numData; i_d++) {
00277 fprintf(ofp, "%f ", ((float*)posteriors)[cIdx*numData + i_d]);
00278
00279 }
00280 fprintf(ofp, "\n");
00281 }
00282 }
00283 fclose (ofp);
00284 }