Exemplo n.º 1
0
int main (int argc, char* argv[])
{
  MODEL *model; 

  read_input_parameters(argc,argv,modelfile,outfile, &verbosity, &format);

  if (format) {
    model=read_binary_model(modelfile);
  } else {
    model=read_model(modelfile);
    if(model->kernel_parm.kernel_type == 0) { /* linear kernel */
        /* compute weight vector */
        add_weight_vector_to_linear_model(model);
    }
  }
    if(model->kernel_parm.kernel_type == 0) { /* linear kernel */
        FILE* modelfl = fopen (outfile, "wb");
        if (modelfl==NULL)
        { perror (modelfile); exit (1); }

        if (verbosity > 1)
            fprintf(modelfl,"B=%.32g\n",model->b);
        long i=0;
        for (i= 0; i< model->totwords; ++i) 
            fprintf(modelfl,"%.32g\n",model->lin_weights[i]);
    } else {
        fprintf(stderr,"No output besides linear models\n");
    }
  free_model(model,1);
  return(0);
}
Exemplo n.º 2
0
int main(int argc, char* argv[]) {
    DOC *X;
	double *Y;
	char trainfile[1024];
    int ntestfiles;
	char **testfiles=NULL;
    int SAMPLE_SIZE = 1000;
    
	
	long totdoc;
	long kernel_cache_size;
	
	LEARN_PARM learn_parm;
	KERNEL_PARM kernel_parm;
    
	/* read input parameters */	
	
  read_input_parameters(argc,argv,trainfile, &testfiles, &ntestfiles ,&verbosity,
                        &kernel_cache_size,&learn_parm, &kernel_parm);
  //
  my_read_examples(trainfile, &X, &Y, &totdoc, kernel_parm);
  dag_tree_kernel_vs_dag_size(X, Y, totdoc, SAMPLE_SIZE, kernel_parm);

  execvp(argv[0], argv);
}
Exemplo n.º 3
0
int main (int argc, char* argv[])
{  
  SAMPLE sample;  /* training sample */
  LEARN_PARM learn_parm;
  KERNEL_PARM kernel_parm;
  STRUCT_LEARN_PARM struct_parm;
  STRUCTMODEL structmodel;
  int alg_type;

  svm_struct_learn_api_init(argc,argv);

  read_input_parameters(argc,argv,trainfile,modelfile,&verbosity,
			&struct_verbosity,&struct_parm,&learn_parm,
			&kernel_parm,&alg_type);

  if(struct_verbosity>=1) {
    printf("Reading training examples..."); fflush(stdout);
  }
  /* read the training examples */
  sample=read_struct_examples(trainfile,&struct_parm);
  if(struct_verbosity>=1) {
    printf("done\n"); fflush(stdout);
  }
  
  /* Do the learning and return structmodel. */
  if(alg_type == 0)
    svm_learn_struct(sample,&struct_parm,&learn_parm,&kernel_parm,&structmodel,NSLACK_ALG);
  else if(alg_type == 1)
    svm_learn_struct(sample,&struct_parm,&learn_parm,&kernel_parm,&structmodel,NSLACK_SHRINK_ALG);
  else if(alg_type == 2)
    svm_learn_struct_joint(sample,&struct_parm,&learn_parm,&kernel_parm,&structmodel,ONESLACK_PRIMAL_ALG);
  else if(alg_type == 3)
    svm_learn_struct_joint(sample,&struct_parm,&learn_parm,&kernel_parm,&structmodel,ONESLACK_DUAL_ALG);
  else if(alg_type == 4)
    svm_learn_struct_joint(sample,&struct_parm,&learn_parm,&kernel_parm,&structmodel,ONESLACK_DUAL_CACHE_ALG);
  else if(alg_type == 9)
    svm_learn_struct_joint_custom(sample,&struct_parm,&learn_parm,&kernel_parm,&structmodel);
  else
    exit(1);

  /* Warning: The model contains references to the original data 'docs'.
     If you want to free the original data, and only keep the model, you 
     have to make a deep copy of 'model'. */
  if(struct_verbosity>=1) {
    printf("Writing learned model...");fflush(stdout);
  }
  write_struct_model(modelfile,&structmodel,&struct_parm);
  if(struct_verbosity>=1) {
    printf("done\n");fflush(stdout);
  }

  free_struct_sample(sample);
  free_struct_model(structmodel);

  svm_struct_learn_api_exit();

  return 0;
}
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);

}
Exemplo n.º 5
0
int main (int argc, char* argv[])
{  
  SAMPLE sample;  /* training sample */
  LEARN_PARM learn_parm;
  KERNEL_PARM kernel_parm;
  STRUCT_LEARN_PARM struct_parm;
  STRUCTMODEL structmodel;

  /* Allow the API to perform whatever initialization is required. */
  api_initialize(argv[0]);

  read_input_parameters(argc,argv,trainfile,modelfile,&verbosity,
			&struct_verbosity,&struct_parm,&learn_parm,
			&kernel_parm);

  if(struct_verbosity>=1) {
    printf("Reading training examples..."); fflush(stdout);
  }
  /* read the training examples */
  sample=read_struct_examples(trainfile,&struct_parm);
  if(struct_verbosity>=1) {
    printf("done\n"); fflush(stdout);
  }
  
  /* Do the learning and return structmodel. */
  svm_learn_struct(sample,&struct_parm,&learn_parm,&kernel_parm,&structmodel);
  
  /* Warning: The model contains references to the original data 'docs'.
     If you want to free the original data, and only keep the model, you 
     have to make a deep copy of 'model'. */
  if(struct_verbosity>=1) {
    printf("Writing learned model...");fflush(stdout);
  }
  write_struct_model(modelfile,&structmodel,&struct_parm);
  if(struct_verbosity>=1) {
    printf("done\n");fflush(stdout);
  }

  free_struct_sample(sample);
  free_struct_model(structmodel);

  /* Allow the API to perform whatever cleanup is required. */
  api_finalize();

  return 0;
}
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);

}
Exemplo n.º 7
0
int main(int argc, char *argv[])
{
    struct State S;

    read_network_parameters(argc, argv, &S);
    read_input_parameters(&S);
    initialize_network_2D(&S);
    initialize_circular_buffer(&S);

    simulate_single_trajectory_2D(&S);

    save_snapshots_firing_activity_2D(&S);
    /* save_inputs(&S); */

    free_network_variables(&S);
    free_input_variables(&S);
    return 0;
}
int main (int argc, char* argv[])
{  
  SAMPLE sample;  /* training sample */
  LEARN_PARM learn_parm;
  KERNEL_PARM kernel_parm;
  STRUCT_LEARN_PARM struct_parm;
  STRUCTMODEL structmodel;
  int alg_type;

  svm_struct_learn_api_init(argc,argv);

  read_input_parameters(argc,argv,trainfile,modelfile,&verbosity,
			&struct_verbosity,&struct_parm,&learn_parm,
			&kernel_parm,&alg_type);

  if(struct_verbosity>=1) {
    //verbose = true;
    printf("Reading training examples..."); fflush(stdout);
  }
  /* read the training examples */
  sample=read_struct_examples(trainfile,&struct_parm);
  if(struct_verbosity>=1) {
    printf("done\n"); fflush(stdout);
  }

  string config_tmp;
  bool update_loss_function = false;
  if(Config::Instance()->getParameter("update_loss_function", config_tmp)) {
    update_loss_function = config_tmp.c_str()[0] == '1';
  }
  printf("[Main] update_loss_function=%d\n", (int)update_loss_function);
  if(!update_loss_function) {
    printf("update_loss_function should be true\n");
    exit(-1);
  }  

  eUpdateType updateType = UPDATE_NODE_EDGE;
  if(Config::Instance()->getParameter("update_type", config_tmp)) {
    updateType = (eUpdateType)atoi(config_tmp.c_str());
  }
  printf("[Main] update_type=%d\n", (int)updateType);

  mkdir(loss_dir, 0777);

  // check if parameter vector files exist
  const char* parameter_vector_dir = "parameter_vector";
  int idx = 0;
  string parameter_vector_dir_last = findLastFile(parameter_vector_dir, "", &idx);
  string parameter_vector_file_pattern = parameter_vector_dir_last + "/iteration_";
  int idx_1 = 1;
  string parameter_vector_file_last = findLastFile(parameter_vector_file_pattern, ".txt", &idx_1);
  printf("[Main] Checking parameter vector file %s\n", parameter_vector_file_last.c_str());

  // vectors used to store RF weights
  vector<double>* alphas = new vector<double>[sample.n];
  vector<double>* alphas_edge = 0;
  if(updateType == UPDATE_NODE_EDGE) {
    alphas_edge = new vector<double>[sample.n];
  }
  int alphas_idx = 0;

  if(fileExists("alphas.txt") && fileExists(parameter_vector_file_last)) {

    // Loading alpha coefficients
    ifstream ifs("alphas.txt");
    string line;
    int lineIdx = 0;
    while(lineIdx < sample.n && getline(ifs, line)) {
      vector<string> tokens;
      splitString(line, tokens);
      for(vector<string>::iterator it = tokens.begin();
          it != tokens.end(); ++it) {
        alphas[lineIdx].push_back(atoi(it->c_str()));
      }
      ++lineIdx;
    }
    ifs.close();
    if(lineIdx > 0) {
      alphas_idx = alphas[0].size();
    }

    // Loading parameters
    printf("[Main] Found parameter vector file %s\n", parameter_vector_file_last.c_str());
    struct_parm.ssvm_iteration = idx + 1;
    update_output_dir(struct_parm.ssvm_iteration);

    //EnergyParam param(parameter_vector_file_last.c_str());
    //updateCoeffs(sample, param, struct_parm, updateType, alphas, alphas_idx);
    //alphas_idx = 1;

  } else {
    struct_parm.ssvm_iteration = 0;

    // insert alpha coefficients for first iteration
    for(int i = 0; i < sample.n; ++i) {
      alphas[i].push_back(1);
    }

    ofstream ofs("alphas.txt", ios::app);
    int i = 0;
    for(; i < sample.n - 1; ++i) {
      ofs << alphas[i][alphas_idx] << " ";
    }
    if(i < sample.n) {
      ofs << alphas[i][alphas_idx];
    }
    ofs << endl;
    ofs.close();

    // edges
    for(int i = 0; i < sample.n; ++i) {
      alphas_edge[i].push_back(1);
    }

    ofstream ofse("alphas_edge.txt", ios::app);
    i = 0;
    for(; i < sample.n - 1; ++i) {
      ofse << alphas_edge[i][alphas_idx] << " ";
    }
    if(i < sample.n) {
      ofse << alphas_edge[i][alphas_idx];
    }
    ofse << endl;
    ofse.close();

    ++alphas_idx;
  }

  const int nMaxIterations = 5;
  bool bIterate = true;
  do {

    printf("-----------------------------------------SSVM-ITERATION-%d-START\n",
           struct_parm.ssvm_iteration);

    struct_parm.iterationId = 1;

    /* Do the learning and return structmodel. */
    if(alg_type == 0)
      svm_learn_struct(sample,&struct_parm,&learn_parm,&kernel_parm,&structmodel,NSLACK_ALG);
    else if(alg_type == 1)
      svm_learn_struct(sample,&struct_parm,&learn_parm,&kernel_parm,&structmodel,NSLACK_SHRINK_ALG);
    else if(alg_type == 2)
      svm_learn_struct_joint(sample,&struct_parm,&learn_parm,&kernel_parm,&structmodel,ONESLACK_PRIMAL_ALG);
    else if(alg_type == 3)
      svm_learn_struct_joint(sample,&struct_parm,&learn_parm,&kernel_parm,&structmodel,ONESLACK_DUAL_ALG);
    else if(alg_type == 4)
      svm_learn_struct_joint(sample,&struct_parm,&learn_parm,&kernel_parm,&structmodel,ONESLACK_DUAL_CACHE_ALG);
    else if(alg_type == 9)
      svm_learn_struct_joint_custom(sample,&struct_parm,&learn_parm,&kernel_parm,&structmodel);
    else
      exit(1);

    char _modelfile[BUFFER_SIZE];
    //sprintf(_modelfile, "%s_%d", modelfile, struct_parm.ssvm_iteration);
    sprintf(_modelfile, "%s_%d", modelfile, struct_parm.ssvm_iteration);
    printf("[Main] Writing learned model to %s\n", _modelfile);
    write_struct_model(_modelfile, &structmodel, &struct_parm);

    // Run inference on training data and increase loss for misclassified points
    printf("[Main] Loading learned model to %s\n", _modelfile);
    EnergyParam param(_modelfile);

    updateCoeffs(sample, param, struct_parm, updateType, alphas, alphas_edge,
                 struct_parm.ssvm_iteration + 1);

    ofstream ofs("alphas.txt", ios::app);
    int i = 0;
    for(; i < sample.n - 1; ++i) {
      ofs << alphas[i][alphas_idx] << " ";
    }
    if(i < sample.n) {
      ofs << alphas[i][alphas_idx];
    }
    ofs << endl;
    ofs.close();

    ofstream ofse("alphas_edge.txt", ios::app);
    i = 0;
    for(; i < sample.n - 1; ++i) {
      ofse << alphas_edge[i][alphas_idx] << " ";
    }
    if(i < sample.n) {
      ofse << alphas_edge[i][alphas_idx];
    }
    ofse << endl;
    ofse.close();

    ++alphas_idx;

    printf("-----------------------------------------SSVM-ITERATION-%d-END\n",
           struct_parm.ssvm_iteration);

    ++struct_parm.ssvm_iteration;

    bIterate = (nMaxIterations == -1 || struct_parm.ssvm_iteration < nMaxIterations);

  } while(bIterate);

  // Output final segmentation for all examples
  long nExamples = sample.n;
  int nRFs = struct_parm.ssvm_iteration;
  double* lossPerLabel = 0;
  labelType* groundTruthLabels = 0;

  for(int i = 0; i < nExamples; i++) { /*** example loop ***/

    Slice_P* slice = sample.examples[i].x.slice;
    Feature* feature = sample.examples[i].x.feature;
    //map<sidType, nodeCoeffType>* nodeCoeffs = sample.examples[i].x.nodeCoeffs;
    //map<sidType, edgeCoeffType>* edgeCoeffs = sample.examples[i].x.edgeCoeffs;
    map<sidType, nodeCoeffType>* nodeCoeffs = 0;
    map<sidType, edgeCoeffType>* edgeCoeffs = 0;
    int nNodes = slice->getNbSupernodes();

    stringstream soutPb;
    soutPb << loss_dir;
    soutPb << "pb_";
    soutPb << getNameFromPathWithoutExtension(slice->getName());
    soutPb << "_";
    soutPb << "combined";
    //soutPb << setw(5) << setfill('0') << ssvm_iteration;
    soutPb << ".tif";
    printf("[Main] Exporting %s\n", soutPb.str().c_str());

    labelType* inferredLabels =
      computeCombinedLabels(slice, feature, groundTruthLabels,
                            lossPerLabel, nRFs, alphas, i,
                            nodeCoeffs, edgeCoeffs, soutPb.str().c_str());

    stringstream sout;
    sout << loss_dir;
    sout << getNameFromPathWithoutExtension(slice->getName());
    sout << "_";
    sout << "combined";
    //sout << setw(5) << setfill('0') << ssvm_iteration;
    sout << ".tif";
    printf("[Main] Exporting %s\n", sout.str().c_str());
    slice->exportOverlay(sout.str().c_str(), inferredLabels);

    stringstream soutBW;
    soutBW << loss_dir;
    soutBW << getNameFromPathWithoutExtension(slice->getName());
    soutBW << "_";
    soutBW << "combined";
    //soutBW << setw(5) << setfill('0') << ssvm_iteration;
    soutBW << "_BW.tif";
    printf("[Main] Exporting %s\n", soutBW.str().c_str());        
    slice->exportSupernodeLabels(soutBW.str().c_str(),
                                 struct_parm.nClasses,
                                 inferredLabels, nNodes,
                                 &(struct_parm.labelToClassIdx));

    delete[] inferredLabels;
  }

  free_struct_sample(sample);
  free_struct_model(structmodel);

  svm_struct_learn_api_exit();

  return 0;
}
Exemplo n.º 9
0
int main_classify (int argc, char* argv[])
{
  DOC *doc;   /* test example */
  WORDSVM *words;
  long max_docs,max_words_doc,lld;
  long totdoc=0,queryid,slackid;
  long correct=0,incorrect=0,no_accuracy=0;
  long res_a=0,res_b=0,res_c=0,res_d=0,wnum,pred_format;
  long j;
  double t1,runtime=0;
  double dist,doc_label,costfactor;
  char *line,*comment; 
  FILE *predfl,*docfl;
  MODEL *model; 

  read_input_parameters(argc,argv,docfile,modelfile,predictionsfile,
			&verbosity,&pred_format);

  nol_ll(docfile,&max_docs,&max_words_doc,&lld); /* scan size of input file */
  max_words_doc+=2;
  lld+=2;

  line = (char *)my_malloc(sizeof(char)*lld);
  words = (WORDSVM *)my_malloc(sizeof(WORDSVM)*(max_words_doc+10));

  model=read_model(modelfile);

  if(model->kernel_parm.kernel_type == 0) { /* linear kernel */
    /* compute weight vector */
    add_weight_vector_to_linear_model(model);
  }
  
  if(verbosity>=2) {
    printf("Classifying test examples.."); fflush(stdout);
  }

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

  while((!feof(docfl)) && fgets(line,(int)lld,docfl)) {
    if(line[0] == '#') continue;  /* line contains comments */
    parse_document(line,words,&doc_label,&queryid,&slackid,&costfactor,&wnum,
		   max_words_doc,&comment);
    totdoc++;
    if(model->kernel_parm.kernel_type == 0) {   /* linear kernel */
      for(j=0;(words[j]).wnum != 0;j++) {  /* Check if feature numbers   */
	if((words[j]).wnum>model->totwords) /* are not larger than in     */
	  (words[j]).wnum=0;               /* model. Remove feature if   */
      }                                        /* necessary.                 */
      doc = create_example(-1,0,0,0.0,create_svector(words,comment,1.0));
      t1=get_runtime();
      dist=classify_example_linear(model,doc);
      runtime+=(get_runtime()-t1);
      free_example(doc,1);
    }
    else {                             /* non-linear kernel */
      doc = create_example(-1,0,0,0.0,create_svector(words,comment,1.0));
      t1=get_runtime();
      dist=classify_example(model,doc);
      runtime+=(get_runtime()-t1);
      free_example(doc,1);
    }
    if(dist>0) {
      if(pred_format==0) { /* old weired output format */
	fprintf(predfl,"%.8g:+1 %.8g:-1\n",dist,-dist);
      }
      if(doc_label>0) correct++; else incorrect++;
      if(doc_label>0) res_a++; else res_b++;
    }
    else {
      if(pred_format==0) { /* old weired output format */
	fprintf(predfl,"%.8g:-1 %.8g:+1\n",-dist,dist);
      }
      if(doc_label<0) correct++; else incorrect++;
      if(doc_label>0) res_c++; else res_d++;
    }
    if(pred_format==1) { /* output the value of decision function */
      fprintf(predfl,"%.8g\n",dist);
    }
    if((int)(0.01+(doc_label*doc_label)) != 1) 
      { no_accuracy=1; } /* test data is not binary labeled */
    if(verbosity>=2) {
      if(totdoc % 100 == 0) {
	printf("%ld..",totdoc); fflush(stdout);
      }
    }
  }  
  free(line);
  free(words);
  free_model(model,1);

  if(verbosity>=2) {
    printf("done\n");

/*   Note by Gary Boone                     Date: 29 April 2000        */
/*      o Timing is inaccurate. The timer has 0.01 second resolution.  */
/*        Because classification of a single vector takes less than    */
/*        0.01 secs, the timer was underflowing.                       */
    printf("Runtime (without IO) in cpu-seconds: %.2f\n",
	   (float)(runtime/100.0));
    
  }
  if((!no_accuracy) && (verbosity>=1)) {
    printf("Accuracy on test set: %.2f%% (%ld correct, %ld incorrect, %ld total)\n",(float)(correct)*100.0/totdoc,correct,incorrect,totdoc);
    printf("Precision/recall on test set: %.2f%%/%.2f%%\n",(float)(res_a)*100.0/(res_a+res_b),(float)(res_a)*100.0/(res_a+res_c));
  }

  return(0);
}
Exemplo n.º 10
0
int main (int argc, char* argv[])
{  
  DOC *docs;  /* training examples */
  long max_docs,max_words_doc;
  long totwords,totdoc,ll,i;
  long kernel_cache_size;
  double *target;
  KERNEL_CACHE kernel_cache;
  LEARN_PARM learn_parm;
  KERNEL_PARM kernel_parm;
  MODEL model;

  read_input_parameters(argc,argv,docfile,modelfile,&verbosity,
			&kernel_cache_size,&learn_parm,&kernel_parm);

  if(verbosity>=1) {
    printf("Scanning examples..."); fflush(stdout);
  }
  nol_ll(docfile,&max_docs,&max_words_doc,&ll); /* scan size of input file */
  max_words_doc+=10;
  ll+=10;
  max_docs+=2;
  if(verbosity>=1) {
    printf("done\n"); fflush(stdout);
  }

  docs = (DOC *)my_malloc(sizeof(DOC)*max_docs);         /* feature vectors */
  target = (double *)my_malloc(sizeof(double)*max_docs); /* target values */
 //printf("\nMax docs: %ld, approximated number of feature occurences %ld, maximal length of a line %ld\n\n",max_docs,max_words_doc,ll);
  read_documents(docfile,docs,target,max_words_doc,ll,&totwords,&totdoc,&kernel_parm);
  printf("\nNumber of examples: %ld, linear space size: %ld\n\n",totdoc,totwords);
 
 //if(kernel_parm.kernel_type==5) totwords=totdoc; // The number of features is proportional to the number of parse-trees, i.e. totdoc 
  				                 // or should we still use totwords to approximate svm_maxqpsize for the Tree Kernel (see hideo.c) ???????

  if(kernel_parm.kernel_type == LINEAR) { /* don't need the cache */
    if(learn_parm.type == CLASSIFICATION) {
      svm_learn_classification(docs,target,totdoc,totwords,&learn_parm,
			       &kernel_parm,NULL,&model);
    }
    else if(learn_parm.type == REGRESSION) {
      svm_learn_regression(docs,target,totdoc,totwords,&learn_parm,
			   &kernel_parm,NULL,&model);
    }
    else if(learn_parm.type == RANKING) {
      svm_learn_ranking(docs,target,totdoc,totwords,&learn_parm,
			&kernel_parm,NULL,&model);
    }
  }
  else {
    if(learn_parm.type == CLASSIFICATION) {
      /* Always get a new kernel cache. It is not possible to use the
         same cache for two different training runs */
      kernel_cache_init(&kernel_cache,totdoc,kernel_cache_size);
      svm_learn_classification(docs,target,totdoc,totwords,&learn_parm,
			       &kernel_parm,&kernel_cache,&model);
      /* Free the memory used for the cache. */
      kernel_cache_cleanup(&kernel_cache);
    }
    else if(learn_parm.type == REGRESSION) {
      /* Always get a new kernel cache. It is not possible to use the
         same cache for two different training runs */
      kernel_cache_init(&kernel_cache,2*totdoc,kernel_cache_size);
      svm_learn_regression(docs,target,totdoc,totwords,&learn_parm,
			   &kernel_parm,&kernel_cache,&model);
      /* Free the memory used for the cache. */
      kernel_cache_cleanup(&kernel_cache);
    }
    else if(learn_parm.type == RANKING) {
      printf("Learning rankings is not implemented for non-linear kernels in this version!\n");
      exit(1);
    }
    else if(learn_parm.type == PERCEPTRON) {
			perceptron_learn_classification(docs,target,totdoc,totwords,&learn_parm,
			&kernel_parm,&kernel_cache,&model,modelfile);
    }
	    else if(learn_parm.type == PERCEPTRON_BATCH) {
			batch_perceptron_learn_classification(docs,target,totdoc,totwords,&learn_parm,
			&kernel_parm,kernel_cache_size,&model);
    }

  }

  /* Warning: The model contains references to the original data 'docs'.
     If you want to free the original data, and only keep the model, you 
     have to make a deep copy of 'model'. */
  write_model(modelfile,&model);

  free(model.supvec);
  free(model.alpha);
  free(model.index);
  
   for(i=0;i<totdoc;i++){
     freeExample(&docs[i]);
   }
  
  free(docs);
  free(target);

  return(0);
}
Exemplo n.º 11
0
int main (int argc, char* argv[])
{
  int o, i, j, cad_num;
  char filename[256];
  CAD **cads, *cad;
  SAMPLE sample, sample_part;  /* training sample */
  LEARN_PARM learn_parm;
  KERNEL_PARM kernel_parm;
  STRUCT_LEARN_PARM struct_parm;
  STRUCTMODEL structmodel;
  int alg_type;
  int rank;

  MPI_Init(&argc, &argv);
  MPI_Comm_rank(MPI_COMM_WORLD, &rank);

  /* select GPU */
  select_gpu(rank);

  svm_struct_learn_api_init(argc,argv);

  read_input_parameters(argc, argv, trainfile, cadfile, testfile, &verbosity,
			&struct_verbosity, &struct_parm, &learn_parm,
			&kernel_parm, &alg_type);

  /* read cad models */
  cads = read_cad_model(cadfile, &cad_num, 0, &struct_parm);

  /* if cad_num == 1, train the final model */
  if(cad_num == 1)
  {
    printf("Train the hierarchical model\n");

    /* set the cad model for structmodel */
    structmodel.cad_num = 1;
    structmodel.cads = cads;

    printf("Read training samples\n");
    sample = read_struct_examples(trainfile, &struct_parm, &structmodel);
    printf("Read training samples done\n");

    if(struct_parm.is_root == 1 || struct_parm.is_aspectlet == 1)
    {
      /* first train weights for root parts */
      printf("Train root models\n");
      cad = cads[0];

      struct_parm.cad_index = 0;
      /* for each part */
      for(i = 0; i < cad->part_num; i++)
      {
        /* choose what parts to train */
        if((cad->roots[i] == -1 && struct_parm.is_root == 1) || (cad->roots[i] == 1 && struct_parm.is_aspectlet == 1))
        {
          printf("Train part %d\n", i);
          /* training iteration index */
          struct_parm.iter = 0;

          struct_parm.part_index = i;
          /* select training samples for part */
          if(rank == 0)
            select_examples_part(trainfile, sample, cad, 0, i);
          MPI_Barrier(MPI_COMM_WORLD);
          sample_part = read_struct_examples("temp_part.dat", &struct_parm, &structmodel);

          /* train the root template */
          struct_parm.deep = 0;

          /* Do the learning and return structmodel. */
          if(alg_type == 1)
            svm_learn_struct(sample_part, &struct_parm, &learn_parm, &kernel_parm, &structmodel);
          else if(alg_type == 2)
            svm_learn_struct_joint(sample_part, &struct_parm, &learn_parm, &kernel_parm, &structmodel, PRIMAL_ALG);
          else if(alg_type == 3)
            svm_learn_struct_joint(sample_part, &struct_parm, &learn_parm, &kernel_parm, &structmodel, DUAL_ALG);
          else if(alg_type == 4)
            svm_learn_struct_joint(sample_part, &struct_parm, &learn_parm, &kernel_parm, &structmodel, DUAL_CACHE_ALG);
          else
            exit(1);

          /* if aspectlet, train the subtree */
          if(cad->roots[i] == 1 && struct_parm.is_aspectlet == 1)
          {
            printf("Train subtree for part %d\n", i);
            struct_parm.deep = 1;
            struct_parm.iter++;

            /* select training samples for part */
            if(rank == 0)
              select_examples_part(trainfile, sample, cad, 0, i);
            MPI_Barrier(MPI_COMM_WORLD);
            free_struct_sample(sample_part);
            sample_part = read_struct_examples("temp_part.dat", &struct_parm, &structmodel);

            free_struct_model(structmodel);
            /* set the cad model for structmodel */
            structmodel.cad_num = 1;
            structmodel.cads = cads;

            if(alg_type == 1)
              svm_learn_struct(sample_part, &struct_parm, &learn_parm, &kernel_parm, &structmodel);
            else if(alg_type == 2)
              svm_learn_struct_joint(sample_part, &struct_parm, &learn_parm, &kernel_parm, &structmodel, PRIMAL_ALG);
            else if(alg_type == 3)
              svm_learn_struct_joint(sample_part, &struct_parm, &learn_parm, &kernel_parm, &structmodel, DUAL_ALG);
            else if(alg_type == 4)
              svm_learn_struct_joint(sample_part, &struct_parm, &learn_parm, &kernel_parm, &structmodel, DUAL_CACHE_ALG);
            else
              exit(1);
          }

          /* data mining hard examples */
          for(j = 0; j < struct_parm.hard_negative; j++)
          {
            /* increase iteration number */
            struct_parm.iter++;

            data_mining_hard_examples("temp_part.dat", testfile, &struct_parm, &structmodel);

            /* read the training examples */
            if(struct_verbosity>=1) 
            {
              printf("Reading training examples...");
              fflush(stdout);
            }
            free_struct_sample(sample_part);
            sample_part = read_struct_examples("temp.dat", &struct_parm, &structmodel);
            if(struct_verbosity>=1) 
            {
              printf("done\n");
              fflush(stdout);
            }
 
            /* Do the learning and return structmodel. */
            free_struct_model(structmodel);

            /* set the cad model for structmodel */
            structmodel.cad_num = 1;
            structmodel.cads = cads;

            if(alg_type == 1)
              svm_learn_struct(sample_part, &struct_parm, &learn_parm, &kernel_parm, &structmodel);
            else if(alg_type == 2)
              svm_learn_struct_joint(sample_part, &struct_parm, &learn_parm, &kernel_parm, &structmodel, PRIMAL_ALG);
            else if(alg_type == 3)
              svm_learn_struct_joint(sample_part, &struct_parm, &learn_parm, &kernel_parm, &structmodel, DUAL_ALG);
            else if(alg_type == 4)
              svm_learn_struct_joint(sample_part, &struct_parm, &learn_parm, &kernel_parm, &structmodel, DUAL_CACHE_ALG);
            else
              exit(1);
          }

          /* save constraints for training the full model */
          if(rank == 0)
            save_constraints(&struct_parm, &structmodel);
          MPI_Barrier(MPI_COMM_WORLD);

          free_struct_sample(sample_part);
          free_struct_model(structmodel);

          /* set the cad model for structmodel */
          structmodel.cad_num = 1;
          structmodel.cads = cads;
        }
      } /* end for each part */

      if(rank == 0)
      {
        write_constraints(struct_parm.cset, &struct_parm);
        free(struct_parm.cset.rhs); 
        for(i = 0; i < struct_parm.cset.m; i++) 
          free_example(struct_parm.cset.lhs[i], 1);
        free(struct_parm.cset.lhs);
      }
      MPI_Barrier(MPI_COMM_WORLD);
    }  /* end if is_root == 1 */

    /* train the full model */
    printf("Train the full model\n");
    struct_parm.iter = 0;
    struct_parm.cad_index = -1;
    struct_parm.part_index = -1;
    struct_parm.deep = 1;

    /* Do the learning and return structmodel. */
    if(alg_type == 1)
      svm_learn_struct(sample, &struct_parm, &learn_parm, &kernel_parm, &structmodel);
    else if(alg_type == 2)
      svm_learn_struct_joint(sample, &struct_parm, &learn_parm, &kernel_parm, &structmodel, PRIMAL_ALG);
    else if(alg_type == 3)
      svm_learn_struct_joint(sample, &struct_parm, &learn_parm, &kernel_parm, &structmodel, DUAL_ALG);
    else if(alg_type == 4)
      svm_learn_struct_joint(sample, &struct_parm, &learn_parm, &kernel_parm, &structmodel, DUAL_CACHE_ALG);
    else
      exit(1);
  
    /* data mining hard examples */
    while(struct_parm.hard_negative > 0)
    {
      /* increase iteration number */
      struct_parm.iter++;

      data_mining_hard_examples(trainfile, testfile, &struct_parm, &structmodel);

      /* read the training examples */
      if(struct_verbosity>=1) 
      {
        printf("Reading training examples...");
        fflush(stdout);
      }
      free_struct_sample(sample);
      sample = read_struct_examples("temp.dat", &struct_parm, &structmodel);
      if(struct_verbosity>=1) 
      {
        printf("done\n");
        fflush(stdout);
      }
 
      /* Do the learning and return structmodel. */
      free_struct_model(structmodel);

      /* set the cad model for structmodel */
      structmodel.cad_num = 1;
      structmodel.cads = cads;

      if(alg_type == 1)
        svm_learn_struct(sample, &struct_parm, &learn_parm, &kernel_parm, &structmodel);
      else if(alg_type == 2)
        svm_learn_struct_joint(sample, &struct_parm, &learn_parm, &kernel_parm, &structmodel, PRIMAL_ALG);
      else if(alg_type == 3)
        svm_learn_struct_joint(sample, &struct_parm, &learn_parm, &kernel_parm, &structmodel, DUAL_ALG);
      else if(alg_type == 4)
        svm_learn_struct_joint(sample, &struct_parm, &learn_parm, &kernel_parm, &structmodel, DUAL_CACHE_ALG);
      else
        exit(1);

      struct_parm.hard_negative--;
    }

    if(rank == 0)
    {
      if(struct_verbosity >= 1) 
      {
        printf("Writing learned model...");
        fflush(stdout);
      }
      sprintf(modelfile, "%s.mod", struct_parm.cls);
      write_struct_model(modelfile, &structmodel, &struct_parm);
      if(struct_verbosity>=1) 
      {
        printf("done\n");
        fflush(stdout);
      }
    }
    MPI_Barrier(MPI_COMM_WORLD);

    free_struct_sample(sample);
    free_struct_model(structmodel);
  }

  /* train weights for aspectlets */
  /* start with the second aspectlet, the first one is the whole object */
  for(o = 1; o < cad_num; o++)
  {
    printf("Train aspectlet %d\n", o);
    cad = cads[o];

    /* set the cad model for structmodel */
    structmodel.cad_num = 1;
    structmodel.cads = &cad;

    /* training iteration index */
    struct_parm.iter = 0;
    struct_parm.cad_index = -1;
    struct_parm.part_index = -1;
    struct_parm.deep = 1;

    /* select training samples for the aspectlet */
    if(rank == 0)
      select_examples_aspectlet(trainfile, cads, o);
    MPI_Barrier(MPI_COMM_WORLD);
    printf("Read training samples\n");
    sample_part = read_struct_examples("temp_part.dat", &struct_parm, &structmodel);
    printf("Read training samples done\n");

    /* Do the learning and return structmodel. */
    if(alg_type == 1)
      svm_learn_struct(sample_part, &struct_parm, &learn_parm, &kernel_parm, &structmodel);
    else if(alg_type == 2)
      svm_learn_struct_joint(sample_part, &struct_parm, &learn_parm, &kernel_parm, &structmodel, PRIMAL_ALG);
    else if(alg_type == 3)
      svm_learn_struct_joint(sample_part, &struct_parm, &learn_parm, &kernel_parm, &structmodel, DUAL_ALG);
    else if(alg_type == 4)
      svm_learn_struct_joint(sample_part, &struct_parm, &learn_parm, &kernel_parm, &structmodel, DUAL_CACHE_ALG);
    else
      exit(1);

    /* data mining hard examples */
    for(i = 0; i < struct_parm.hard_negative; i++)
    {
      /* increase iteration number */
      struct_parm.iter++;

      data_mining_hard_examples("temp_part.dat", testfile, &struct_parm, &structmodel);

      /* read the training examples */
      if(struct_verbosity>=1) 
      {
        printf("Reading training examples...");
        fflush(stdout);
      }
      free_struct_sample(sample_part);
      sample_part = read_struct_examples("temp.dat", &struct_parm, &structmodel);
      if(struct_verbosity>=1) 
      {
        printf("done\n");
        fflush(stdout);
      }
 
      /* Do the learning and return structmodel. */
      free_struct_model(structmodel);

      /* set the cad model for structmodel */
      structmodel.cad_num = 1;
      structmodel.cads = &cad;

      if(alg_type == 1)
        svm_learn_struct(sample_part, &struct_parm, &learn_parm, &kernel_parm, &structmodel);
      else if(alg_type == 2)
        svm_learn_struct_joint(sample_part, &struct_parm, &learn_parm, &kernel_parm, &structmodel, PRIMAL_ALG);
      else if(alg_type == 3)
        svm_learn_struct_joint(sample_part, &struct_parm, &learn_parm, &kernel_parm, &structmodel, DUAL_ALG);
      else if(alg_type == 4)
        svm_learn_struct_joint(sample_part, &struct_parm, &learn_parm, &kernel_parm, &structmodel, DUAL_CACHE_ALG);
      else
        exit(1);
    }

    if(rank == 0)
    {
      if(struct_verbosity>=1) 
      {
        printf("Writing learned model...");
        fflush(stdout);
      }
      sprintf(modelfile, "%s_cad%03d.mod", struct_parm.cls, o);
      write_struct_model(modelfile, &structmodel, &struct_parm);
      if(struct_verbosity>=1) 
      {
        printf("done\n");
        fflush(stdout);
      }
    }
    MPI_Barrier(MPI_COMM_WORLD);

    free_struct_sample(sample_part);
    free_struct_model(structmodel);
  }  /* end for each cad model */

  for(i = 0; i < cad_num; i++)
    destroy_cad(cads[i]);
  free(cads);

  svm_struct_learn_api_exit();

  MPI_Finalize();

  return 0;
}
Exemplo n.º 12
0
/* The real main function. */
int main (int argc, char* argv[])
{
  DOC **docs;  /* training examples */
  long totwords,totdoc,i;
  double *target;
  double *alpha_in=NULL;
  KERNEL_CACHE *kernel_cache;
  LEARN_PARM learn_parm;
  KERNEL_PARM kernel_parm;
  MODEL *model=(MODEL *)my_malloc(sizeof(MODEL));

  read_input_parameters(argc,argv,docfile,modelfile,restartfile,&verbosity,
			&learn_parm,&kernel_parm);
  read_documents(docfile,&docs,&target,&totwords,&totdoc);
  if(restartfile[0]) alpha_in=read_alphas(restartfile,totdoc);

  if(kernel_parm.kernel_type == LINEAR) { /* don't need the cache */
    kernel_cache=NULL;
  }
  else {
    /* Always get a new kernel cache. It is not possible to use the
       same cache for two different training runs */
    kernel_cache=kernel_cache_init(totdoc,learn_parm.kernel_cache_size);
  }

  if(learn_parm.type == CLASSIFICATION) {
    svm_learn_classification(docs,target,totdoc,totwords,&learn_parm,
			     &kernel_parm,kernel_cache,model,alpha_in);
  }
  else if(learn_parm.type == REGRESSION) {
    svm_learn_regression(docs,target,totdoc,totwords,&learn_parm,
			 &kernel_parm,&kernel_cache,model);
  }

  /* 2014/09/19 Ranking is what we need. In svm_learn.c . */
  else if(learn_parm.type == RANKING) {
    svm_learn_ranking(docs,target,totdoc,totwords,&learn_parm,
		      &kernel_parm,&kernel_cache,model);
  }
  else if(learn_parm.type == OPTIMIZATION) {
    svm_learn_optimization(docs,target,totdoc,totwords,&learn_parm,
			   &kernel_parm,kernel_cache,model,alpha_in);
  }

  if(kernel_cache) {
    /* Free the memory used for the cache. */
    kernel_cache_cleanup(kernel_cache);
  }

  /* Warning: The model contains references to the original data 'docs'.
     If you want to free the original data, and only keep the model, you
     have to make a deep copy of 'model'. */
  /* deep_copy_of_model=copy_model(model); */


  /*  */

  write_model(modelfile,model);

  free(alpha_in);
  free_model(model,0);
  for(i=0;i<totdoc;i++)
    free_example(docs[i],1);
  free(docs);
  free(target);

  return(0);
}
Exemplo n.º 13
0
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);

}
Exemplo n.º 14
0
void
mexFunction (int nout, mxArray ** out, int nin, mxArray const ** in)
{
  SAMPLE sample;  /* training sample */
  LEARN_PARM learn_parm;
  KERNEL_PARM kernel_parm;
  STRUCT_LEARN_PARM struct_parm;
  STRUCTMODEL structmodel;
  int alg_type;

  enum {IN_ARGS=0, IN_SPARM} ;
  enum {OUT_W=0} ;

  /* SVM-light is not fully reentrant, so we need to run this patch first */
  init_qp_solver() ;
  verbosity = 0 ;
  kernel_cache_statistic = 0 ;

  if (nin != 2) {
    mexErrMsgTxt("Two arguments required") ;
  }

  /* Parse ARGS  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  char arg [1024 + 1] ;
  int argc ;
  char ** argv ;

  if (! uIsString(in[IN_ARGS], -1)) {
    mexErrMsgTxt("ARGS must be a string") ;
  }

  mxGetString(in[IN_ARGS], arg, sizeof(arg) / sizeof(char)) ;
  arg_split (arg, &argc, &argv) ;

  svm_struct_learn_api_init(argc+1, argv-1) ;

  read_input_parameters (argc+1,argv-1,
                         &verbosity, &struct_verbosity,
                         &struct_parm, &learn_parm,
                         &kernel_parm, &alg_type ) ;
  
  if (kernel_parm.kernel_type != LINEAR &&
      kernel_parm.kernel_type != CUSTOM) {
    mexErrMsgTxt ("Only LINEAR or CUSTOM kerneles are supported") ;
  }

  /* Parse SPARM ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  mxArray const * sparm_array = in [IN_SPARM] ;
  mxArray const * patterns_array ;
  mxArray const * labels_array ;
  mxArray const * kernelFn_array ;
  int numExamples, ei ;

  if (! sparm_array) {
    mexErrMsgTxt("SPARM must be a structure") ;
  }
  struct_parm.mex = sparm_array ;

  patterns_array = mxGetField(sparm_array, 0, "patterns") ;
  if (! patterns_array ||
      ! mxIsCell(patterns_array)) {
    mexErrMsgTxt("SPARM.PATTERNS must be a cell array") ;
  }

  numExamples = mxGetNumberOfElements(patterns_array) ;

  labels_array = mxGetField(sparm_array, 0, "labels") ;
  if (! labels_array ||
      ! mxIsCell(labels_array) ||
      ! mxGetNumberOfElements(labels_array) == numExamples) {
    mexErrMsgTxt("SPARM.LABELS must be a cell array "
                 "with the same number of elements of "
                 "SPARM.PATTERNS") ;
  }

  sample.n = numExamples ;
  sample.examples = (EXAMPLE *) my_malloc (sizeof(EXAMPLE) * numExamples) ;
  for (ei = 0 ; ei < numExamples ; ++ ei) {
    sample.examples[ei].x.mex = mxGetCell(patterns_array, ei) ;
    sample.examples[ei].y.mex = mxGetCell(labels_array,   ei) ;
    sample.examples[ei].y.isOwner = 0 ;
  }

  if (struct_verbosity >= 1) {
    mexPrintf("There are %d training examples\n", numExamples) ;
  }

  kernelFn_array = mxGetField(sparm_array, 0, "kernelFn") ;
  if (! kernelFn_array && kernel_parm.kernel_type == CUSTOM) {
    mexErrMsgTxt("SPARM.KERNELFN must be define for CUSTOM kernels") ;
  }
  if (kernelFn_array) {
    MexKernelInfo * info ;
    if (mxGetClassID(kernelFn_array) != mxFUNCTION_CLASS) {
      mexErrMsgTxt("SPARM.KERNELFN must be a valid function handle") ;
    }
    info = (MexKernelInfo*) kernel_parm.custom ;
    info -> structParm = sparm_array ;
    info -> kernelFn   = kernelFn_array ;
  }

  /* Learning  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  switch (alg_type) {
  case 0:
    svm_learn_struct(sample,&struct_parm,&learn_parm,&kernel_parm,&structmodel,NSLACK_ALG) ;
    break ;
  case 1:
    svm_learn_struct(sample,&struct_parm,&learn_parm,&kernel_parm,&structmodel,NSLACK_SHRINK_ALG);
    break ;
  case 2:
    svm_learn_struct_joint(sample,&struct_parm,&learn_parm,&kernel_parm,&structmodel,ONESLACK_PRIMAL_ALG);
    break ;
  case 3:
    svm_learn_struct_joint(sample,&struct_parm,&learn_parm,&kernel_parm,&structmodel,ONESLACK_DUAL_ALG);
    break ;
  case 4:
    svm_learn_struct_joint(sample,&struct_parm,&learn_parm,&kernel_parm,&structmodel,ONESLACK_DUAL_CACHE_ALG);
    break  ;
  case 9:
    svm_learn_struct_joint_custom(sample,&struct_parm,&learn_parm,&kernel_parm,&structmodel);
    break ;
  default:
    mexErrMsgTxt("Unknown algorithm type") ;
  }

  /* Write output  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

  /* Warning: The model contains references to the original data 'docs'.
     If you want to free the original data, and only keep the model, you
     have to make a deep copy of 'model'. */

  mxArray * model_array = newMxArrayEncapsulatingSmodel (&structmodel) ;
  out[OUT_W] = mxDuplicateArray (model_array) ;
  destroyMxArrayEncapsulatingSmodel (model_array) ;
  
  free_struct_sample (sample) ;
  free_struct_model (structmodel) ;
  svm_struct_learn_api_exit () ;
  free_qp_solver () ;
}
Exemplo n.º 15
0
/* call as  model = mexsvmlearn(data,labels,options) */
void mexFunction(int nlhs, mxArray *plhs[],
				 int nrhs, const mxArray *prhs[])
{
	char **argv;
	int argc;
	DOC **docs;  /* training examples */
	long totwords,totdoc,i;
	double *target;
	double *alpha_in=NULL;
	KERNEL_CACHE *kernel_cache;
	LEARN_PARM learn_parm;
	KERNEL_PARM kernel_parm;
	MODEL model;

	/* check for valid calling format */
	if ((nrhs != 3)  || (nlhs != 1))
		mexErrMsgTxt(ERR001);

	if (mxGetM(prhs[0]) != mxGetM(prhs[1]))
		mexErrMsgTxt(ERR002);

	if (mxGetN(prhs[1]) != 1)
		mexErrMsgTxt(ERR003);

	/* reset static variables -- as a .DLL, static things are sticky  */
	global_init( );

	/* convert the parameters (given in prhs[2]) into an argv/argc combination */
	argv = make_argv((mxArray *)prhs[2],&argc); /* send the options */



	/* this was originally supposed to be argc, argv, re-written for MATLAB ...
	its cheesy - but it workss :: convert the options array into an argc, 
	argv pair  and let svm_lite handle it from there. */

	read_input_parameters(argc,argv,docfile,modelfile,restartfile,&verbosity, 
		&learn_parm,&kernel_parm);

	extract_user_opts((mxArray *)prhs[2], &kernel_parm);

	totdoc = mxGetM(prhs[0]);
	totwords = mxGetN(prhs[0]);

	/* prhs[0] = samples (mxn) array
	prhs[1] = labels (mx1) array */
	mexToDOC((mxArray *)prhs[0], (mxArray *)prhs[1], &docs, &target, NULL, NULL);

	/* TODO modify to accept this array 
	if(restartfile[0]) alpha_in=read_alphas(restartfile,totdoc); */

	if(kernel_parm.kernel_type == LINEAR) { /* don't need the cache */
		kernel_cache=NULL;
	}
	else {
		/* Always get a new kernel cache. It is not possible to use the
		same cache for two different training runs */
		kernel_cache=kernel_cache_init(totdoc,learn_parm.kernel_cache_size);
	}


	if(learn_parm.type == CLASSIFICATION) {
		svm_learn_classification(docs,target,totdoc,totwords,&learn_parm,
			&kernel_parm,kernel_cache,&model,alpha_in);

	}
	else if(learn_parm.type == REGRESSION) {
		svm_learn_regression(docs,target,totdoc,totwords,&learn_parm,
			&kernel_parm,&kernel_cache,&model);
	}
	else if(learn_parm.type == RANKING) {
		svm_learn_ranking(docs,target,totdoc,totwords,&learn_parm,
			&kernel_parm,&kernel_cache,&model);
	}
	else if(learn_parm.type == OPTIMIZATION) {
		svm_learn_optimization(docs,target,totdoc,totwords,&learn_parm,
			&kernel_parm,kernel_cache,&model,alpha_in);
	}
	else {
		mexErrMsgTxt(ERR004);
	}

	if(kernel_cache) {
		/* Free the memory used for the cache. */
		kernel_cache_cleanup(kernel_cache);
	}

	/* **********************************
	* After the training/learning portion has finished,
	* copy the model back to the output arrays for MATLAB 
	* ********************************** */
	store_model(&model, plhs);

	free_kernel();
	global_destroy( );	
}
Exemplo n.º 16
0
int svm_learn (int argc, char* argv[], DOC **docs, double *label, long totwords, long totdoc, MODEL **_model)
{  
  char docfile[200];           /* file with training examples */
  char modelfile[200];         /* file for resulting classifier */
  char restartfile[200];       /* file with initial alphas */
  double *target=label;
  double *alpha_in=NULL;
  KERNEL_CACHE *kernel_cache;
  LEARN_PARM learn_parm;
  KERNEL_PARM kernel_parm;
  MODEL *model=(MODEL *)my_malloc(sizeof(MODEL));

  HIDEO_ENV *hideo_env=create_env();

  model->td_pred=NULL;
  model->n_td_pred=0;

  if (read_input_parameters(argc,argv,docfile,modelfile,restartfile,&verbosity,
	  &learn_parm,&kernel_parm) != 0) { return(-1); }
  // We override predictions file parameter for transductive SVM (this results in storing predictions into model->td_pred).
  learn_parm.predfile[0]=0;
  //read_documents(docfile,&docs,&target,&totwords,&totdoc);
  if(restartfile[0]) alpha_in=read_alphas(restartfile,totdoc);

  if(kernel_parm.kernel_type == LINEAR) { /* don't need the cache */
    kernel_cache=NULL;
  }
  else {
    /* Always get a new kernel cache. It is not possible to use the
       same cache for two different training runs */
    kernel_cache=kernel_cache_init(totdoc,learn_parm.kernel_cache_size);
  }

  if(learn_parm.type == CLASSIFICATION) {
    svm_learn_classification(docs,target,totdoc,totwords,&learn_parm,
			     &kernel_parm,kernel_cache,model,alpha_in,hideo_env);
  }
  else if(learn_parm.type == REGRESSION) {
    svm_learn_regression(docs,target,totdoc,totwords,&learn_parm,
			 &kernel_parm,&kernel_cache,model,hideo_env);
  }
  else if(learn_parm.type == RANKING) {
    svm_learn_ranking(docs,target,totdoc,totwords,&learn_parm,
		      &kernel_parm,&kernel_cache,model,hideo_env);
  }
  else if(learn_parm.type == OPTIMIZATION) {
    svm_learn_optimization(docs,target,totdoc,totwords,&learn_parm,
			   &kernel_parm,kernel_cache,model,alpha_in,hideo_env);
  }

  if(kernel_cache) {
    /* Free the memory used for the cache. */
    kernel_cache_cleanup(kernel_cache);
  }

  /* Warning: The model contains references to the original data 'docs'.
     If you want to free the original data, and only keep the model, you 
     have to make a deep copy of 'model'. */
  (*_model)=copy_model(model); 
  //write_model(modelfile,model);

  free(alpha_in);
  free_model(model,0);
  //for(i=0;i<totdoc;i++) 
  //  free_example(docs[i],1);
  //free(docs);
  //free(target);
  free_env(hideo_env);

  return(0);
}
Exemplo n.º 17
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);
}
Exemplo n.º 18
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
}