int main(int argc, char* argv[]) {
  double *scores = NULL;
  long i;

  char testfile[1024];
  char modelfile[1024];
    char scoreFile[1024];
    FILE *fscore;

  STRUCTMODEL model;
  STRUCT_LEARN_PARM sparm;
  LEARN_PARM lparm;
  KERNEL_PARM kparm;

  SAMPLE testsample;

  /* read input parameters */
  read_input_parameters(argc,argv,testfile,modelfile,scoreFile,&sparm);
	fscore = fopen(scoreFile,"w");

  /* read model file */
  printf("Reading model..."); fflush(stdout);
  model = read_struct_model(modelfile, &sparm);
  printf("done.\n"); 

  /* read test examples */
	printf("Reading test examples..."); fflush(stdout);
  testsample = read_struct_test_examples(testfile,&sparm);
	printf("done.\n");

  init_struct_model(testsample,&model,&sparm,&lparm,&kparm);

  scores = classify_struct_example(testsample.examples[0].x,&model);
  for(i = 0; i < (testsample.examples[0].n_pos+testsample.examples[0].n_neg); i++){
    fprintf(fscore, "%0.5f\n", scores[i]);
  }
    
  fclose(fscore);

  //free_struct_sample(testsample); TODO: Uncomment this, and fix this function. It frees h.h_is which was never allocated while classifying.
  free_struct_model(model,&sparm);

  return(0);

}
int main (int argc, char* argv[])
{
  long correct=0,incorrect=0,no_accuracy=0;
  long i;
  double t1,runtime=0;
  double avgloss=0,l;
  FILE *predfl;
  STRUCTMODEL model; 
  STRUCT_LEARN_PARM sparm;
  STRUCT_TEST_STATS teststats;
  SAMPLE testsample;
  LABEL y;

  svm_struct_classify_api_init(argc,argv);

  read_input_parameters(argc,argv,testfile,modelfile,predictionsfile,&sparm,
			&verbosity,&struct_verbosity);

  if(struct_verbosity>=1) {
    printf("Reading model..."); fflush(stdout);
  }
  model=read_struct_model(modelfile,&sparm);
  if(struct_verbosity>=1) {
    fprintf(stdout, "done.\n");
  }

  if(model.svm_model->kernel_parm.kernel_type == LINEAR) { /* linear kernel */
    /* compute weight vector */
    //add_weight_vector_to_linear_model(model.svm_model);
    //model.w=model.svm_model->lin_weights;
  }
  
  if(struct_verbosity>=1) {
    printf("Reading test examples..."); fflush(stdout);
  }
  testsample=read_struct_examples(testfile,&sparm);
  if(struct_verbosity>=1) {
    printf("done.\n"); fflush(stdout);
  }

  if(struct_verbosity>=1) {
    printf("Classifying test examples..."); fflush(stdout);
  }

  if ((predfl = fopen (predictionsfile, "w")) == NULL)
  { perror (predictionsfile); exit (1); }

  for(i=0;i<testsample.n;i++) {
    t1=get_runtime();
    y=classify_struct_example(testsample.examples[i].x,&model,&sparm);
    runtime+=(get_runtime()-t1);

    write_label(predfl,y);
    l=loss(testsample.examples[i].y,y,&sparm);
    avgloss+=l;
    if(l == 0) 
      correct++;
    else
      incorrect++;
    eval_prediction(i,testsample.examples[i],y,&model,&sparm,&teststats);

    if(empty_label(testsample.examples[i].y)) 
      { no_accuracy=1; } /* test data is not labeled */
    if(struct_verbosity>=2) {
      if((i+1) % 100 == 0) {
	printf("%ld..",i+1); fflush(stdout);
      }
    }
    free_label(y);
  }  
  avgloss/=testsample.n;
  fclose(predfl);

  if(struct_verbosity>=1) {
    printf("done\n");
    printf("Runtime (without IO) in cpu-seconds: %.2f\n",
	   (float)(runtime/100.0));    
  }
  if((!no_accuracy) && (struct_verbosity>=1)) {
    printf("Average loss on test set: %.4f\n",(float)avgloss);
    printf("Zero/one-error on test set: %.2f%% (%ld correct, %ld incorrect, %d total)\n",(float)100.0*incorrect/testsample.n,correct,incorrect,testsample.n);
  }
  print_struct_testing_stats(testsample,&model,&sparm,&teststats);
  free_struct_sample(testsample);
  free_struct_model(model);

  svm_struct_classify_api_exit();

  return(0);
}
Ejemplo n.º 3
0
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
#endif
{
	long correct=0,incorrect=0,no_accuracy=0;
	long i;
	double t1,runtime=0;
	double avgloss=0,l;
#ifndef COMPILE_MEX_INTERFACE
	FILE *predfl;
#endif
	STRUCTMODEL model; 
	STRUCT_LEARN_PARM sparm;
	STRUCT_TEST_STATS teststats;
	SAMPLE testsample;
	LABEL y;
#ifdef COMPILE_MEX_INTERFACE
	int argc;
	char **argv;
	if (nrhs < 3) {
		print_help();
		return;
	}
	else if (nrhs==3) {
		argc=1;
		argv=(char **)my_malloc(MAX_ARGVS*sizeof(char *));
		argv[0]="OLR";
	}
	else
		create_argc_argv(prhs[3],&argc,&argv);
#endif
	svm_struct_classify_api_init(argc,argv);
	
#ifndef COMPILE_MEX_INTERFACE
	read_input_parameters(argc,argv,testfile,modelfile,predictionsfile,&sparm,
						  &verbosity,&struct_verbosity);
#else
	read_input_parameters(argc,argv,&sparm,&verbosity,&struct_verbosity);
#endif
	
	if(struct_verbosity>=1) {
		printf("Reading model..."); fflush(stdout);
	}
#ifndef COMPILE_MEX_INTERFACE
	model=read_struct_model(modelfile,&sparm);
#else
	model=read_struct_model(prhs[2],&sparm);
#endif
	if(struct_verbosity>=1) {
		fprintf(stdout, "done.\n");
	}
	
	if(model.svm_model->kernel_parm.kernel_type == LINEAR) { /* linear kernel */
		/* compute weight vector */
		add_weight_vector_to_linear_model(model.svm_model);
		model.w=model.svm_model->lin_weights;
	}
	
	if(struct_verbosity>=1) {
		printf("Reading test examples..."); fflush(stdout);
	}
#ifndef COMPILE_MEX_INTERFACE
	testsample=read_struct_examples(testfile,&sparm);
#else
	testsample=read_struct_examples(prhs,&sparm);
#endif
	if(struct_verbosity>=1) {
		printf("done.\n"); fflush(stdout);
	}
	
	if(struct_verbosity>=1) {
		printf("Classifying test examples..."); fflush(stdout);
	}
#ifndef COMPILE_MEX_INTERFACE
	if ((predfl = fopen (predictionsfile, "w")) == NULL)
	{ perror (predictionsfile); exit (1); }
#else
	mwSize rows=mxGetM(prhs[0]);
	mxArray *predictions=mxCreateDoubleMatrix(rows,1,mxREAL);
	double *pred_ptr=mxGetPr(predictions);
#endif
	for(i=0;i<testsample.n;i++) {
		t1=get_runtime();
		y=classify_struct_example(testsample.examples[i].x,&model,&sparm);
		runtime+=(get_runtime()-t1);
#ifndef COMPILE_MEX_INTERFACE
		write_label(predfl,y);
#else
		write_label(&pred_ptr,y);
#endif
		l=loss(testsample.examples[i].y,y,&sparm);
		avgloss+=l;
		if(l == 0) 
			correct++;
		else
			incorrect++;
		eval_prediction(i,testsample.examples[i],y,&model,&sparm,&teststats);
		
		if(empty_label(testsample.examples[i].y)) 
		{ no_accuracy=1; } /* test data is not labeled */
		if(struct_verbosity>=2) {
			if((i+1) % 100 == 0) {
				printf("%ld..",i+1); fflush(stdout);
			}
		}
		free_label(y);
	}
	avgloss/=testsample.n;
#ifndef COMPILE_MEX_INTERFACE
	fclose(predfl);
#endif
	if(struct_verbosity>=1) {
		printf("done\n");
		printf("Runtime (without IO) in cpu-seconds: %.2f\n",
			   (float)(runtime/100.0));    
	}
	if((!no_accuracy) && (struct_verbosity>=1)) {
		printf("Average loss on test set: %.4f\n",(float)avgloss);
		printf("Zero/one-error on test set: %.2f%% (%ld correct, %ld incorrect, %d total)\n",(float)100.0*incorrect/testsample.n,correct,incorrect,testsample.n);
	}
	print_struct_testing_stats(testsample,&model,&sparm,&teststats);
	free_struct_sample(testsample);
	free_struct_model(model);
	
	svm_struct_classify_api_exit();
#ifndef COMPILE_MEX_INTERFACE
	return(0);
#else
	plhs[0]=predictions;
#endif
}
int main(int argc, char* argv[]) {
    double avghingeloss;
    LABEL y;
    long i, correct;
    double weighted_correct;

    char testfile[1024];
    char modelfile[1024];
    char labelfile[1024];
    char latentfile[1024];
    char scorefile[1024];
    FILE	*flabel;
    FILE	*flatent;
    FILE *fscore;

    STRUCTMODEL model;
    STRUCT_LEARN_PARM sparm;

    SAMPLE testsample;


    /* read input parameters */
    read_input_parameters(argc,argv,testfile,modelfile,labelfile,latentfile,scorefile,model.kernel_info_file,model.filestub, &sparm);

    printf("C: %f\n",sparm.C);
    flabel = fopen(labelfile,"w");
    flatent = fopen(latentfile,"w");
    fscore = fopen(scorefile, "w");

    init_struct_model(model.kernel_info_file, &model, &sparm);

    read_struct_model(modelfile, &model);


    /* read test examples */
    printf("Reading test examples..."); fflush(stdout);
    testsample = read_struct_examples(testfile, &model, &sparm);
    printf("done.\n");

    IMAGE_KERNEL_CACHE ** cached_images = init_cached_images(testsample.examples,&model);

    avghingeloss = 0.0;
    correct = 0;
    weighted_correct=0.0;
    int *valid_example_kernel = (int *) malloc(5*sizeof(int));
    for(i = 0; i < model.num_kernels; i++)
    valid_example_kernel[i] = 1;
    
    double total_example_weight = 0;
    int num_distinct_examples = 0;
    int last_image_id = -1;
    LATENT_VAR h = make_latent_var(&model);
	double * scores = (double *)calloc(sparm.n_classes, sizeof(double));
    for (i=0;i<testsample.n;i++) {
        while (testsample.examples[i].x.image_id == last_image_id) i++;
        last_image_id = testsample.examples[i].x.image_id;
        num_distinct_examples++;
        //    if(finlatent) {
        //        read_latent_var(&h,finlatent);
            //printf("%d %d\n",h.position_x,h.position_y);
        //    }
        //printf("%f\n",sparm.C);
            struct timeval start_time;
            struct timeval finish_time;
            gettimeofday(&start_time, NULL);

            classify_struct_example(testsample.examples[i].x,&y,&h,cached_images,&model,&sparm,1);

            gettimeofday(&finish_time, NULL);
            double microseconds = 1e6 * (finish_time.tv_sec - start_time.tv_sec) + (finish_time.tv_usec - start_time.tv_usec);
        //printf("This ESS call took %f milliseconds.\n", microseconds/1e3);

            total_example_weight += testsample.examples[i].x.example_cost;
            //double hinge_l = get_hinge_l_from_pos_score(pos_score,testsample.examples[i].y);
            //printf("with a pos_score of %f, a label of %d we get a hinge_l of %f\n", pos_score, testsample.examples[i].y.label, hinge_l);
       // double weighted_hinge_l = hinge_l * testsample.examples[i].x.example_cost;
        //avghingeloss += weighted_hinge_l;
        //if (hinge_l<1) {

        //A classification is considered "correct" if it guesses one of the objects in the image
        if (y.label == testsample.examples[i].y.label || testsample.examples[i].x.also_correct[y.label]) {
            correct++;
            weighted_correct+=testsample.examples[i].x.example_cost;
        }

        print_label(y, flabel);
        fprintf(flabel,"\n"); fflush(flabel);

        print_latent_var(testsample.examples[i].x, h, flatent);

        get_class_scores(testsample.examples[i].x, cached_images, scores, &model, &sparm);
        fprintf(fscore, "%s ", testsample.examples[i].x.image_path);
        for (int j = 0; j < sparm.n_classes; ++j) {
            fprintf(fscore, "%f ", scores[j]);
        }
        fprintf(fscore, "\n");
    }
    free_latent_var(h);	
    fclose(flabel);
    fclose(flatent);
	free(scores);

    //double w_cost = regularizaton_cost(model.w_curr.get_vec(), model.sizePsi);
    //avghingeloss =  avghingeloss/testsample.n;
    printf("\n");
    //printf("Objective Value with C=%f is %f\n\n\n", sparm.C, (sparm.C * avghingeloss) + w_cost);
    //printf("Average hinge loss on dataset: %.4f\n", avghingeloss);
    printf("Zero/one error on test set: %.4f\n", 1.0 - ((float) correct) / (1.0 * num_distinct_examples));
    printf("Weighted zero/one error on the test set %.4f\n", 	1.0 - (weighted_correct/total_example_weight));
    printf("zeroone %.4f weightedzeroone %.4f\n", 1.0 - ((float) correct) / (1.0 * num_distinct_examples), 1.0 - (weighted_correct/total_example_weight));  

    fclose(fscore);
    
    free_cached_images(cached_images, &model);
    //free_struct_sample(testsample);
    free_struct_model(model,&sparm);

    return(0);

}