int main(int argc, char* argv[]) {
  double avgloss,l;
  long i, correct;

  char testfile[1024];
  char modelfile[1024];

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

  SAMPLE testsample;
  LABEL y;
  LATENT_VAR h; 

  /* read input parameters */
  read_input_parameters(argc,argv,testfile,modelfile,&sparm);

  /* 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_examples(testfile,&sparm);
  printf("done.\n");

  init_struct_model(testsample,&model,&sparm,&lparm,&kparm);
  
  avgloss = 0.0;
  correct = 0;
  for (i=0;i<testsample.n;i++) {
    classify_struct_example(testsample.examples[i].x,&y,&h,&model,&sparm);
    l = loss(testsample.examples[i].y,y,h,&sparm);
    avgloss += l;
    if (l==0) correct++;

    free_label(y);
    free_latent_var(h); 
  }

  printf("Average loss on test set: %.4f\n", avgloss/testsample.n);
  printf("Zero/one error on test set: %.4f\n", 1.0 - ((float) correct)/testsample.n);

  free_struct_sample(testsample);
  free_struct_model(model,&sparm);

  return(0);

}
예제 #2
0
double compute_current_loss(SAMPLE val, STRUCTMODEL *sm, STRUCT_LEARN_PARM *sparm)
{
	long i;
	LABEL y;
	double cur_loss = 0.0;
	double store;
	for(i = 0; i < val.n; i++)
	{
		classify_struct_example(val.examples[i].x,&y,sm,sparm);
		store = loss(val.examples[i].y,y,sparm);
		cur_loss += store;
	}

	cur_loss /= (double) val.n;
	return cur_loss;
}
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);
}
예제 #5
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
}
예제 #6
0
/* data mining hard negative samples */
void data_mining_hard_examples(char *trainfile, char *testfile, STRUCT_LEARN_PARM *sparm, STRUCTMODEL *sm)
{
  int i, j, n, ntrain, ntest, num, object_label, count, count_pos, count_neg;
  char line[BUFFLE_SIZE];
  double *energy, *energies;
  SAMPLE testsample;
  LABEL *y = NULL;
  ENERGYINDEX *energy_index;
  FILE *fp, *ftrain, *ftest;

  /* MPI process */
  int rank;
  int procs_num;
  int start, end, block_size;

  MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  MPI_Comm_size(MPI_COMM_WORLD, &procs_num);

  /* read negative examples */
  printf("Reading negative samples for data mining...");
  testsample = read_struct_examples(testfile, sparm, sm);
  printf("Done\n");

  n = testsample.n;
  block_size = (n+procs_num-1) / procs_num;
  start = rank*block_size;
  end = start+block_size-1 > n-1 ? n-1 : start+block_size-1;

  energy = (double*)my_malloc(sizeof(double)*n);
  energies = (double*)my_malloc(sizeof(double)*n);
  memset(energy, 0, sizeof(double)*n);
  memset(energies, 0, sizeof(double)*n);
  for(i = start; i <= end; i++) 
  {
    y = classify_struct_example(testsample.examples[i].x, &num, 0, sm, sparm);
    count = 0;
    for(j = 0; j < num; j++)
    {
      if(y[j].object_label)
        count++;
    }
    printf("Data mining hard negative example %d/%d: %d objects detected\n", i+1, n, count);
    if(count == 0)
      energy[i] = -sparm->loss_value;
    else
    {
      for(j = 0; j < num; j++)
      {
        if(y[j].object_label)
        {
          energy[i] = y[j].energy;
          break;
        }
      }
    }
    /* free labels */
    for(j = 0; j < num; j++)
      free_label(y[j]);
    free(y);
  }
  MPI_Allreduce(energy, energies, n, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);

  if(rank == 0)
  {
    energy_index = (ENERGYINDEX*)my_malloc(sizeof(ENERGYINDEX)*n);
    for(i = 0; i < n; i++)
    {
      energy_index[i].index = i;
      energy_index[i].energy = energies[i];
    }
    /* sort energies */
    qsort(energy_index, n, sizeof(ENERGYINDEX), compare_energy);

    /* construct new training data and write to file */
    printf("Writing data to temp.dat...\n");
    fp = fopen("temp.dat", "w");
    ftrain = fopen(trainfile, "r");
    if(ftrain == NULL)
    {
      printf("Cannot open file %s to read\n", trainfile);
      exit(1);
    }
    ftest = fopen(testfile, "r");
    if(ftest == NULL)
    {
      printf("Cannot open file %s to read\n", testfile);
      exit(1);
    }

    /* positive samples from training data file */
    fscanf(ftrain, "%d\n", &ntrain);
    count_pos = ntrain/2;
    fscanf(ftest, "%d\n", &ntest);
    if(ntest < ntrain/2)
      n = ntest + ntrain/2;
    else
      n = ntrain;
    count_neg = n - count_pos;

    fprintf(fp, "%d\n", n);
    for(i = 0; i < ntrain; i++)
    {
      fgets(line, BUFFLE_SIZE, ftrain);
      sscanf(line, "%d", &object_label);
      if(object_label == 1)
        fputs(line, fp);
      else
        break;
    }
    fclose(ftrain);

    /* negative samples from hard negative examples */
    count = 0;
    for(i = 0; i < ntest; i++)
    {
      fgets(line, BUFFLE_SIZE, ftest);
      for(j = 0; j < count_neg; j++)
      {
        if(i == energy_index[j].index)
        {
          count++;
          fputs(line, fp);
          break;
        }
      }
      if(count >= count_neg)
        break;
    }
    fclose(ftest);
    fclose(fp);
    free(energy_index);
    printf("Done\n");
  }
  MPI_Barrier(MPI_COMM_WORLD);

  free(energy);
  free(energies);
  free_struct_sample(testsample);
}
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);

}