//Move misclassified nodes from source graph (src) to destiny graph (dst) void opf_MoveMisclassifiedNodes(Subgraph **src, Subgraph **dst, int *p){ int i, j, k, num_of_misclassified=0; Subgraph *newsrc = NULL, *newdst = NULL; for (i=0; i < (*src)->nnodes; i++){ if ((*src)->node[i].truelabel != (*src)->node[i].label) num_of_misclassified++; } *p = num_of_misclassified; if (num_of_misclassified>0){ newsrc = CreateSubgraph((*src)->nnodes-num_of_misclassified); newdst = CreateSubgraph((*dst)->nnodes+num_of_misclassified); newsrc->nfeats = (*src)->nfeats; newdst->nfeats = (*dst)->nfeats; newsrc->nlabels = (*src)->nlabels; newdst->nlabels = (*dst)->nlabels; for (i = 0; i < (*dst)->nnodes; i++) CopySNode(&(newdst->node[i]), &((*dst)->node[i]), newdst->nfeats); j=i; k = 0; for (i=0; i < (*src)->nnodes; i++){ if ((*src)->node[i].truelabel == (*src)->node[i].label)// misclassified node CopySNode(&(newsrc->node[k++]), &((*src)->node[i]), newsrc->nfeats); else CopySNode(&(newdst->node[j++]), &((*src)->node[i]), newdst->nfeats); } DestroySubgraph(&(*src)); DestroySubgraph(&(*dst)); *src = newsrc; *dst = newdst; } }
int main(int argc, char **argv){ fflush(stdout); fprintf(stdout, "\nProgram that generates training, evaluation and test sets for the OPF classifier\n"); fprintf(stdout, "\nIf you have any problem, please contact: "); fprintf(stdout, "\n- [email protected]"); fprintf(stdout, "\n- [email protected]\n"); fprintf(stdout, "\nLibOPF version 2.0 (2009)\n"); fprintf(stdout, "\n"); fflush(stdout); if(argc != 6){ fprintf(stderr, "\nusage opf_split <P1> <P2> <P3> <P4> <P5>"); fprintf(stderr, "\nP1: input dataset in the OPF file format"); fprintf(stderr, "\nP2: percentage for the training set size [0,1]"); fprintf(stderr, "\nP3: percentage for the evaluation set size [0,1] (leave 0 in the case of no learning)"); fprintf(stderr, "\nP4: percentage for the test set size [0,1]"); fprintf(stderr, "\nP5: normalize features? 1 - Yes 0 - No\n\n"); exit(-1); } Subgraph *g = NULL, *gAux = NULL, *gTraining = NULL, *gEvaluating = NULL, *gTesting = NULL; float training_p = atof(argv[2]), evaluating_p = atof(argv[3]), testing_p = atof(argv[4]); int normalize = atoi(argv[5]); CheckInputData(training_p, evaluating_p, testing_p); fprintf(stdout, "\nReading data set ..."); fflush(stdout); g = ReadSubgraph(argv[1]); fprintf(stdout, " OK"); fflush(stdout); if(normalize) opf_NormalizeFeatures(g); fprintf(stdout, "\nSplitting data set ..."); fflush(stdout); opf_SplitSubgraph(g, &gAux, &gTesting, training_p+evaluating_p); if (evaluating_p > 0) opf_SplitSubgraph(gAux, &gTraining, &gEvaluating, training_p/(training_p+evaluating_p)); else gTraining = CopySubgraph(gAux); fprintf(stdout, " OK"); fflush(stdout); fprintf(stdout, "\nWriting data sets to disk ..."); fflush(stdout); WriteSubgraph(gTraining, "training.dat"); WriteSubgraphPrototype(gTraining, "trainingp.dat"); if (evaluating_p > 0) { WriteSubgraph(gEvaluating, "evaluating.dat"); WriteSubgraphPrototype(gEvaluating, "evaluatingp.dat"); } WriteSubgraph(gTesting, "testing.dat"); WriteSubgraphPrototype(gTesting, "testingp.dat"); fprintf(stdout, " OK"); fflush(stdout); fprintf(stdout, "\nDeallocating memory ..."); DestroySubgraph(&g); DestroySubgraph(&gAux); DestroySubgraph(&gTraining); DestroySubgraph(&gEvaluating); DestroySubgraph(&gTesting); fprintf(stdout, " OK\n"); return 0; }
// Remove irrelevant nodes void opf_RemoveIrrelevantNodes(Subgraph **sg){ Subgraph *newsg = NULL; int i,k,num_of_irrelevants=0; for (i=0; i < (*sg)->nnodes; i++) { if (!(*sg)->node[i].relevant) num_of_irrelevants++; } if (num_of_irrelevants>0){ newsg = CreateSubgraph((*sg)->nnodes - num_of_irrelevants); newsg->nfeats = (*sg)->nfeats; // for (i=0; i < newsg->nnodes; i++) // newsg->node[i].feat = AllocFloatArray(newsg->nfeats); k=0; newsg->nlabels = (*sg)->nlabels; for (i=0; i < (*sg)->nnodes; i++){ if ((*sg)->node[i].relevant){// relevant node CopySNode(&(newsg->node[k]), &((*sg)->node[i]), newsg->nfeats); k++; } } newsg->nlabels=(*sg)->nlabels; DestroySubgraph(sg); *sg=newsg; } }
int main(int argc, char**argv){ if(argc != 3){ fprintf(stderr, "\nusage opf2ann <input libopf file> <output fann file>\n"); exit(-1); } Subgraph *g = ReadSubgraph(argv[1]); WriteSubgraph2FANNFormat(g, argv[2]); DestroySubgraph(&g); return 0; }
//Move irrelevant nodes from source graph (src) to destiny graph (dst) void opf_MoveIrrelevantNodes(Subgraph **src, Subgraph **dst){ int i, j, k, num_of_irrelevants=0; Subgraph *newsrc = NULL, *newdst = NULL; for (i=0; i < (*src)->nnodes; i++){ if (!(*src)->node[i].relevant) num_of_irrelevants++; } if (num_of_irrelevants>0){ newsrc = CreateSubgraph((*src)->nnodes-num_of_irrelevants); newdst = CreateSubgraph((*dst)->nnodes+num_of_irrelevants); newsrc->nfeats = (*src)->nfeats; newdst->nfeats = (*dst)->nfeats; newsrc->nlabels = (*src)->nlabels; newdst->nlabels = (*dst)->nlabels; // for (i=0; i < newsrc->nnodes; i++) // newsrc->node[i].feat = AllocFloatArray(newsrc->nfeats); // for (i=0; i < newdst->nnodes; i++) // newdst->node[i].feat = AllocFloatArray(newdst->nfeats); for (i = 0; i < (*dst)->nnodes; i++) CopySNode(&(newdst->node[i]), &((*dst)->node[i]), newdst->nfeats); j=i; k = 0; for (i=0; i < (*src)->nnodes; i++){ if ((*src)->node[i].relevant)// relevant node CopySNode(&(newsrc->node[k++]), &((*src)->node[i]), newsrc->nfeats); else CopySNode(&(newdst->node[j++]), &((*src)->node[i]), newdst->nfeats); } DestroySubgraph(&(*src)); DestroySubgraph(&(*dst)); *src = newsrc; *dst = newdst; } }
//Learning function: it executes the learning procedure for CompGraph replacing the //missclassified samples in the evaluation set by non prototypes from //training set ----- void opf_OPFLearning(Subgraph **sgtrain, Subgraph **sgeval){ int i = 0, iterations = 10; float Acc = FLT_MIN, AccAnt = FLT_MIN,MaxAcc=FLT_MIN, delta; Subgraph *sg=NULL; do{ AccAnt = Acc; fflush(stdout); fprintf(stdout, "\nrunning iteration ... %d ", i); opf_OPFTraining(*sgtrain); opf_OPFClassifying(*sgtrain, *sgeval); Acc = opf_Accuracy(*sgeval); if (Acc > MaxAcc){ MaxAcc = Acc; if (sg!=NULL) DestroySubgraph(&sg); sg = CopySubgraph(*sgtrain); } opf_SwapErrorsbyNonPrototypes(&(*sgtrain), &(*sgeval)); fflush(stdout); fprintf(stdout,"opf_Accuracy in the evaluation set: %.2f %%\n", Acc*100); i++; delta = fabs(Acc-AccAnt); }while ((delta > 0.0001) && (i <= iterations)); DestroySubgraph(&(*sgtrain)); *sgtrain = sg; }
int main(int argc, char**argv){ if(argc != 3){ fprintf(stderr, "\nusage opf2svmtorch <input libopf file> <output svmtorch file>\n"); exit(-1); } Subgraph *g = ReadSubgraph(argv[1]); if(g->nlabels == 2) WriteSubgraph2SVMTorchFormat(g,argv[2]); else WriteSubgraph2SVMTorchFormatMC(g,argv[2]); DestroySubgraph(&g); return 0; }
int main(int argc, char **argv){ fflush(stdout); fprintf(stdout, "\nProgram that gives information about the OPF file\n"); fprintf(stdout, "\nIf you have any problem, please contact: "); fprintf(stdout, "\n- [email protected]"); fprintf(stdout, "\n- [email protected]\n"); fprintf(stdout, "\nLibOPF version 2.0 (2009)\n"); fprintf(stdout, "\n"); fflush(stdout); if(argc != 2) { fprintf(stderr, "\nusage opf_train <P1>"); fprintf(stderr, "\nP1: OPF file"); exit(-1); } Subgraph *g = NULL; FILE *fp = NULL; int ndata, nfeats, nlabels; size_t result; char msg[128]; if((fp = fopen(argv[1], "rb")) == NULL){ sprintf(msg, "%s%s", "Unable to open file ", argv[1]); Error(msg,"opf_info"); } result = fread(&ndata, sizeof(int), 1, fp); result = fread(&nlabels, sizeof(int), 1, fp); result = fread(&nfeats, sizeof(int), 1, fp); fprintf(stdout, "\nInformations about %s file\n --------------------------------", argv[1]); fprintf(stdout,"\nData size: %d",ndata); fprintf(stdout,"\nFeatures size: %d",nfeats); fprintf(stdout,"\nLabels number: %d",nlabels); fprintf(stdout, "\n--------------------------------\n"); DestroySubgraph(&g); fclose(fp); return 0; }
void OPFScore::predict(Data *data) { Subgraph *g = data2Subgraph(data); int cntSamples, nSamples; destroyScores(); scoreSize = data->getNSamples(); scoresNumClasses = subgraph->nlabels; scores = new float*[scoreSize]; for (int i = 0; i < scoreSize; i++) scores[i] = new float[scoresNumClasses]; opf_OPFClassifyingScore(subgraph, g, scores); for(cntSamples = 0, nSamples = data->getNSamples(); cntSamples < nSamples; ++cntSamples){ data->setClassificationLabel(cntSamples, g->node[cntSamples].label); } DestroySubgraph(&g); //this->predictedData = data->clone(); }
int main(int argc, char **argv){ fflush(stdout); fprintf(stdout, "\nProgram that generates the precomputed distance file for the OPF classifier\n"); fprintf(stdout, "\nIf you have any problem, please contact: "); fprintf(stdout, "\n- [email protected]"); fprintf(stdout, "\n- [email protected]\n"); fprintf(stdout, "\nLibOPF version 2.0 (2009)\n"); fprintf(stdout, "\n"); fflush(stdout); if(argc != 4){ fprintf(stderr, "\nusage opf_distance <P1> <P2> <P3>"); fprintf(stderr, "\nP1: Dataset in the OPF file format"); fprintf(stderr, "\nP2: Distance ID\n"); fprintf(stderr, "\n 1 - Euclidean"); fprintf(stderr, "\n 2 - Chi-Square"); fprintf(stderr, "\n 3 - Manhattan (L1)"); fprintf(stderr, "\n 4 - Canberra"); fprintf(stderr, "\n 5 - Squared Chord"); fprintf(stderr,"\n 6 - Squared Chi-Squared"); fprintf(stderr,"\n 7 - BrayCurtis"); fprintf(stderr,"\n 8 - dLog"); fprintf(stderr, "\nP3: Distance normalization? 1- yes 0 - no"); exit(-1); } Subgraph *sg = ReadSubgraph(argv[1]); FILE *fp = fopen("distances.dat", "wb"); int i, j, distance = atoi(argv[2]), normalize = atoi(argv[3]); float **Distances = NULL, max = FLT_MIN; size_t result; result = fwrite(&sg->nnodes, sizeof(int), 1, fp); fprintf(stdout, "\n Stack %u...", stackavail()); Distances = (float **)malloc(sg->nnodes*sizeof(float *)); for (i = 0; i < sg->nnodes; i++) Distances[i] = (float *)calloc(sizeof(float),sg->nnodes); fprintf(stdout, "\n Stack after %u...", stackavail()); switch(distance){ case 1: fprintf(stdout, "\n Computing euclidean distance ..."); for (i = 0; i < sg->nnodes; i++){ for (j = 0; j < sg->nnodes; j++){ if(i == j) Distances[i][j] = 0.0; else Distances[sg->node[i].position][sg->node[j].position] = opf_EuclDist(sg->node[i].feat, sg->node[j].feat, sg->nfeats); if(Distances[sg->node[i].position][sg->node[j].position] > max) max = Distances[sg->node[i].position][sg->node[j].position]; } } break; case 2: fprintf(stdout, "\n Computing chi-square distance ...\n"); for (i = 0; i < sg->nnodes; i++){ for (j = 0; j < sg->nnodes; j++){ if(i == j) Distances[i][j] = 0.0; else Distances[sg->node[i].position][sg->node[j].position] = opf_ChiSquaredDist(sg->node[i].feat, sg->node[j].feat, sg->nfeats); if(Distances[sg->node[i].position][sg->node[j].position] > max) max = Distances[sg->node[i].position][sg->node[j].position]; } } break; case 3: fprintf(stdout, "\n Computing Manhattan distance ...\n"); for (i = 0; i < sg->nnodes; i++){ for (j = 0; j < sg->nnodes; j++){ if(i == j) Distances[i][j] = 0.0; else Distances[sg->node[i].position][sg->node[j].position] = opf_ManhattanDist(sg->node[i].feat, sg->node[j].feat, sg->nfeats); if(Distances[sg->node[i].position][sg->node[j].position] > max) max = Distances[sg->node[i].position][sg->node[j].position]; } } break; case 4: fprintf(stdout, "\n Computing Canberra distance ...\n"); for (i = 0; i < sg->nnodes; i++){ for (j = 0; j < sg->nnodes; j++){ if(i == j) Distances[i][j] = 0.0; else Distances[sg->node[i].position][sg->node[j].position] = opf_CanberraDist(sg->node[i].feat, sg->node[j].feat, sg->nfeats); if(Distances[sg->node[i].position][sg->node[j].position] > max) max = Distances[sg->node[i].position][sg->node[j].position]; } } break; case 5: fprintf(stdout, "\n Computing Squared Chord distance ...\n"); for (i = 0; i < sg->nnodes; i++){ for (j = 0; j < sg->nnodes; j++){ if(i == j) Distances[i][j] = 0.0; else Distances[sg->node[i].position][sg->node[j].position] = opf_SquaredChordDist(sg->node[i].feat, sg->node[j].feat, sg->nfeats); if(Distances[sg->node[i].position][sg->node[j].position] > max) max = Distances[sg->node[i].position][sg->node[j].position]; } } break; case 6: fprintf(stdout, "\n Computing Squared Chi-squared distance ...\n"); for (i = 0; i < sg->nnodes; i++){ for (j = 0; j < sg->nnodes; j++){ if(i == j) Distances[i][j] = 0.0; else Distances[sg->node[i].position][sg->node[j].position] = opf_SquaredChiSquaredDist(sg->node[i].feat, sg->node[j].feat, sg->nfeats); if(Distances[sg->node[i].position][sg->node[j].position] > max) max = Distances[sg->node[i].position][sg->node[j].position]; } } break; case 7: fprintf(stdout, "\n Computing Bray Curtis distance ...\n"); for (i = 0; i < sg->nnodes; i++){ for (j = 0; j < sg->nnodes; j++){ if(i == j) Distances[i][j] = 0.0; else Distances[sg->node[i].position][sg->node[j].position] = opf_BrayCurtisDist(sg->node[i].feat, sg->node[j].feat, sg->nfeats); if(Distances[sg->node[i].position][sg->node[j].position] > max) max = Distances[sg->node[i].position][sg->node[j].position]; } } case 8: fprintf(stdout, "\n Computing dLog ...\n"); for (i = 0; i < sg->nnodes; i++){ for (j = 0; j < sg->nnodes; j++){ if(i == j) Distances[i][j] = 0.0; else Distances[sg->node[i].position][sg->node[j].position] = opf_dLog(sg->node[i].feat, sg->node[j].feat, sg->nfeats); if(Distances[sg->node[i].position][sg->node[j].position] > max) max = Distances[sg->node[i].position][sg->node[j].position]; } //fprintf(stdout, "\t[%d] %.2f", i, Distances[i][1]); } break; default: fprintf(stderr, "\nInvalid distance ID ...\n"); } fprintf(stdout, "\n Writing matrix file ...\n"); fflush(stdout); getchar(); if (!normalize) max = 1.0; for (i = 0; i < sg->nnodes; i++){ for (j = 0; j < sg->nnodes; j++){ Distances[i][j]/=max; result = fwrite(&Distances[i][j], sizeof(float), 1, fp); } } fprintf(stdout, "\n\nDistances generated ...\n"); fflush(stdout); fprintf(stdout, "\n\nDeallocating memory ...\n"); for (i = 0; i < sg->nnodes; i++) free(Distances[i]); free(Distances); DestroySubgraph(&sg); fclose(fp); return 0; }
int main(int argc, char **argv) { fflush(stdout); fprintf(stdout, "\nProgram that executes the test phase of the OPF classifier\n"); fprintf(stdout, "\nIf you have any problem, please contact: "); fprintf(stdout, "\n- [email protected]"); fprintf(stdout, "\n- [email protected]\n"); fprintf(stdout, "\nLibOPF version 3.0 (2013)\n"); fprintf(stdout, "\n"); fflush(stdout); if((argc != 3) && (argc != 2)) { fprintf(stderr, "\nusage opf_classify <P1> <P2>"); fprintf(stderr, "\nP1: test set in the OPF file format"); fprintf(stderr, "\nP2: precomputed distance file (leave it in blank if you are not using this resource\n"); exit(-1); } int n,i; float time; char fileName[256]; FILE *f = NULL; timer tic, toc; if(argc == 3) opf_PrecomputedDistance = 1; fprintf(stdout, "\nReading data files ..."); fflush(stdout); Subgraph *gTest = ReadSubgraph(argv[1]), *gTrain = opf_ReadModelFile("classifier.opf"); fprintf(stdout, " OK"); fflush(stdout); if(opf_PrecomputedDistance) opf_DistanceValue = opf_ReadDistances(argv[2], &n); fprintf(stdout, "\nClassifying test set ..."); fflush(stdout); gettimeofday(&tic,NULL); opf_OPFClassifying(gTrain, gTest); gettimeofday(&toc,NULL); fprintf(stdout, " OK"); fflush(stdout); fprintf(stdout, "\nWriting output file ..."); fflush(stdout); sprintf(fileName,"%s.out",argv[1]); f = fopen(fileName,"w"); for (i = 0; i < gTest->nnodes; i++) fprintf(f,"%d\n",gTest->node[i].label); fclose(f); fprintf(stdout, " OK"); fflush(stdout); fprintf(stdout, "\nDeallocating memory ..."); DestroySubgraph(&gTrain); DestroySubgraph(&gTest); if(opf_PrecomputedDistance) { for (i = 0; i < n; i++) free(opf_DistanceValue[i]); free(opf_DistanceValue); } fprintf(stdout, " OK\n"); time = ((toc.tv_sec-tic.tv_sec)*1000.0 + (toc.tv_usec-tic.tv_usec)*0.001)/1000.0; fprintf(stdout, "\nTesting time: %f seconds\n", time); fflush(stdout); sprintf(fileName,"%s.time",argv[1]); f = fopen(fileName,"a"); fprintf(f,"%f\n",time); fclose(f); return 0; }
int main(int argc, char **argv) { if (argc != 5) { fprintf(stderr, "\nusage FeatureSelection <training set> <evaluating set> <testing set> <search space configuration file>\n"); exit(-1); } SearchSpace *s = NULL; int i; double time_opt, time_classify, classification_error; timer tic, toc; FILE *f = NULL; TransferFunc optTransfer = NULL; Subgraph *Train = NULL, *Evaluate = NULL, *Merge = NULL, *Test = NULL, *newTrain = NULL, *newTest = NULL; Train = ReadSubgraph(argv[1]); Evaluate = ReadSubgraph(argv[2]); Test = ReadSubgraph(argv[3]); s = ReadSearchSpaceFromFile(argv[4], _WCA_); optTransfer = S2TransferFunction; for (i = 0; i < Train->nfeats; i++) { s->LB[i] = -20; s->LB[i] = 20; } fprintf(stderr, "\nInitializing search space ... "); InitializeSearchSpace(s, _WCA_); fprintf(stderr, "\nOk\n"); fflush(stderr); fprintf(stderr, "\nRunning WCA ... "); gettimeofday(&tic, NULL); runWCA(s, FeatureSelectionOPF, Train, Evaluate, optTransfer); gettimeofday(&toc, NULL); fflush(stderr); fprintf(stderr, "\nOK\n"); time_opt = ((toc.tv_sec - tic.tv_sec) * 1000.0 + (toc.tv_usec - tic.tv_usec) * 0.001) / 1000.0; fprintf(stdout, "\nOptimization time: %f seconds\n", time_opt); fflush(stderr); Merge = opf_MergeSubgraph(Train, Evaluate); fflush(stderr); fprintf(stderr, "\nWriting new training and testing sets ...\n"); newTrain = CreateSubgraphFromSelectedFeatures(Merge, s->g); newTest = CreateSubgraphFromSelectedFeatures(Test, s->g); fprintf(stderr, "\nTraining set\n"); WriteSubgraph(newTrain, "training.wca.dat"); fprintf(stderr, "\n\nTesting set\n"); WriteSubgraph(newTest, "testing.wca.dat"); fflush(stderr); fprintf(stderr, "\nOK\n"); opf_OPFTraining(newTrain); gettimeofday(&tic, NULL); opf_OPFClassifying(newTrain, newTest); gettimeofday(&toc, NULL); classification_error = opf_Accuracy(newTest); time_classify = ((toc.tv_sec - tic.tv_sec) * 1000.0 + (toc.tv_usec - tic.tv_usec) * 0.001) / 1000.0; fprintf(stdout, "\nClassification time: %f seconds\n", time_classify); fflush(stderr); f = fopen("best_feats.txt", "a"); fprintf(f, "%d %d", newTrain->nfeats, (int)s->g[0]); for (i = 1; i < Train->nfeats; i++) { fprintf(f, " %d", (int)s->g[i]); } fprintf(f, "\n"); fclose(f); fprintf(stderr, "\nAccuracy: %.2lf%%\n", 100 * classification_error); f = fopen("final_accuracy.txt", "a"); fprintf(f, "%lf\n", classification_error); fclose(f); fflush(stderr); fprintf(stderr, "\nDeallocating memory ..."); DestroySubgraph(&Train); DestroySubgraph(&Evaluate); DestroySubgraph(&Merge); DestroySubgraph(&Test); DestroySubgraph(&newTrain); DestroySubgraph(&newTest); fflush(stderr); fprintf(stderr, "\nOK\n"); f = fopen("optimization.time", "a"); fprintf(f, "%f %f\n", time_opt, time_classify); fclose(f); DestroySearchSpace(&s, _WCA_); return 0; }
int main(int argc, char **argv) { fflush(stdout); fprintf(stdout, "\nProgram that executes the training phase of the OPF classifier with knn adjacency\n"); fprintf(stdout, "\nIf you have any problem, please contact: "); fprintf(stdout, "\n- [email protected]"); fprintf(stdout, "\n- [email protected]\n"); fprintf(stdout, "\nLibOPF version 2.0 (2009)\n"); fprintf(stdout, "\n"); fflush(stdout); if ((argc != 5) && (argc != 4)) { fprintf(stderr, "\nusage opf_train <P1> <P2>"); fprintf(stderr, "\nP1: training set in the OPF file format"); fprintf(stderr, "\nP2: evaluating set in the OPF file format (used to learn k)"); fprintf(stderr, "\nP3: kmax"); fprintf(stderr, "\nP4: precomputed distance file (leave it in blank if you are not using this resource)\n"); exit(-1); } int n, i, kmax = atoi(argv[3]); char fileName[256]; FILE *f = NULL; timer tic, toc; double time; if (argc == 5) opf_PrecomputedDistance = 1; fprintf(stdout, "\nReading data file ..."); fflush(stdout); Subgraph *Train = ReadSubgraph(argv[1]); Subgraph *Eval = ReadSubgraph(argv[2]); fprintf(stdout, " OK"); fflush(stdout); if (opf_PrecomputedDistance) opf_DistanceValue = opf_ReadDistances(argv[4], &n); fprintf(stdout, "\nTraining OPF classifier ..."); fflush(stdout); gettimeofday(&tic, NULL); opf_OPFknnTraining(Train, Eval, kmax); gettimeofday(&toc, NULL); fprintf(stdout, " OK"); fflush(stdout); fprintf(stdout, "\nWriting classifier's model file ..."); fflush(stdout); opf_WriteModelFile(Train, "classifier.opf"); fprintf(stdout, " OK"); fflush(stdout); fprintf(stdout, "\nWriting output file ..."); fflush(stdout); sprintf(fileName, "%s.out", argv[1]); f = fopen(fileName, "w"); for (i = 0; i < Train->nnodes; i++) fprintf(f, "%d\n", Train->node[i].label); fclose(f); fprintf(stdout, " OK"); fflush(stdout); fprintf(stdout, "\nDeallocating memory ..."); fflush(stdout); DestroySubgraph(&Train); DestroySubgraph(&Eval); if (opf_PrecomputedDistance) { for (i = 0; i < n; i++) free(opf_DistanceValue[i]); free(opf_DistanceValue); } fprintf(stdout, " OK\n"); time = ((toc.tv_sec - tic.tv_sec) * 1000.0 + (toc.tv_usec - tic.tv_usec) * 0.001) / 1000.0; fprintf(stdout, "\nTraining time: %f seconds\n", time); fflush(stdout); sprintf(fileName, "%s.time", argv[1]); f = fopen(fileName, "a"); fprintf(f, "%f\n", time); fclose(f); return 0; }
int main(int argc, char **argv) { if (argc != 4) { fprintf(stderr, "\nusage CombinatorialOPF <training set> <testing set> <input combinatorial matrix>\n"); exit(-1); } int i, j, acc_index, m, n; double time_comb, classification_accuracy, overall_accuracy = 0, **r; timer tic, toc; FILE *f = NULL; Subgraph *Train = NULL, *Test = NULL, *newTrain = NULL, *newTest = NULL; Train = ReadSubgraph(argv[1]); Test = ReadSubgraph(argv[2]); fprintf(stderr, "\nInitializing combinatorial OPF ... "); f = fopen(argv[3], "r"); fscanf(f, "%d\n", &n); m = pow(2, n); r = (double **)calloc(m, sizeof(double *)); for (i = 0; i < m; i++) r[i] = (double *)calloc(n, sizeof(double)); for (i = 0; i < m; i++) { for (j = 0; j < n; j++) { fscanf(f, "%lf ", &r[i][j]); } fscanf(f, "\n"); } fclose(f); fprintf(stderr, "\nOk\n"); fflush(stderr); fprintf(stderr, "\nRunning OPF ... "); f = fopen("final_accuracy.txt", "a"); gettimeofday(&tic, NULL); for (i = 0; i < m; i++) { newTrain = CreateSubgraphFromSelectedFeatures(Train, r[i]); newTest = CreateSubgraphFromSelectedFeatures(Test, r[i]); opf_OPFTraining(newTrain); opf_OPFClassifying(newTrain, newTest); classification_accuracy = opf_Accuracy(newTest); fprintf(f, "%lf\n", classification_accuracy); if (classification_accuracy > overall_accuracy) { overall_accuracy = classification_accuracy; acc_index = i; } } gettimeofday(&toc, NULL); fclose(f); fflush(stderr); fprintf(stderr, "\nOK\n"); time_comb = ((toc.tv_sec - tic.tv_sec) * 1000.0 + (toc.tv_usec - tic.tv_usec) * 0.001) / 1000.0; fprintf(stdout, "\nCombination time: %f seconds\n", time_comb); fflush(stderr); f = fopen("best_feats.txt", "a"); fprintf(f, "%d ", n); for (i = 0; i < n; i++) { fprintf(f, "%d ", (int)r[acc_index][i]); } fprintf(f, "\n"); fclose(f); fflush(stderr); fprintf(stderr, "\nDeallocating memory ..."); DestroySubgraph(&Train); DestroySubgraph(&Test); DestroySubgraph(&newTrain); DestroySubgraph(&newTest); fflush(stderr); fprintf(stderr, "\nOK\n"); f = fopen("combination.time", "a"); fprintf(f, "%f\n", time_comb); fclose(f); return 0; }
int main(int argc, char **argv){ fflush(stdout); fprintf(stdout, "\nProgram that executes the training and classification phase of the OPF classifier\n"); fprintf(stdout, "\nIf you have any problem, please contact: "); fprintf(stdout, "\n- [email protected]"); fprintf(stdout, "\n- [email protected]\n"); fprintf(stdout, "\nLibOPF version 2.0 (2009)\n"); fprintf(stdout, "\n"); fflush(stdout); if((argc != 3) && (argc != 2)){ fprintf(stderr, "\nusage opf_train_and_classify <P1> <P2> <P3>"); fprintf(stderr, "\nP1: training set in the OPF file format"); fprintf(stderr, "\nP2: test set in the OPF file format"); fprintf(stderr, "\nP3: precomputed distance file (leave it in blank if you are not using this resource)\n"); exit(-1); } int n, i; char fileName[256]; FILE *f = NULL; timer tic, toc,ticC,tocC; float time, timeC; if(argc == 4) opf_PrecomputedDistance = 1; fprintf(stdout, "\nReading data file ..."); fflush(stdout); Subgraph *g = ReadSubgraph(argv[1]); Subgraph *gTest = ReadSubgraph(argv[1]); fprintf(stdout, " OK"); fflush(stdout); if(opf_PrecomputedDistance) opf_DistanceValue = opf_ReadDistances(argv[3], &n); fprintf(stdout, "\nTraining OPF classifier ..."); fflush(stdout); gettimeofday(&tic,NULL); opf_OPFTraining(g); gettimeofday(&toc,NULL); fprintf(stdout, " OK"); fflush(stdout); fprintf(stdout, "\nClassifying test set ..."); fflush(stdout); gettimeofday(&ticC,NULL); opf_OPFClassifying(g, gTest); gettimeofday(&tocC,NULL); fprintf(stdout, " OK"); fflush(stdout); fprintf(stdout, "\nWriting classifier's model file ..."); fflush(stdout); opf_WriteModelFile(g, "classifier.opf"); fprintf(stdout, " OK"); fflush(stdout); fprintf(stdout, "\nWriting output file ..."); fflush(stdout); sprintf(fileName,"%s.out",argv[1]); f = fopen(fileName,"w"); for (i = 0; i < g->nnodes; i++) fprintf(f,"%d\n",g->node[i].label); fclose(f); fprintf(stdout, " OK"); fflush(stdout); fprintf(stdout, "\nDeallocating memory ..."); fflush(stdout); DestroySubgraph(&g); DestroySubgraph(&gTest); if(opf_PrecomputedDistance){ for (i = 0; i < n; i++) free(opf_DistanceValue[i]); free(opf_DistanceValue); } fprintf(stdout, " OK\n"); time = ((toc.tv_sec-tic.tv_sec)*1000.0 + (toc.tv_usec-tic.tv_usec)*0.001)/1000.0; fprintf(stdout, "\nTraining time: %f seconds\n", time); fflush(stdout); timeC = ((tocC.tv_sec-ticC.tv_sec)*1000.0 + (tocC.tv_usec-ticC.tv_usec)*0.001)/1000.0; fprintf(stdout, "\nTesting time: %f seconds\n", time); fflush(stdout); sprintf(fileName,"%s.time",argv[1]); f = fopen(fileName,"a"); fprintf(f,"%f,%f\n",time,timeC); fclose(f); return 0; }