Example #1
0
//----------------------------------------------------------------------
//  Main program
//----------------------------------------------------------------------
int main(int argc, char **argv)
{
    getArgs(argc, argv);			// read command-line arguments
    term.setAbsMaxTotStage(stages);		// set number of stages

    KMdata dataPts(dim, maxPts);		// allocate data storage
    int nPts = 0;				// actual number of points
    						// generate points
    if (dataIn != NULL) {			// read points from file
	while (nPts < maxPts && readPt(*dataIn, dataPts[nPts])) nPts++;
    }
    else {					// generate points randomly
	nPts = maxPts;
	kmClusGaussPts(dataPts.getPts(), nPts, dim, k);
    }

    cout << "Data Points:\n";			// echo data points
    for (int i = 0; i < nPts; i++)
	printPt(cout, dataPts[i]);

    dataPts.setNPts(nPts);			// set actual number of pts
    dataPts.buildKcTree();			// build filtering structure

    KMfilterCenters ctrs(k, dataPts);		// allocate centers

    						// run each of the algorithms
    cout << "\nExecuting Clustering Algorithm: Lloyd's\n";
    KMlocalLloyds kmLloyds(ctrs, term);		// repeated Lloyd's
    ctrs = kmLloyds.execute();			// execute
    printSummary(kmLloyds, dataPts, ctrs);	// print summary

    cout << "\nExecuting Clustering Algorithm: Swap\n";
    KMlocalSwap kmSwap(ctrs, term);		// Swap heuristic
    ctrs = kmSwap.execute();
    printSummary(kmSwap, dataPts, ctrs);

    cout << "\nExecuting Clustering Algorithm: EZ-Hybrid\n";
    KMlocalEZ_Hybrid kmEZ_Hybrid(ctrs, term);	// EZ-Hybrid heuristic
    ctrs = kmEZ_Hybrid.execute();
    printSummary(kmEZ_Hybrid, dataPts, ctrs);

    cout << "\nExecuting Clustering Algorithm: Hybrid\n";
    KMlocalHybrid kmHybrid(ctrs, term);		// Hybrid heuristic
    ctrs = kmHybrid.execute();
    printSummary(kmHybrid, dataPts, ctrs);

    kmExit(0);
}
Example #2
0
//----------------------------------------------------------------------
//  Main program
//----------------------------------------------------------------------
int main(int argc, char **argv)
{
	getArgs(argc, argv);			// read command-line arguments
	term.setAbsMaxTotStage(stages);		// set number of stages	

	KMdata dataPts(dim, maxPts);		// allocate data storage
	printf("Start Loading!\n");

	int nPts = 0;				// actual number of points
	// generate points
	if( !binary )
	{
		if (dataIn != NULL) 
		{			// read points from file
			while (nPts < maxPts && readPt(*dataIn, dataPts[nPts])) 
				nPts++;
		}
		else 
		{					// generate points randomly
			nPts = maxPts;
			kmClusGaussPts(dataPts.getPts(), nPts, dim, k);
		}
	}
	else
	{
		CFeaFileReader feaReader;
		feaReader.openFile(infname.c_str() );
		nPts = 0;
		char* pBuf = feaReader.getNextSample4VarLen(true);		
		for(; ;)
		{
			if( nPts >= maxPts )
				break;
			if( !pBuf )
				break;

			KMpoint pPtFea = (dataPts[nPts]);
			float *pF = (float *)pBuf;
			for (int d = 0; d < dim; d++) 
			{
				pPtFea[d] = pF[d];
			}
			nPts++;
			pBuf = feaReader.getNextSample4VarLen();
		}
	}
	printf("Load OK!\n");

	dataPts.setNPts(nPts);			// set actual number of pts
	dataPts.buildKcTree();			// build filtering structure

	KMfilterCenters ctrs(k, dataPts);		// allocate centers

	__int64 t1, t2;
	t1 = cvGetTickCount();
	cout << "\nExecuting Clustering Algorithm: Hybrid\n";
	KMlocalHybrid kmHybrid(ctrs, term);		// Hybrid heuristic
	ctrs = kmHybrid.execute();
	t2 = cvGetTickCount() - t1;

	printSummary(kmHybrid, dataPts, ctrs);

	printf( "Elapsed time = %10.2f (ms)\n", t2/(cvGetTickFrequency()*1000) );

	kmExit(0);
}
Example #3
0
//----------------------------------------------------------------------
//  Main program
//----------------------------------------------------------------------
int nao_kmeans()
{
  ifstream importMeans; // permettra d'utiliser les centres enregistrés dans l'étape de training
  int nPts; // actual number of points

  getArgs();			// read command-line arguments

  term.setAbsMaxTotStage(stages);	// set number of stages

  // I- IMPORT STIPS
  nPts = 0;
  dim = STIPS_DIMENSION;
  KMdata dataPts(dim, maxPts);		// allocate data storage
  if (dataIn != NULL){
    laptevParser(*dataIn, dataPts, &nPts);
  }
  else{
      perror("Pas de données à lire !!!");
      return EXIT_FAILURE;
    }
  dataPts.setNPts(nPts);			// set actual number of pts
  dataPts.buildKcTree();			// build filtering structure

  // II- IMPORT TRAINING CENTERS
  KMfilterCenters ctrs(k, dataPts);		// allocate centers
  importMeans.open("/home/nao/data/activities_recognition/training.means", ios::in);
  if(importMeans != NULL){
    trainingMeansParser(importMeans,ctrs);
    importMeans.close();
  }
  else{
    cerr << "Error while importing Means" << endl;
    return 2;
  }

  // III- GET ASSIGNMENTS
  KMctrIdxArray closeCtr = new KMctrIdx[dataPts.getNPts()]; // dataPts = 1 label
  double* sqDist = new double[dataPts.getNPts()];
  ctrs.getAssignments(closeCtr, sqDist);

  // IV- BAG OF WORDS STEP
  // initialisation de l'histogramme
  int* bowHistogram = NULL;
  bowHistogram = new int[k];
  for(int centre = 0; centre<k; centre++)
    bowHistogram[centre]=0;
  // remplissage de l'histogramme
  for(int point = 0; point < nPts ; point++){
    bowHistogram[closeCtr[point]]++;
  }
  delete closeCtr;
  delete[] sqDist;
  // exportation dans le fichier "testing.bow" sous un format pouvant être lu par libSVM
  char* KMeansToBow = NULL;
  KMeansToBow = new char[(256+1)];
   sprintf(KMeansToBow,"%s","/home/nao/data/activities_recognition/stip"); //sprintf(KMeansToBow,"%s.bow",argv[1]);
  ofstream testingBow(KMeansToBow, ios::out | ios::trunc);  // ouverture en écriture avec effacement du fichier ouvert
  if(testingBow){
    testingBow << "0";
    for(int centre = 0; centre<k ; centre++){
      testingBow << " " << centre + 1 << ":" << bowHistogram[centre];
    }
    testingBow << endl;
    testingBow.close();
  }
  else
    cerr << "Impossible d'ouvrir le fichier erreur1" << endl;
  // affichage
  cout << "Bag Of Words histogram" << endl;
  for(int centre = 0; centre<k; centre++){
  cout << "Centre " << centre << ": ";
    for (int i = 0; i<bowHistogram[centre]; i++)
      cout << "*";
    cout << endl;
  }

  delete[] bowHistogram;

  //kmExit(0);
   cout << "[kmeans.cpp]delete[]" << endl;
}