1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
5 // By downloading, copying, installing or using the software you agree to this license.
6 // If you do not agree to this license, do not download, install,
7 // copy or use the software.
10 // Intel License Agreement
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
19 // * Redistribution's of source code must retain the above copyright notice,
20 // this list of conditions and the following disclaimer.
22 // * Redistribution's in binary form must reproduce the above copyright notice,
23 // this list of conditions and the following disclaimer in the documentation
24 // and/or other materials provided with the distribution.
26 // * The name of Intel Corporation may not be used to endorse or promote products
27 // derived from this software without specific prior written permission.
29 // This software is provided by the copyright holders and contributors "as is" and
30 // any express or implied warranties, including, but not limited to, the implied
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
32 // In no event shall the Intel Corporation or contributors be liable for any direct,
33 // indirect, incidental, special, exemplary, or consequential damages
34 // (including, but not limited to, procurement of substitute goods or services;
35 // loss of use, data, or profits; or business interruption) however caused
36 // and on any theory of liability, whether in contract, strict liability,
37 // or tort (including negligence or otherwise) arising in any way out of
38 // the use of this software, even if advised of the possibility of such damage.
46 /* Testing parameters */
47 static char test_desc[] = "KMeans clustering";
48 static char* func_name[] =
53 //based on Ara Nefian's implementation
54 float distance(float* vector_1, float *vector_2, int VecSize)
60 for (i = 0; i < VecSize; i++)
62 //printf ("%f, %f\n", vector_1[i], vector_2[i]);
63 dist = dist + (vector_1[i] - vector_2[i])*(vector_1[i] - vector_2[i]);
68 //returns number of made iterations
69 int _real_kmeans( int numClusters, float **sample, int numSamples,
70 int VecSize, int* a_class, double eps, int iter )
82 //printf("* numSamples = %d, numClusters = %d, VecSize = %d\n", numSamples, numClusters, VecSize);
85 dist = new float[numClusters];
86 counter = new int[numClusters];
88 //allocate memory for curr_cluster and prev_cluster
89 curr_cluster = new float*[numClusters];
90 prev_cluster = new float*[numClusters];
91 for (k = 0; k < numClusters; k++){
92 curr_cluster[k] = new float[VecSize];
93 prev_cluster[k] = new float[VecSize];
96 //pick initial cluster centers
97 for (k = 0; k < numClusters; k++)
99 for (n = 0; n < VecSize; n++)
101 curr_cluster[k][n] = sample[k*(numSamples/numClusters)][n];
102 prev_cluster[k][n] = sample[k*(numSamples/numClusters)][n];
109 while ((error > eps) && (NumIter < iter))
112 //printf("NumIter = %d, error = %lf, \n", NumIter, error);
114 //assign samples to clusters
115 for (i = 0; i < numSamples; i++)
117 for (k = 0; k < numClusters; k++)
119 dist[k] = distance(sample[i], curr_cluster[k], VecSize);
123 for (k = 1; k < numClusters; k++)
125 if (dist[k] < minDist)
133 //reset clusters and counters
134 for (k = 0; k < numClusters; k++){
136 for (n = 0; n < VecSize; n++){
137 curr_cluster[k][n] = 0.0;
140 for (i = 0; i < numSamples; i++){
141 for (n = 0; n < VecSize; n++){
142 curr_cluster[a_class[i]][n] = curr_cluster[a_class[i]][n] + sample[i][n];
144 counter[a_class[i]]++;
147 for (k = 0; k < numClusters; k++){
148 for (n = 0; n < VecSize; n++){
149 curr_cluster[k][n] = curr_cluster[k][n]/(float)counter[k];
154 for (k = 0; k < numClusters; k++){
155 for (n = 0; n < VecSize; n++){
156 error = error + (curr_cluster[k][n] - prev_cluster[k][n])*(curr_cluster[k][n] - prev_cluster[k][n]);
159 //error = error/(double)(numClusters*VecSize);
161 //copy curr_clusters to prev_clusters
162 for (k = 0; k < numClusters; k++){
163 for (n =0; n < VecSize; n++){
164 prev_cluster[k][n] = curr_cluster[k][n];
170 //deallocate memory for curr_cluster and prev_cluster
171 for (k = 0; k < numClusters; k++){
172 delete curr_cluster[k];
173 delete prev_cluster[k];
184 static int fmaKMeans(void)
199 static int read_param = 0;
201 /* Initialization global parameters */
205 /* Read test-parameters */
206 trsiRead( &lNumVect, "1000", "Number of vectors" );
207 trsiRead( &lVectSize, "10", "Number of vectors" );
208 trsiRead( &lNumClust, "20", "Number of clusters" );
209 trsiRead( &lMaxNumIter,"100","Maximal number of iterations");
210 trssRead( &flEpsilon, "0.5", "Accuracy" );
213 crit = cvTermCriteria( CV_TERMCRIT_EPS|CV_TERMCRIT_ITER, lMaxNumIter, flEpsilon );
216 vectors = (float**)cvAlloc( lNumVect * sizeof(float*) );
217 for( i = 0; i < lNumVect; i++ )
219 vectors[i] = (float*)cvAlloc( lVectSize * sizeof( float ) );
222 output = (int*)cvAlloc( lNumVect * sizeof(int) );
223 etalon_output = (int*)cvAlloc( lNumVect * sizeof(int) );
226 for( i = 0; i < lNumVect; i++ )
228 ats1flInitRandom( -2000, 2000, vectors[i], lVectSize );
231 /* run etalon kmeans */
232 /* actually it is the simpliest realization of kmeans */
234 int ni = _real_kmeans( lNumClust, vectors, lNumVect, lVectSize, etalon_output, crit.epsilon, crit.max_iter );
236 trsWrite( ATS_CON, "%d iterations done\n", ni );
238 /* Run OpenCV function */
239 #define _KMEANS_TIME 0
244 __int64 tics = atsGetTickCount();
247 cvKMeans( lNumClust, vectors, lNumVect, lVectSize,
251 tics = atsGetTickCount() - tics;
254 //double dbUsecs =ATS_TICS_TO_USECS((double)tics);
255 trsWrite( ATS_CON, "Tics per iteration %d\n", tics/ni );
260 for( j = 0; j < lNumVect; j++ )
262 if ( output[j] != etalon_output[j] )
269 for( i = 0; i < lNumVect; i++ )
271 cvFree( &(vectors[i]) );
275 cvFree(&etalon_output);
277 if( lErrors == 0 ) return trsResult( TRS_OK, "No errors fixed for this text" );
278 else return trsResult( TRS_FAIL, "Detected %d errors", lErrors );
286 /* Register test function */
287 trsReg( func_name[0], test_desc, atsAlgoClass, fmaKMeans );