Esempio n. 1
0
void opf_OPFAgglomerativeLearning(Subgraph **sgtrain, Subgraph **sgeval){
    int n, i = 1;
    float Acc;

    /*while  there exists misclassified samples in sgeval*/
    do{
        fflush(stdout); fprintf(stdout, "\nrunning iteration ... %d ", i++);
        n = 0;
        opf_OPFTraining(*sgtrain);
        opf_OPFClassifying(*sgtrain, *sgeval);
        Acc = opf_Accuracy(*sgeval); fprintf(stdout," %f",Acc*100);
        opf_MoveMisclassifiedNodes(&(*sgeval), &(*sgtrain), &n);
        fprintf(stdout,"\nMisclassified nodes: %d",n);
    }while(n);
}
Esempio n. 2
0
//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;
}
Esempio n. 3
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;
}
Esempio n. 4
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;
}
//Incremental classification function: it  classifies labeled samples from sg and adds them to sgtrain-----
void opf_OPFClassifyingIncremental(Subgraph *sgtrain, Subgraph *sg)
{
  int i;

  // DEBUG VARIABLES
  //printf("Classifying %d new nodes...\n", sg->nnodes);
  int totalOfNewNodes = sg->nnodes;
  int totalOfSimple = 0;
  int totalOfRecheckProt = 0;
  int totalOfNewTree = 0;

  opf_OPFClassifying(sgtrain, sg); 

  //sortList(sgtrain);
  
  // Adding each new node
  for (i = 0; i < sg->nnodes; i++)
  {
    // Classify the current node - > it is killing OUR TIME! -> changed to classify all (above)
    //opf_OPFClassifyOneInstance(sgtrain, sg, i); 
  
    // Adds the new node to the subgraph structure
    AddSNode(sgtrain, &(sg->node[i]));
    // The new node is (sgtrain->node[sgtrain->nnodes-1])

    // Gets the index of both the new node and its predecessor (as found by OPF_Classifying)
    int newNode = sgtrain->nnodes-1;
    int predecessor = sgtrain->node[newNode].pred;

    //printf("\tCurrently classifying a node of label %d (%.2f,%.2f) [true: %d]. \n\t**s predecessor is label %d (%.2f,%.2f) [prototype: %d]\n",
     //sgtrain->node[newNode].label, sgtrain->node[newNode].feat[0], sgtrain->node[newNode].feat[1],
     //sgtrain->node[newNode].truelabel, sgtrain->node[predecessor].label,
     //sgtrain->node[predecessor].feat[0], sgtrain->node[predecessor].feat[1],
     //sgtrain->node[predecessor].pred == NIL);

    // Checks if the new node has been correctly classified
    if((sgtrain->node[newNode].label == sgtrain->node[newNode].truelabel)) {
      // Checks if its predecessor is not a prototype
      if (sgtrain->node[predecessor].pred != NIL){
        /*DEBUG*/
        //printf("\tThis node is simple! Running INSERT...\n");
        totalOfSimple++;

        // If so, simply updates the mininum tree
        
        int* oldNodes = (int*)calloc(sgtrain->nnodes, sizeof(int));
        int t = 0;

        INSERT(sgtrain, newNode, predecessor, &t, oldNodes);

        free(oldNodes);
      } else {
        /*DEBUG*/totalOfRecheckProt++;
        //printf("\tThis node is a recheck! Running recheck...\n");

        // If predecessor is a prototype, need to determine new prototype
        recheckPrototype(sgtrain, newNode, predecessor);
      }
    } else {
      // The node has not been classified correctly; 
      // as such, both the new node and its linked predecessor become prototypes

      /*DEBUG*/totalOfNewTree++;
     //printf("\tThis node is a new tree! Prototyping it and reconquering its pred...\n");
      
      // Updating the new node; simply a new prototype whose pair is its pred
      sgtrain->node[newNode].label = sgtrain->node[newNode].truelabel;
      sgtrain->node[newNode].pred = NIL;
      sgtrain->node[newNode].pathval = 0; 
      sgtrain->node[newNode].prototypePair = predecessor;

      // Updates the linked node; becomes a prototype and begin reconquest
      sgtrain->node[predecessor].prototypePair = newNode;
      sgtrain->node[predecessor].pred = NIL;
      sgtrain->node[predecessor].pathval = 0;
      reconquest(sgtrain, predecessor);
    }
    // Reordering the ordered list of nodes
    //printf("Correcting list...\n");
    correctList(sgtrain);
 }

  /*DEBUG*///printf("******* Total of Nodes: %d\n", totalOfNewNodes);
  /*DEBUG*///printf("******* Types: S: %d, RP: %d, NT: %d\n", totalOfSimple, totalOfRecheckProt, totalOfNewTree);
}
Esempio n. 6
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;
}