//Reads the corpus file, the output folder, the minimum and the maximum number of clusters and runs the EM algorithm. int main(int argc, char **argv) { const char* info = "printinfo"; if(strcmp(argv[2],info) == 0){ Corpus *c = new Corpus(argv[1]); cout << "Corpus Loaded - Unique Terms = " << c->vocsize << endl; cout << "Total Terms = " << c->terms << endl; cout << "Total Articles = " << c->size() << endl; double avg = (double)c->terms/(double)c->size(); cout << "avg = " << avg << endl; std::tr1::unordered_map<string,int>::iterator it; string outfile = "Vocabulary.txt"; ofstream out; out.open(outfile.c_str()); for(it=c->id2word.begin(); it != c->id2word.end(); it++){ if(c->df[it->second] > 3){ out << it->first << endl; } } out.close(); return 0; } long pi = 3.141592653589793; if(argc < 6) cout << "Usage: ./em Cropus_File Output_Folder min_number_of_clusters max_number_of_clusters max_em_iterations" << endl; int key=15; long double likelihood=0.0,L=0; Corpus *c = new Corpus(argv[1]); int minC = atoi(argv[3]); int maxC = atoi(argv[4]); int MaxIter = atoi(argv[5]); long double likelihoods[maxC+1]; cout << "Corpus Loaded - Unique Terms = " << c->vocsize << endl; //OMPED Iterations in order to accelerate the process #pragma omp parallel for for(unsigned j=minC; j <= maxC; j++){ EM *em = new EM(j,c,MaxIter,string(argv[2])); likelihoods[j] = em->run(); em->~EM(); } string outfile = string(argv[2])+"/likelihoods.txt"; ofstream out; out.open(outfile.c_str()); for(unsigned i = minC; i <= maxC; i++){ double d = (i*(c->vocsize-1))+(i-1); long double penalty = (d/2.0)*log2(c->terms); long double dr = ((d/2.0)*(2*pi)); long double bic = -likelihoods[i] + penalty; cout << i << " " << -likelihoods[i] << " " << penalty << " " << bic << endl; out << i << " " << -likelihoods[i] << " " << penalty << " " << bic << endl; } out.close(); return 0; }
void Classifier::SaveClassifierToFile(string fileName) { if (em_model.isTrained() == false) return; FileStorage fs = FileStorage(fileName, FileStorage::WRITE); em_model.write(fs); fs.release(); }
static void flux_jacobian_eigen_structure(const MODEL::Properties& p, const GV& direction, EM& Rv, EM& Lv, EV& Dv) { Rv.setIdentity(); Lv.setIdentity(); Dv = p.v * direction; }
int train(float* src,char *filename,int N) { EM* myGMM = new EM(5); FileStorage tmp(filename,cv::FileStorage::WRITE); Mat train_32F(N,3,CV_32FC1,src); train_32F.convertTo(train_32F,CV_32F,1/255.); myGMM->train(train_32F); tmp<<"model"; myGMM->write(tmp); tmp.release(); return 0; }
//Learn classifier bool Classifier::Train(Mat sample) { cout << "Training classfication model..."; clock_t startTime_train = clock(); em_model.train(sample); clock_t endTime_train = clock(); double timeSec_train = (endTime_train - startTime_train) / static_cast<double>(CLOCKS_PER_SEC); cout << " train time = " << timeSec_train << "secs\n"; SaveClassifierToFile("em" + to_string(i) + ".xml"); i++; return em_model.isTrained(); }
int main(int argc, char**argv) { GetPot cl(argc, argv); if(cl.search(2,"-h","--help")) {USAGE(); exit(0);} string clustermodel=cl.follow("clustermodel.gz",2,"-c","--clustermodel"); string outfile=cl.follow("vector.gz",2,"-v","--vectorfile"); uint number=cl.follow(0,2,"-i","--idx"); EM em; em.loadModel(clustermodel); vector<double> vec=em.center(number).mean; VectorFeature feat(vec); feat.save(outfile); DBG(10) << "cmdline was: "; printCmdline(argc,argv); }
void Nieto::ExpectationMaximizationOpenCV2Features(const Mat1b &imageI, const Mat1b &imageL, map<string, double> &i_means0, map<string, double> &i_covs0, map<string, double> &i_weights0, map<string, double> &l_means0, map<string, double> &l_covs0, map<string, double> &l_weights0, int maxIters) { double tempoInicio = static_cast<double>(getTickCount()); const int nFeatures = 2; // 2 features => {I, L} const int nClusters = 4; // 4 classes => {pavement, markings, objects, unknown} const bool aplicaResize = true; // samples feature I Mat1b I_imageClone = imageI.clone(); Mat1b I_trainImageClone = imageI.clone(); if (aplicaResize) resize(I_imageClone, I_trainImageClone, Size(160, 35), 0, 0, INTER_NEAREST); Mat1d I_samples = I_trainImageClone.reshape(1, I_trainImageClone.rows * I_trainImageClone.cols); // samples feature L Mat1b L_imageClone = imageL.clone(); Mat1b L_trainImageClone = imageL.clone(); if (aplicaResize) resize(L_imageClone, L_trainImageClone, Size(160, 35), 0, 0, INTER_NEAREST); Mat1d L_samples = L_trainImageClone.reshape(1, L_trainImageClone.rows * L_trainImageClone.cols); // junta as amostras (uma em cada linha) Mat1d samplesArray[] = { I_samples, L_samples }; Mat1d samples; cv::hconcat(samplesArray, 2, samples); // formata o _means0 Mat1d means0 = Mat1d(nClusters, nFeatures, CV_64FC1); means0.at<double>(0, 0) = i_means0["pavement"]; means0.at<double>(1, 0) = i_means0["markings"]; means0.at<double>(2, 0) = i_means0["objects"]; means0.at<double>(3, 0) = 255.0 / 2.0; means0.at<double>(0, 1) = l_means0["pavement"]; means0.at<double>(1, 1) = l_means0["markings"]; means0.at<double>(2, 1) = l_means0["objects"]; means0.at<double>(3, 1) = 255.0 / 2.0; // formata o _covs0 Mat1d covs0_pavement = Mat1d(Size(nFeatures, nFeatures), double(0)); covs0_pavement.at<double>(0, 0) = i_covs0["pavement"]; covs0_pavement.at<double>(1, 1) = l_covs0["pavement"]; Mat1d covs0_markings = Mat1d(Size(nFeatures, nFeatures), double(0));; covs0_markings.at<double>(0, 0) = i_covs0["markings"]; covs0_markings.at<double>(1, 1) = l_covs0["markings"]; Mat1d covs0_objects = Mat1d(Size(nFeatures, nFeatures), double(0));; covs0_objects.at<double>(0, 0) = i_covs0["objects"]; covs0_objects.at<double>(1, 1) = l_covs0["objects"]; Mat1d covs0_unknown = Mat1d(Size(nFeatures, nFeatures), double(0));; covs0_unknown.at<double>(0, 0) = ((255.0 / 2.0) / sqrt(3)) * ((255.0 / 2.0) / sqrt(3)); covs0_unknown.at<double>(1, 1) = ((255.0 / 2.0) / sqrt(3)) * ((255.0 / 2.0) / sqrt(3)); vector<Mat> covs0 = { covs0_pavement, covs0_markings, covs0_objects, covs0_unknown }; // formata o _weights0 Mat1d weights0 = Mat1d(nClusters, 1, CV_64FC1); double total_i = i_weights0["pavement"] + i_weights0["markings"] + i_weights0["objects"] + i_weights0["unknown"]; double total_l = l_weights0["pavement"] + l_weights0["markings"] + l_weights0["objects"] + l_weights0["unknown"]; double total_weights = total_i + total_l; weights0.at<double>(0, 0) = (i_weights0["pavement"] + l_weights0["pavement"]) / total_weights; weights0.at<double>(1, 0) = (i_weights0["markings"] + l_weights0["markings"]) / total_weights; weights0.at<double>(2, 0) = (i_weights0["objects"] + l_weights0["objects"]) / total_weights; weights0.at<double>(3, 0) = (i_weights0["unknown"] + l_weights0["unknown"]) / total_weights; // cout << means0 << endl; // condi��es do EM // dims => samples.cols // if (!(&means0) || (!means0.empty() && means0.rows == nClusters && means0.cols == samples.cols && means0.channels() == 1)) cout << "means - ok!" << endl; EM em = EM(nClusters, EM::COV_MAT_DIAGONAL); em.set("maxIters", maxIters); em.trainE(samples, means0, covs0, weights0); // calcula o tempo de execu��o double tempoFim = static_cast<double>(getTickCount()); double tempoExecutando = ((tempoFim - tempoInicio) / getTickFrequency()) * 1000; // exibe as sa�das definidas (texto e/ou imagem) if (verbose) cout << "- em opencv (2 features): " << tempoExecutando << " ms" << endl; if (display) { // predict Mat1b predictedImage = Mat1b(I_imageClone.size(), uchar(0)); for (int j = 0; j < predictedImage.rows; ++j) { unsigned char *ptRowI = I_imageClone.ptr<uchar>(j); unsigned char *ptRowL = L_imageClone.ptr<uchar>(j); unsigned char *ptRowDst = predictedImage.ptr<uchar>(j); for (int i = 0; i < predictedImage.cols; ++i) { Mat1d elementPredict = Mat1d(Size(2, 1), CV_64FC1); elementPredict.at<double>(0) = ptRowL[i]; elementPredict.at<double>(1) = ptRowI[i]; Vec2d emPredicted = em.predict(elementPredict); switch ((int)emPredicted[1]) { case 0: ptRowDst[i] = 160; break; case 1: ptRowDst[i] = 255; break; case 2: ptRowDst[i] = 80; break; case 3: ptRowDst[i] = 0; break; } } } imshow("EM OpenCV - 2 Features", predictedImage); } }
void Nieto::ExpectationMaximizationOpenCV(const Mat1b &inGrayFrameRoi, int maxIters, map<string, double> &_means0, map<string, double> &_covs0, map<string, double> &_weights0) { double tempoInicio = static_cast<double>(getTickCount()); const int nClusters = 4; // 4 classes => {pavement, markings, objects, unknown} const bool aplicaResize = true; EM em = EM(nClusters, EM::COV_MAT_DIAGONAL); Mat1b grayFrameRoiClone = inGrayFrameRoi.clone(); Mat1b trainGrayFrameRoiClone = inGrayFrameRoi.clone(); if (aplicaResize) resize(grayFrameRoiClone, trainGrayFrameRoiClone, Size(160, 35), 0, 0, INTER_NEAREST); Mat1d samples = trainGrayFrameRoiClone.reshape(1, trainGrayFrameRoiClone.rows * trainGrayFrameRoiClone.cols); // formata o _means0 Mat1d means0 = Mat1d(nClusters, 1, CV_64FC1); means0.at<double>(0) = _means0["pavement"]; means0.at<double>(1) = _means0["markings"]; means0.at<double>(2) = _means0["objects"]; means0.at<double>(3) = 255.0 / 2.0; // formata o _covs0 vector<Mat> covs0 = { Mat1d(1, 1, _covs0["pavement"]), Mat1d(1, 1, _covs0["markings"]), Mat1d(1, 1, _covs0["objects"]), Mat1d(1, 1, ((255.0 / 2.0) / sqrt(3)) * ((255.0 / 2.0) / sqrt(3))) }; // formata o _weights0 // Mat1d weights0 = *(Mat1f(nClusters, 1, CV_64FC1) << 0.75, 0.10, 0.10, 0.05); Mat1d weights0 = *(Mat1f(nClusters, 1, CV_64FC1) << _weights0["pavement"], _weights0["markings"], _weights0["objects"], _weights0["unknown"] ); // cout << means0 << endl; em.set("maxIters", maxIters); em.trainE(samples, means0, covs0, weights0); // calcula o tempo de execu��o double tempoFim = static_cast<double>(getTickCount()); double tempoExecutando = ((tempoFim - tempoInicio) / getTickFrequency()) * 1000; // exibe as sa�das definidas (texto e/ou imagem) if (verbose) cout << "- em opencv (1 feature): " << tempoExecutando << " ms" << endl; if (display) { // predict Mat1b predictedImage = Mat1b(grayFrameRoiClone.size(), uchar(0)); for (int j = 0; j < predictedImage.rows; ++j) { unsigned char *ptRowSrc = grayFrameRoiClone.ptr<uchar>(j); unsigned char *ptRowDst = predictedImage.ptr<uchar>(j); for (int i = 0; i < predictedImage.cols; ++i) { Vec2d emPredicted = em.predict(ptRowSrc[i]); switch ((int)emPredicted[1]) { case 0: ptRowDst[i] = 160; break; case 1: ptRowDst[i] = 255; break; case 2: ptRowDst[i] = 80; break; case 3: ptRowDst[i] = 0; break; } } } imshow("EM OpenCV - 1 Feature", predictedImage); } }