Ejemplo 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);
}
Ejemplo 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;
}
Ejemplo n.º 3
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;
}
Ejemplo n.º 4
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;
}