cmpbindata.cc

  1 const char *help = "\
  2 progname: cmpbindata.cc\n\
  3 code2html: This program compares two bindata files.\n\
  4 version: Torch3 vision2.0, 2004-2005\n\
  5 (c) Sebastien Marcel (marcel@idiap.ch)\n";
  6 
  7 #include "DiskXFile.h"
  8 #include "CmdLine.h"
  9 
 10 using namespace Torch;
 11 
 12 real MSE(int n_inputs, real *x, real *y)
 13 {
 14 	real d_ = 0.0;
 15 
 16 	for(int i = 0 ; i < n_inputs ; i++)
 17 	{
 18 		real z = x[i] - y[i];
 19 		d_ += z * z;
 20 	}
 21 	
 22 	return d_ / (real) n_inputs;
 23 }
 24 
 25 int main(int argc, char *argv[])
 26 {
 27 	char *filename_in1;
 28 	char *filename_in2;
 29 	bool verbose;
 30 	
 31 	CmdLine cmd;
 32 	cmd.setBOption("write log", false);
 33 
 34 	cmd.info(help);
 35 	cmd.addText("\nArguments:");
 36 	cmd.addSCmdArg("filename in1", &filename_in1, "input bindata filename 1");
 37 	cmd.addSCmdArg("filename in2", &filename_in2, "input bindata filename 2");
 38 	cmd.addText("\nArguments:");
 39 	cmd.addBCmdOption("-verbose", &verbose, false, "verbose");
 40 	cmd.read(argc, argv);
 41 
 42 	DiskXFile *file1 = NULL;
 43 	DiskXFile *file2 = NULL;
 44   
 45 	int n_patterns1;
 46 	int n_inputs1;
 47 
 48 	file1 = new DiskXFile(filename_in1, "r");
 49 		
 50 	file1->read(&n_patterns1, sizeof(int), 1);
 51 	file1->read(&n_inputs1, sizeof(int), 1);
 52 
 53 	if(verbose)
 54 	{
 55 		print("Reading bindata file (%s)\n", filename_in1);
 56 		print("   n_inputs = %d\n", n_inputs1);
 57 		print("   n_patterns = %d\n", n_patterns1);  
 58 	}
 59 
 60 
 61 	int n_patterns2;
 62 	int n_inputs2;
 63 
 64 	file2 = new DiskXFile(filename_in2, "r");
 65 		
 66 	file2->read(&n_patterns2, sizeof(int), 1);
 67 	file2->read(&n_inputs2, sizeof(int), 1);
 68 
 69 	if(verbose)
 70 	{
 71 		print("Reading bindata file (%s)\n", filename_in2);
 72 		print("   n_inputs = %d\n", n_inputs2);
 73 		print("   n_patterns = %d\n", n_patterns2);  
 74 	}
 75 
 76 	if(n_inputs1 != n_inputs2) error("n_inputs different");
 77 
 78 	int n_inputs = n_inputs1;
 79 	
 80 	real **inputs1 = new real*[n_patterns1];
 81 	for(int p = 0 ; p < n_patterns1 ; p++)
 82 	{
 83 		inputs1[p] = new real [n_inputs];
 84 		file1->read(inputs1[p], sizeof(real), n_inputs);
 85 	}
 86 
 87 	real **inputs2 = new real*[n_patterns2];
 88 	for(int p = 0 ; p < n_patterns2 ; p++)
 89 	{
 90 		inputs2[p] = new real [n_inputs];
 91 		file2->read(inputs2[p], sizeof(real), n_inputs);
 92 	}
 93 
 94 	// Compare each pattern of file to patterns of file 1
 95 	real mean = 0.0;
 96 
 97 	for(int p2 = 0 ; p2 < n_patterns2 ; p2++)
 98 	{
 99 	   	real mean_ = 0.0;
100 
101 		print("Pattern %d:\n", p2);
102 		for(int p1 = 0 ; p1 < n_patterns1 ; p1++)
103 		{
104 			real d_ = MSE(n_inputs, inputs1[p1], inputs2[p2]);
105 
106 			print("   > %g\n", d_);
107 
108 			mean_ += d_;
109 		}
110 
111 		mean_ /= (real) n_patterns1;
112 
113 		print("   mean = %g\n", mean_);
114 
115 		mean += mean_;
116 	}
117 	
118 	mean /= (real) n_patterns2;
119 
120 	print("Average distance between %s and %s:\n", filename_in1, filename_in2);
121 	print(" > %g\n", mean);
122 
123 	for(int p = 0 ; p < n_patterns1 ; p++) delete [] inputs1[p];
124 	delete [] inputs1;
125 
126 	for(int p = 0 ; p < n_patterns2 ; p++) delete [] inputs2[p];
127 	delete [] inputs2;
128 
129 	delete file1;
130 	delete file2;
131 
132 	return 0;
133 }
134