static std::vector<double> computeKMeans( KMdata & datapoints, uint64_t const k, uint64_t const runs, bool const debug ) { KMterm const term(runs, 0, 0, 0, 0.10, 0.10, 3, 0.50, 10, 0.95); datapoints.buildKcTree(); KMfilterCenters ctrs(k, datapoints); KMlocalHybrid kmAlg(ctrs, term); ctrs = kmAlg.execute(); std::vector<double> centrevector; for ( uint64_t i = 0; i < k; ++i ) { centrevector.push_back(ctrs[i][0]); if ( debug ) std::cerr << "centre[" << i << "]=" << ctrs[i][0] << std::endl; } std::sort(centrevector.begin(),centrevector.end()); return centrevector; }
void im_compute_bdd_bow(const IMbdd& bdd, std::map <std::string, struct svm_problem>& peopleBOW){ std::string path2bdd(bdd.getFolder()); std::vector<std::string> activities = bdd.getActivities(); std::vector<std::string> people = bdd.getPeople(); int dim = bdd.getDim(); int maxPts = bdd.getMaxPts(); int k = bdd.getK(); for(std::vector<std::string>::iterator person = people.begin(); person != people.end(); ++person){ int currentActivity = 1; struct svm_problem svmPeopleBOW; svmPeopleBOW.l = 0; svmPeopleBOW.x = NULL; svmPeopleBOW.y = NULL; std::cout << "Computing the svmProblem of " << *person << std::endl; for(std::vector<std::string>::iterator activity = activities.begin(); activity != activities.end(); ++activity){ string rep(path2bdd + "/" + *person + "/" + *activity + "/fp"); DIR * repertoire = opendir(rep.c_str()); if (!repertoire){ std::cerr << "Impossible to open the feature points directory!" << std::endl; exit(EXIT_FAILURE); } struct dirent * ent; while ( (ent = readdir(repertoire)) != NULL){ std::string file = ent->d_name; if(file.compare(".") != 0 && file.compare("..") != 0){ std::string path2FPs(rep + "/" + file); KMdata dataPts(dim,maxPts); int nPts = importSTIPs(path2FPs, dim, maxPts, &dataPts); if(nPts != 0){ dataPts.setNPts(nPts); dataPts.buildKcTree(); KMfilterCenters ctrs(k, dataPts); importCenters(path2bdd + "/" + "training.means", dim, k, &ctrs); // Only one BOW struct svm_problem svmBow = computeBOW(currentActivity, dataPts, ctrs); addBOW(svmBow.x[0], svmBow.y[0], svmPeopleBOW); destroy_svm_problem(svmBow); } } } closedir(repertoire); currentActivity++; } peopleBOW.insert(std::make_pair<std::string, struct svm_problem>((*person), svmPeopleBOW)); } }
int CVML_API doKMeans(int dim, int k, int numofSamples, std::vector<std::vector<double>>& _samples,std::vector<std::vector<double>> & center) { typedef double T; center.clear(); center.resize(k); try{ // execution parameters (see KMterm.h and KMlocal.h) KMterm term(100, 0, 0, 0, // run for 100 stages 0.10, 0.10, 3, // other typical parameter values 0.50, 10, 0.95); //int dim = 1; // dimension int nPts = numofSamples; // number of data points KMdata dataPts(dim, nPts); // allocate data storage //KMdata *dataPts = new KMdata(dim, nPts); for ( unsigned int i = 0 ; i < numofSamples ; i++ ){ for(int j=0;i<dim;j++){ dataPts[i][j] = _samples[i][j]; } } dataPts.buildKcTree(); // build filtering structure KMfilterCenters ctrs(k, dataPts); // allocate centers // run the algorithm //KMlocalLloyds kmAlg(ctrs, term); // repeated Lloyd's // KMlocalSwap kmAlg(ctrs, term); // Swap heuristic // KMlocalEZ_Hybrid kmAlg(ctrs, term); // EZ-Hybrid heuristic KMlocalHybrid kmAlg(ctrs, term); // Hybrid heuristic ctrs = kmAlg.execute(); // execute for ( unsigned int i = 0 ; i < k ; i++ ){ for(int j=0;i<dim;j++){ T v = static_cast<T>(ctrs[i][j]); center[i].push_back(v); } } return 1; } catch(...) { //cvml::TRACK("doKMeans error \n"); return 0; } return 1; }
Foam::pointField Foam::searchableBox::coordinates() const { pointField ctrs(6); const pointField pts = treeBoundBox::points(); const faceList& fcs = treeBoundBox::faces; forAll(fcs, i) { ctrs[i] = fcs[i].centre(pts); }
Foam::pointField Foam::oldCyclicPolyPatch::calcFaceCentres ( const UList<face>& faces, const pointField& points ) { pointField ctrs(faces.size()); forAll(faces, facei) { ctrs[facei] = faces[facei].centre(points); }
Foam::pointField Foam::perfectInterface::calcFaceCentres ( const primitivePatch& pp ) { const pointField& points = pp.points(); pointField ctrs(pp.size()); forAll(ctrs, patchFaceI) { ctrs[patchFaceI] = pp[patchFaceI].centre(points); }
/** * \fn void createTrainingMeans(std::string stipFile, int dim, int maxPts, int k, std::string meansFile) * \brief Import HOG and HOF from a file and compute KMeans algorithm to create * the file training.means. * * \param[in] stipFile The file containing the STIPs. * \param[in] dim Points and centers's dimension. * \param[in] maxPts The maximum number of data we can compute * \param[in] k The number of centers * \param[out] meansFile The file in wich we will save the KMeans centers. */ void createTrainingMeans(std::string stipFile, int dim, int maxPts, int k, std::string meansFile ){ KMdata dataPts(dim,maxPts); int nPts = importSTIPs(stipFile, dim, maxPts, &dataPts); dataPts.setNPts(nPts); dataPts.buildKcTree(); KMfilterCenters ctrs(k, dataPts); int ic = 3; kmIvanAlgorithm(ic, dim, dataPts, k, ctrs); exportCenters(meansFile, dim, k, ctrs); }
vector<DataPoint> SpeechKMeans::WeightedKMeans(vector<DataPoint> &points, vector<double> &weights, int k) { KMterm term(100, 0, 0, 0, // run for 100 stages 0.10, 0.10, 3, // other typical parameter values 0.50, 10, 0.95); int dim = problems_.num_features(); // dimension int nPts = points.size(); // number of data points KMdata dataPts(dim, nPts); // allocate data storage for (int p = 0; p < nPts; ++p) { dataPts[p] = new double[dim]; for (int i = 0; i < dim; ++i) { dataPts[p][i] = points[p][i]; } } //kmUniformPts(dataPts.getPts(), nPts, dim); // generate random points dataPts.buildKcTree(); // build filtering structure KMfilterCenters ctrs(k, dataPts); // allocate centers // run the algorithm //KMlocalLloyds kmAlg(ctrs, term); // repeated Lloyd's // KMlocalSwap kmAlg(ctrs, term); // Swap heuristic // KMlocalEZ_Hybrid kmAlg(ctrs, term); // EZ-Hybrid heuristic KMlocalHybrid kmAlg(ctrs, term); // Hybrid heuristic ctrs = kmAlg.execute(); // execute // print number of stages cout << "Number of stages: " << kmAlg.getTotalStages() << "\n"; // print average distortion cout << "Average distortion: " << ctrs.getDist(false)/nPts << "\n"; ctrs.print(); // print final centers cerr << "copying points" << endl; vector<DataPoint> results(k); for (int j = 0; j < k; ++j) { results[j].resize(dim); for (int i = 0; i < dim; ++i) { results[j][i] = ctrs.getCtrPts()[j][i]; } } cerr << "done copying points" << endl; return results; }
int CVML_API doKMeans(int dim, int k, int numofSamples, const double * _samples, double*& center) { typedef double T; if(center) { delete [] center; center=0; } center = new T[k*dim]; memset( center,0,sizeof(T)*k*dim ); try{ // execution parameters (see KMterm.h and KMlocal.h) KMterm term(100, 0, 0, 0, // run for 100 stages 0.10, 0.10, 3, // other typical parameter values 0.50, 10, 0.95); //int dim = 1; // dimension int nPts = numofSamples; // number of data points KMdata dataPts(dim, nPts); // allocate data storage //KMdata *dataPts = new KMdata(dim, nPts); for ( unsigned int i = 0 ; i < numofSamples ; i++ ){ for(int j=0;i<dim;j++){ int idx = i * dim + j; dataPts[i][j] = _samples[idx]; } } dataPts.buildKcTree(); // build filtering structure KMfilterCenters ctrs(k, dataPts); // allocate centers // run the algorithm //KMlocalLloyds kmAlg(ctrs, term); // repeated Lloyd's // KMlocalSwap kmAlg(ctrs, term); // Swap heuristic // KMlocalEZ_Hybrid kmAlg(ctrs, term); // EZ-Hybrid heuristic KMlocalHybrid kmAlg(ctrs, term); // Hybrid heuristic ctrs = kmAlg.execute(); // execute for ( unsigned int i = 0 ; i < k ; i++ ){ for(int j=0;i<dim;j++){ int idx = i * dim + j; center[idx] = static_cast<T>(ctrs[i][j]); } } return 1; } catch(...) { //cvml::TRACK("doKMeans error \n"); return 0; } return 1; }
int im_create_specifics_training_means(IMbdd bdd, const std::vector<std::string>& trainingPeople //std::vector <std::string> rejects ){ std::string path2bdd(bdd.getFolder()); int dim = bdd.getDim(); int maxPts = bdd.getMaxPts(); std::vector <std::string> activities = bdd.getActivities(); int nr_class = activities.size(); int nr_people = trainingPeople.size(); // The total number of centers int k = bdd.getK(); int subK = k/nr_class; if(k%nr_class != 0){ std::cerr << "K is no divisible by the number of activities !!" << std::endl; exit(EXIT_FAILURE); } double ***vDataPts = new double**[nr_class]; int nrFP[nr_class]; // number of feature points for each class int currentActivity = 0; // For each activity for(std::vector<std::string>::iterator activity = activities.begin() ; activity != activities.end() ; ++activity){ nrFP[currentActivity] = 0; // We concatenate all the training people int nrFPpP[nr_people]; // number of feature points per person double*** activityDataPts = new double**[nr_people]; int currentPerson = 0; for(std::vector<std::string>::const_iterator person = trainingPeople.begin() ; person != trainingPeople.end() ; ++person){ nrFPpP[currentPerson] = 0; std::string rep(path2bdd + "/" + *person + "/" + *activity); DIR * repertoire = opendir(rep.c_str()); if (!repertoire){ std::cerr << "Impossible to open the feature points directory!" << std::endl; exit(EXIT_FAILURE); } // Checking that the file concatenate.<activity>.fp exists struct dirent * ent = readdir(repertoire); std::string file(ent->d_name); while (ent && (file.compare("concatenate." + *activity + ".fp")) != 0){ ent = readdir(repertoire); file = ent->d_name; } if(!ent){ std::cerr << "No file concatenate.<activity>.fp" << std::endl; exit(EXIT_FAILURE); } // Importing the feature points std::string path2FP(rep + "/" + file); KMdata kmData(dim,maxPts); nrFPpP[currentPerson] = importSTIPs(path2FP, dim, maxPts, &kmData); if(nrFPpP[currentPerson] != 0){ activityDataPts[currentPerson] = new double*[nrFPpP[currentPerson]]; for(int n = 0 ; n<nrFPpP[currentPerson] ; n++){ activityDataPts[currentPerson][n] = new double [dim]; for(int d = 0 ; d<dim ; d++) activityDataPts[currentPerson][n][d] = kmData[n][d]; } } // else the current person does not participate in this activity nrFP[currentActivity] += nrFPpP[currentPerson]; currentPerson++; } // ++person // Saving people in vDataPts vDataPts[currentActivity] = new double*[nrFP[currentActivity]]; int index=0; for(int p=0 ; p<nr_people ; p++){ for(int fp=0 ; fp<nrFPpP[p] ; fp++){ vDataPts[currentActivity][index] = new double[dim]; for(int d=0 ; d<dim ; d++){ vDataPts[currentActivity][index][d] = activityDataPts[p][fp][d]; } index++; } } // index must be equal to nrFP[currentActivity] - 1 // Deleting activityDataPts for(int p=0 ; p<nr_people ; p++){ for(int fp=0 ; fp < nrFPpP[p] ; fp++) delete[] activityDataPts[p][fp]; delete[] activityDataPts[p]; } delete[] activityDataPts; currentActivity++; } // ++activity // Total number of feature points int ttFP = 0; for(int a=0 ; a<nr_class ; a++){ ttFP += nrFP[a]; } // Memory allocation of the centers double** vCtrs = new double*[k]; for(int i=0 ; i<k ; i++){ vCtrs[i] = new double[dim]; } // Doing the KMeans algorithm for each activities int ic = 3; // the iteration coefficient (Ivan's algorithm) int currCenter = 0; for(int i=0 ; i<nr_class ; i++){ KMdata kmData(dim,nrFP[i]); for(int n=0 ; n<nrFP[i]; n++) for(int d=0 ; d<dim ; d++) kmData[n][d] = vDataPts[i][n][d]; kmData.setNPts(nrFP[i]); kmData.buildKcTree(); KMfilterCenters kmCtrs(subK,kmData); kmIvanAlgorithm(ic, dim, kmData, subK, kmCtrs); for(int n=0 ; n<subK ; n++){ for(int d=0 ; d<dim ; d++){ vCtrs[currCenter][d] = kmCtrs[n][d]; } currCenter++; } } std::cout << "Concatenate KMdata" << std::endl; // Concatenate all the KMdata /* In reality it is not necessary but the objectif KMfilterCenters must be initialized with the KMdata */ KMdata dataPts(dim,ttFP); int nPts = 0; for (int i=0 ; i<nr_class ; i++){ for(int n=0 ; n<nrFP[i]; n++){ for(int d=0 ; d<dim ; d++){ dataPts[nPts][d] = vDataPts[i][n][d]; } nPts++; } } // Releasing vDataPts for(int i=0 ; i<nr_class ; i++){ for(int n=0 ; n<nrFP[i] ; n++) delete [] vDataPts[i][n]; delete [] vDataPts[i]; } delete[] vDataPts; dataPts.buildKcTree(); // Returning the true centers KMfilterCenters ctrs(k,dataPts); for(int n=0 ; n<k ; n++){ for(int d=0 ; d<dim ; d++){ ctrs[n][d] = vCtrs[n][d]; } } // Releasing vCtrs for(int i=0 ; i<k ; i++) delete [] vCtrs[i]; delete[]vCtrs; exportCenters(bdd.getFolder() + "/" + bdd.getKMeansFile(), dim, k, ctrs); return k; }
/** * \fn void predictActivity(std::string, std::string bddName, int maxPts) * \brief Predict the activity done in a video with an existant trained BDD. * * \param[in] The path to the video to predict. * \param[in] bddName The name of the BDD containing videos. * \param[in] maxPts The maximum number of STIPs we can extract. * \return The name of the activity. */ void predictActivity(std::string videoPath, std::string bddName ){ std::string path2bdd("bdd/" + bddName); // Loading parameters IMbdd bdd(bddName,path2bdd); bdd.load_bdd_configuration(path2bdd.c_str(),"imconfig.xml"); int scale_num = bdd.getScaleNum(); std::string descriptor = bdd.getDescriptor(); int dim = bdd.getDim(); int k = bdd.getK(); int maxPts = bdd.getMaxPts(); //double p = getTrainProbability(path2bdd); // Computing feature points KMdata dataPts(dim,maxPts); int nPts = 0; nPts = extract_feature_points(videoPath, scale_num, descriptor, dim, maxPts, dataPts); if(nPts == 0){ std::cerr << "No activity detected !" << std::endl; exit(EXIT_FAILURE); } std::cout << nPts << " vectors extracted..." << std::endl; dataPts.setNPts(nPts); dataPts.buildKcTree(); KMfilterCenters ctrs(k, dataPts); importCenters(bdd.getFolder() + "/" + bdd.getKMeansFile(), dim, k, &ctrs); std::cout << "KMeans centers imported..." << std::endl; activitiesMap *am; int nbActivities = mapActivities(path2bdd,&am); struct svm_problem svmProblem = computeBOW(0, dataPts, ctrs); double means[k], stand_devia[k]; load_gaussian_parameters(bdd, means, stand_devia); // simple, gaussian, both, nothing if(bdd.getNormalization().compare("") != 0) bow_normalization(bdd,svmProblem); std::cout << "Bag of words normalized..." << std::endl; struct svm_model** pSVMModels = new svm_model*[nbActivities]; std::vector<std::string> modelFiles(bdd.getModelFiles()); int i=0; for (std::vector<std::string>::iterator it = modelFiles.begin() ; it != modelFiles.end() ; ++it){ pSVMModels[i]= svm_load_model((*it).c_str()); i++; } std::cout << "SVM models imported..." << std::endl; double probs[nbActivities]; double label = svm_predict_ovr_probs(pSVMModels, svmProblem.x[0], nbActivities, probs, 2); std::cerr<<"Probs: "; for(int j=0 ; j<nbActivities ; j++){ std::cout << setw(2) << setiosflags(ios::fixed) << probs[j]*100<<" "; } destroy_svm_problem(svmProblem); int index = searchMapIndex(label, am, nbActivities); std::cout << "Activity predicted: "; std::cout << am[index].activity << "(" << am[index].label << ")"; std::cout << std::endl; for(int m=0 ; m<nbActivities ; m++){ svm_free_and_destroy_model(&pSVMModels[m]); } }
Foam::pointField Foam::searchableCylinder::coordinates() const { pointField ctrs(1, 0.5*(point1_ + point2_)); return ctrs; }
void KMLProxy::run(Particles *initial_centers) { IMP_INTERNAL_CHECK(is_init_,"The proxy was not initialized"); IMP_LOG(VERBOSE,"KMLProxy::run start \n"); //use the initial centers if provided KMPointArray *kmc=nullptr; if (initial_centers != nullptr) { IMP_INTERNAL_CHECK(kcenters_ == initial_centers->size(), "the number of initial points differs from the number of required" <<" centers\n"); IMP_LOG(VERBOSE,"KMLProxy::run initial centers provided : \n"); kmc = allocate_points(kcenters_,atts_.size()); for (unsigned int i=0;i<kcenters_;i++){ Particle *cen=(*initial_centers)[i]; for(unsigned int j=0;j<atts_.size();j++) { (*(*kmc)[i])[j]=cen->get_value(atts_[j]); } } } IMP_LOG(VERBOSE,"KMLProxy::run load initial guess \n"); //load the initail guess KMFilterCenters ctrs(kcenters_, data_, kmc,damp_factor_); //apply lloyd search IMP_LOG(VERBOSE,"KMLProxy::run load lloyd \n"); lloyd_alg_ = new KMLocalSearchLloyd(&ctrs,&term_); log_header(); IMP_CHECK_CODE(clock_t start = clock()); IMP_LOG(VERBOSE,"KMLProxy::run excute lloyd \n"); lloyd_alg_->execute(); IMP_LOG(VERBOSE,"KMLProxy::run analyse \n"); KMFilterCentersResults best_clusters = lloyd_alg_->get_best(); IMP_CHECK_CODE(Float exec_time = elapsed_time(start)); // print summary IMP_LOG_WRITE(TERSE,log_summary(&best_clusters,exec_time)); IMP_LOG_WRITE(TERSE,best_clusters.show(IMP_STREAM)); IMP_INTERNAL_CHECK(kcenters_ == (unsigned int) best_clusters.get_number_of_centers(), "The final number of centers does not match the requested one"); IMP_INTERNAL_CHECK (dim_ == (unsigned int) best_clusters.get_dim(), "The dimension of the final clusters is wrong"); //TODO clear the centroids list //set the centroids: Particle *p; IMP_LOG(VERBOSE,"KMLProxy::run load best results \n"); for (unsigned int ctr_ind = 0; ctr_ind < kcenters_; ctr_ind++) { KMPoint *kmp = best_clusters[ctr_ind]; //create a new particle p = new Particle(m_); centroids_.push_back(p); for (unsigned int att_ind = 0; att_ind < dim_; att_ind++) { p->add_attribute(atts_[att_ind],(*kmp)[att_ind],false); } } //set the assignment of particles to centers //array of number of all points //TODO - return this IMP_LOG(VERBOSE,"KMLProxy::run get assignments \n"); const Ints *close_center = best_clusters.get_assignments(); IMP_LOG(VERBOSE,"KMLProxy::run get assignments 2\n"); for (int i=0;i<data_->get_number_of_points();i++) { //std::cout<<"ps number i: " << i << " close center : " //<< (*close_center)[i] << std::endl; assignment_[ps_[i]]=(*close_center)[i]; } }
//---------------------------------------------------------------------- // 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; }