Ejemplo n.º 1
0
void run_chromatic_sampler(graphlab::core<mrf_graph_type, gibbs_update>& core,
                           const std::string& chromatic_results_fn,
                           const std::vector<double>& runtimes,
                           const bool draw_images) {
  // Initialize scheduler
  core.set_scheduler_type("chromatic");
  core.set_scope_type("null");

  const size_t ncpus = core.get_options().get_ncpus();

  // Use fixed update function
  gibbs_update ufun;
  core.schedule_all( ufun );
  
  double total_runtime = 0;
  double actual_total_runtime = 0;
  foreach(const double experiment_runtime, runtimes) {
    total_runtime += experiment_runtime;
    // get the experiment id
    size_t experiment_id = file_line_count(chromatic_results_fn);
    std::cout << "Running chromatic sampler experiment " << experiment_id
              << " for " << experiment_runtime << " seconds." << std::endl;
    // set the termination time
    core.engine().set_timeout(experiment_runtime);
    // Run the engine
    graphlab::timer timer;
    timer.start();
    core.start();
    double actual_experiment_runtime = timer.current_time();
    actual_total_runtime += actual_experiment_runtime;
    /// ==================================================================
    // Compute final statistics of the mode
    run_statistics stats(core.graph());
    stats.print();
    // Save the beliefs
    save_beliefs(core.graph(),
                 make_filename("chromatic_blfs_", ".tsv", experiment_id));
    // // Save the current assignments
    save_asg(core.graph(),
             make_filename("chromatic_asg_", ".asg", experiment_id));
    // Save the experiment
    std::ofstream fout(chromatic_results_fn.c_str(), std::ios::app);
    fout.precision(16);
    fout << experiment_id << '\t'
         << total_runtime << '\t'
         << actual_total_runtime << '\t'
         << ncpus << '\t'
         << stats.nsamples << '\t'
         << stats.nchanges << '\t'
         << stats.loglik << '\t'
         << stats.min_samples << '\t'
         << stats.max_samples << std::endl;
    fout.close();
    // Plot images if desired
    if(draw_images) draw_mrf(experiment_id, "chromatic", core.graph());
  }
Ejemplo n.º 2
0
void run_jtsplash_sampler(mrf_graph_type& mrf_graph,
                          const std::string& jtsplash_results_fn,
                          const std::vector<double>& runtimes,
                          const bool draw_images,
                          const splash_settings& settings) {

  //  size_t ncpus = core.engine().get_ncpus();
  // bool affinities = 
  //   core.get_engine_options().get_cpu_affinities();
  // Initialize the jtsplash sampler
  jt_splash_sampler jtsplash_sampler(mrf_graph, settings);

  double total_runtime = 0;
  double actual_total_runtime = 0;
  foreach(const double experiment_runtime, runtimes) {
    total_runtime += experiment_runtime;
    // get the experiment id
    size_t experiment_id = file_line_count(jtsplash_results_fn);
    std::cout << "Running JTSplash sampler experiment " << experiment_id
              << " for " << experiment_runtime << " seconds." << std::endl;

    std::cout << "Settings: ======================" << std::endl
              << "Experiment:    " << experiment_id << std::endl
              << "runtime:       " << experiment_runtime << std::endl
              << "treesize:      " << settings.max_tree_size << std::endl
              << "treewidth:     " << settings.max_tree_width << std::endl
              << "treeheight:    " << settings.max_tree_height << std::endl
              << "factorsize:    " << settings.max_factor_size << std::endl
              << "subthreads:    " << settings.subthreads << std::endl
              << "priorities:    " << settings.priorities << std::endl   
              << "vanish:        " << settings.vanish_updates << std::endl;   


    graphlab::timer timer;
    timer.start();

    // run the sampler once
    jtsplash_sampler.sample_seconds(experiment_runtime);

    double actual_experiment_runtime = timer.current_time();
    std::cout << "Actual Experiment Runtime:" 
              << actual_experiment_runtime << std::endl;        
    actual_total_runtime += actual_experiment_runtime;
    std::cout << "Total Experiment Runtime (actual): "
              << total_runtime 
              << "(" << actual_total_runtime << ")" 
              << std::endl;


    // check mrf graph
    for(size_t i = 0; i < mrf_graph.num_vertices(); ++i) {
      ASSERT_EQ(mrf_graph.vertex_data(i).tree_info.tree_id, NULL_VID);
    }



    /// ==================================================================
    // Compute final statistics of the mode
    run_statistics stats(mrf_graph);
    stats.print();
    // Save the beliefs
    save_beliefs(mrf_graph,
                 make_filename("jtsplash_blfs_", ".tsv", experiment_id));
    // // Save the current assignments
    save_asg(mrf_graph,
             make_filename("jtsplash_asg_", ".asg", experiment_id));
    // Save the experiment
    std::ofstream fout(jtsplash_results_fn.c_str(), std::ios::app);
    fout.precision(8);
    fout << experiment_id << '\t'
         << total_runtime << '\t'
         << actual_total_runtime << '\t'
         << settings.ntrees << '\t'
         << stats.nsamples << '\t'
         << stats.nchanges << '\t'
         << stats.loglik << '\t'
         << stats.min_samples << '\t'
         << stats.max_samples << '\t'
         << settings.max_tree_size << '\t'
         << settings.max_tree_width << '\t'
         << settings.max_factor_size << '\t'
         << settings.max_tree_height << '\t'
         << settings.subthreads << '\t'
         << settings.priorities << '\t'
         << jtsplash_sampler.total_trees() << '\t'  
         << jtsplash_sampler.total_collisions() << '\t'
         << std::endl;
    fout.close();

    // Plot images if desired
    if(draw_images) draw_mrf(experiment_id, "jtsplash", mrf_graph);

  } // end of for loop over runtimes
Ejemplo n.º 3
0
int fileop_use_by_name( char *file_name )
{
	FILE			*fp;
	char			name[ MAX_TOKEN_LENGTH ];
	string_object	src;
	string_object	s;
	char			comment_state	= FALSE;
	unsigned long 	file_size;
	unsigned long 	i;
	struct stat		stat_buffer;
	
	// file open an prepare to load

	strcpy( name, file_name );
	file_handle_path( name, MAX_TOKEN_LENGTH );

	if ( NULL == (fp	= fopen( name, "r" )) )
	{
		cprintf( ERROR, CONT, "fail to use file \"%s\"\n", name );
		return ( ERROR );
	}

	set_loading_file_name( name );

	//	file loading

	stat( name, &stat_buffer );
	file_size	= (unsigned long)(stat_buffer.st_size);

	if ( NULL == (src	= make_string_object( "", file_size + 1 )) )
	{
		cprintf( ERROR, CONT, "file is too big, can't read to memory : file size = %lu (%s)\n", file_size, name );
		return ( ERROR );
	}

	fread( src, file_size, 1, fp );
	
	for ( i = 0, s = src; i < file_size; i++, s++ )
	{
		switch ( *s )
		{
			case '#' : 
				comment_state	= TRUE;
				break;
			case '\n' : 
				comment_state	= FALSE;
				*s	= LINEFEED_REPLACED;
				break;
			default : 
				break;
		}

		if ( comment_state )
			*s	= ' ';
	}

	file_line_count( LINE_COUNT_RESET );

	if ( evaluate( src ) )
	{
		cprintf( BOLD, CONT, "error found in file : \"%s\"\n", name );
		cprintf( BOLD, CONT, "at line #%d\n", file_line_count( LINE_COUNT_STAY ) );
	}
	
	dispose_string_object( src );

	set_loading_file_name( NULL );
	
	fclose( fp );
	
	return ( NO_ERROR );
}
Ejemplo n.º 4
0
int main(int argc, char **argv)
{ 
   parse_args(argc, argv);

   int Nlines = file_line_count( distlist );
   fprintf(stderr,"Total distance instances: %d\n",  Nlines);

   // Read in the distlist
   fprintf(stderr,"Reading distance list: "); tic();

   float *dist = (float *) MALLOC( Nlines*sizeof(float) );
   int *sw = (int *) MALLOC( Nlines*sizeof(int) );
   int *sp = (int *) MALLOC( Nlines*sizeof(int) );

   FILE *fptr = fopen(distlist, "r");
   int lcnt = 0;

   while ( lcnt < Nlines && 
	   fscanf(fptr, "%f%d%d",
		  &dist[lcnt], &sw[lcnt], &sp[lcnt] ) != EOF ) {
      //fprintf(stderr, "%f %d %d\n", dist[lcnt], sw[lcnt], sp[lcnt]);
      lcnt++;
   }
   fclose(fptr);
   fprintf(stderr, "%f s\n",toc());

   // Find the maximum distance value
   float maxdist = 0;
   for ( int i = 0; i < Nlines; i++ ) {
      if ( dist[i] > maxdist ) 
	 maxdist = dist[i];
   }

   // Estimate the four distributions
   fprintf(stderr,"Estimating distributions: "); tic();
   double kwidth = 0.05*maxdist;
   double x[100];
   double P_swsp[100];
   double P_swdp[100];
   double P_dwsp[100];
   double P_dwdp[100];

   for ( int i = 0; i < 100; i++ ) {
      x[i] = i*maxdist/100;
      P_swsp[i] = 0;
      P_swdp[i] = 0;
      P_dwsp[i] = 0;
      P_dwdp[i] = 0;
   }

   for ( int i = 0; i < Nlines; i++ ) {
      double *P_ptr;
      if ( sw[i] && sp[i] ) // SWSP
	 P_ptr = P_swsp;
      else if ( sw[i] && !sp[i] ) // SWDP
	 P_ptr = P_swdp;
      else if ( !sw[i] && sp[i] ) // DWSP
	 P_ptr = P_dwsp;
      else //DWDP 
	 P_ptr = P_dwdp;

      int jA = MAX(0,floor((dist[i]-kwidth)/(maxdist/100)));
      int jB = MIN(100,ceil((dist[i]+kwidth)/(maxdist/100)));

      for ( int j = jA; j < jB; j++ ) P_ptr[j]++;
   }

   dist_normalize( x, P_swsp );
   dist_normalize( x, P_swdp );
   dist_normalize( x, P_dwsp );
   dist_normalize( x, P_dwdp );
   fprintf(stderr, "%f s\n",toc());

   // Compute the precision-recall values
   fprintf(stderr,"Computing precision and recall: "); tic();
   double prec[100];
   double rec_swsp[100];
   double rec_swdp[100];

   for ( int i = 0; i < 100; i++ ) {
      prec[i] = 0;
      rec_swsp[i] = 0;
      rec_swdp[i] = 0;
   }

   int tot_swsp = 0;
   int tot_swdp = 0;
   for ( int i = 0; i < Nlines; i++ ) {
      if ( sw[i] && sp[i] )
	 tot_swsp++;

      if ( sw[i] && !sp[i] )
	 tot_swdp++;
      
      for ( int k = 99; k >= 0; k-- ) {
	 if ( dist[i] > x[k] ) break;

	 if ( !sw[i] ) {
	    prec[k]++;
	 }

	 if ( sw[i] && sp[i] ) rec_swsp[k]++;
	 if ( sw[i] && !sp[i] ) rec_swdp[k]++;
      }
   }

   for ( int i = 0; i < 100; i++ ) {
      prec[i] = (rec_swsp[i]+rec_swdp[i])/(rec_swsp[i]+rec_swdp[i]+prec[i]+1e-20);
      rec_swsp[i] = rec_swsp[i]/tot_swsp;
      rec_swdp[i] = rec_swdp[i]/tot_swdp;
   }
   
   fprintf(stderr, "%f s\n",toc());

   // Write the distributions and prec-rec values to outfile
   fprintf(stderr,"Writing distributions: "); tic();
   fptr = fopen(outfile, "w");
   for ( int i = 0; i < 100; i++ ) {
      fprintf(fptr, "%f %f %f %f %f %f %f %f \n", 
	      x[i], P_swsp[i], P_swdp[i], P_dwsp[i], P_dwdp[i], 
	      prec[i], rec_swsp[i], rec_swdp[i]);
   }
   fclose(fptr);
   fprintf(stderr, "%f s\n",toc());

   // Compute representation quality measures
   fprintf(stderr,"Compute quality measures: "); tic();
   double mu1 = 0; int c1 = 0;
   double mu2 = 0; int c2 = 0;
   double mu12 = 0; int c12 = 0;
   double mu34 = 0; int c34 = 0;
   
   for ( int i = 0; i < Nlines; i++ ) {
      if ( sw[i] && sp[i] ) {// SWSP
	 mu1 += dist[i];
	 c1++;
      }
      
      if ( sw[i] && !sp[i] ) {// SWDP
	 mu2 += dist[i];
	 c2++;
      }
      
      if ( sw[i] ) {// SW
	 mu12 += dist[i];
	 c12++;
      } else { // DW 
	 mu34 += dist[i];
	 c34++;
      }      
   }

   mu1 /= c1;
   mu2 /= c2;
   mu12 /= c12;
   mu34 /= c34;

   double var1 = 0;
   double var2 = 0;
   double var12 = 0; 
   double var34 = 0; 

   for ( int i = 0; i < Nlines; i++ ) {
      if ( sw[i] && sp[i] ) {// SWSP
	 var1 += pow(dist[i]-mu1,2)/(c1-1);
      }
      
      if ( sw[i] && !sp[i] ) {// SWDP
	 var2 += pow(dist[i]-mu2,2)/(c2-1);
      }
      
      if ( sw[i] ) {// SW
	 var12 += pow(dist[i]-mu12,2)/(c12-1);
      } else { // DW 
	 var34 += pow(dist[i]-mu34,2)/(c34-1);
      }      
   }
   
   double S_1_2 = pow(mu1-mu2,2)/(var1+var2);
   double S_12_34 = pow(mu12-mu34,2)/(var12+var34);
   double R = S_12_34/S_1_2;
   
   double PRB1 = 0;
   double PRB2 = 0;
   double mindiff1 = 1;
   double mindiff2 = 1;

   for ( int i = 0; i < 100; i++ ) {
      if ( rec_swsp[i] > 0.01 && fabs(prec[i]-rec_swsp[i]) < mindiff1 ) {
	 mindiff1 = fabs(prec[i]-rec_swsp[i]);
	 PRB1 = 0.5*(prec[i]+rec_swsp[i]);
      }

      if ( rec_swdp[i] > 0.01 && fabs(prec[i]-rec_swdp[i]) < mindiff2 ) {
	 mindiff2 = fabs(prec[i]-rec_swdp[i]);
	 PRB2 = 0.5*(prec[i]+rec_swdp[i]);
      }
   }
   fprintf(stderr, "%f s\n",toc());

   fprintf(stderr,"Compute average precisions: "); tic();
   float AveP = average_precision_overall( dist, sw, Nlines );
   float AveP_sp = average_precision_samespkr( dist, sw, sp, Nlines );
   float AveP_dp = average_precision_diffspkr( dist, sw, sp, Nlines );
   float sid_AveP = average_precision_sid( dist, sw, sp, Nlines );
   float FAatRecall = falsealarm_at_recall( dist, sw, Nlines, 0.5 );
   fprintf(stderr, "%f s\n",toc());

//   fprintf(stderr,"mu1=%f\nmu2=%f\nsigma1=%f\nsigma2=%f\n",mu1,mu2,sqrtf(var1),sqrtf(var2));
//   fprintf(stderr,"mu12=%f\nmu34=%f\nsigma12=%f\nsigma34=%f\n",mu12,mu34,sqrtf(var12),sqrtf(var34));

   fprintf(stderr,"----------------Results----------------\n");
   fprintf(stderr,"File: %s\n",distlist);
   fprintf(stderr,"S_1_2 = %3.3f\n", S_1_2);
   fprintf(stderr,"S_12_34 = %3.3f\n", S_12_34);
   fprintf(stderr,"R = %3.3f\n", R);
   fprintf(stderr,"PRB_swsp = %3.3f\n", PRB1);
   fprintf(stderr,"PRB_swdp = %3.3f\n", PRB2);
   fprintf(stderr,"---------------------------------------\n");
   fprintf(stderr,"same_ap = %3.3f\n", AveP_sp);
   fprintf(stderr,"diff_ap = %3.3f\n", AveP_dp);
   fprintf(stderr,"ave_prec = %3.3f\n", AveP);
   fprintf(stderr,"---------------------------------------\n");
   fprintf(stderr,"sid_ap = %3.3f\n", sid_AveP);
   fprintf(stderr,"---------------------------------------\n");
   fprintf(stderr,"FA_at_Recall50 = %3.3f\n", FAatRecall);
   fprintf(stderr,"---------------------------------------\n");

   // FREE everything that was malloc-d
   FREE(dist);
   FREE(sw);
   FREE(sp);

   return 0;
}