joinbindata.cc

  1 const char *help = "\
  2 progname: joinbindata.cc\n\
  3 code2html: This program joins 2 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 int main(int argc, char *argv[])
 13 {
 14 	char *filename_in1;
 15 	char *filename_in2;
 16 	char *filename_out;
 17 	bool average;
 18 	bool delta;
 19 	bool diff; 
 20 	bool verbose;
 21 	
 22 	CmdLine cmd;
 23 	cmd.setBOption("write log", false);
 24 
 25 	cmd.info(help);
 26 	cmd.addText("\nArguments:");
 27 	cmd.addSCmdArg("filename in1", &filename_in1, "input bindata filename 1");
 28 	cmd.addSCmdArg("filename in2", &filename_in2, "input bindata filename 2");
 29 	cmd.addText("\nArguments:");
 30 	cmd.addBCmdOption("-verbose", &verbose, false, "verbose");
 31 	cmd.addBCmdOption("-diff", &diff, false, "diff");
 32 	cmd.addBCmdOption("-delta", &delta, false, "delta");
 33 	cmd.addBCmdOption("-average", &average, false, "average");
 34 	cmd.addSCmdOption("-o", &filename_out, "join.bindata", "output bindata filename");
 35 	cmd.read(argc, argv);
 36 
 37 	DiskXFile *file1 = NULL;
 38 	DiskXFile *file2 = NULL;
 39 	DiskXFile *ofile = NULL;
 40   
 41 	int n_patterns1;
 42 	int n_inputs1;
 43 
 44 	file1 = new DiskXFile(filename_in1, "r");
 45 		
 46 	file1->read(&n_patterns1, sizeof(int), 1);
 47 	file1->read(&n_inputs1, sizeof(int), 1);
 48 
 49 	if(verbose)
 50 	{
 51 		print("Reading bindata file (%s)\n", filename_in1);
 52 		print("   n_inputs = %d\n", n_inputs1);
 53 		print("   n_patterns = %d\n", n_patterns1);  
 54 	}
 55 
 56 
 57 	int n_patterns2;
 58 	int n_inputs2;
 59 
 60 	file2 = new DiskXFile(filename_in2, "r");
 61 		
 62 	file2->read(&n_patterns2, sizeof(int), 1);
 63 	file2->read(&n_inputs2, sizeof(int), 1);
 64 
 65 	if(verbose)
 66 	{
 67 		print("Reading bindata file (%s)\n", filename_in2);
 68 		print("   n_inputs = %d\n", n_inputs2);
 69 		print("   n_patterns = %d\n", n_patterns2);  
 70 	}
 71 
 72 	if(n_inputs1 != n_inputs2) error("n_inputs different");
 73 
 74 	int n_patterns;
 75 	
 76 	if(average) n_patterns = n_patterns2;
 77 	else n_patterns = n_patterns1 * n_patterns2;
 78 
 79 	int n_inputs;
 80 	
 81 	if(diff) n_inputs = n_inputs1;
 82 	else n_inputs = n_inputs1+n_inputs2;
 83 	
 84 	if(verbose)
 85 	{
 86 		print("Writting bindata file (%s)\n", filename_out);
 87 		print("   n_inputs = %d\n", n_inputs);
 88 		print("   n_patterns = %d\n", n_patterns);  
 89 	}
 90 
 91 	ofile = new DiskXFile(filename_out, "w");
 92 	ofile->write(&n_patterns, sizeof(int), 1);
 93 	ofile->write(&n_inputs, sizeof(int), 1);
 94 
 95 	real *inputs = new real [n_inputs];
 96 	real *mean = new real [n_inputs1];
 97 	for(int i = 0 ; i < n_inputs1 ; i++) mean[i] = 0.0;
 98 	
 99 	real **inputs1 = new real*[n_patterns1];
100 	for(int p = 0 ; p < n_patterns1 ; p++)
101 	{
102 		inputs1[p] = new real [n_inputs1];
103 		file1->read(inputs1[p], sizeof(real), n_inputs1);
104 		for(int i = 0 ; i < n_inputs1 ; i++) mean[i] += inputs1[p][i];
105 	}
106 	for(int i = 0 ; i < n_inputs1 ; i++) mean[i] /= (real) n_patterns1;
107 
108 	real **inputs2 = new real*[n_patterns2];
109 	for(int p = 0 ; p < n_patterns2 ; p++)
110 	{
111 		inputs2[p] = new real [n_inputs2];
112 		file2->read(inputs2[p], sizeof(real), n_inputs2);
113 	}
114 
115 	if(average)
116 	{
117 		for(int p2 = 0 ; p2 < n_patterns2 ; p2++)
118 		{
119 			if(diff)
120 		   		for(int i = 0 ; i < n_inputs ; i++) inputs[i] = inputs2[p2][i] - mean[i];
121 			else
122 			{
123 		   		int j = 0;	
124 				if(delta)
125 		   			for(int i = 0 ; i < n_inputs2 ; i++, j++) inputs[j] = inputs2[p2][i] - mean[i];
126 				else for(int i = 0 ; i < n_inputs2 ; i++, j++) inputs[j] = inputs2[p2][i];
127 
128 		   		for(int i = 0 ; i < n_inputs1 ; i++, j++) inputs[j] = mean[i];
129 			}
130 
131 			ofile->write(inputs, sizeof(real), n_inputs);
132 		}
133 	}
134 	else
135 	{
136 		for(int p1 = 0 ; p1 < n_patterns1 ; p1++)
137 		{
138 		   for(int p2 = 0 ; p2 < n_patterns2 ; p2++)
139 		   {
140 			if(diff)
141 		   		for(int i = 0 ; i < n_inputs ; i++) inputs[i] = inputs2[p2][i] - inputs1[p1][i];
142 			else
143 			{
144 		   		int j = 0;	
145 				if(delta)
146 		   			for(int i = 0 ; i < n_inputs2 ; i++, j++) inputs[j] = inputs2[p2][i] - inputs1[p1][i];
147 				else for(int i = 0 ; i < n_inputs2 ; i++, j++) inputs[j] = inputs2[p2][i];
148 
149 		   		for(int i = 0 ; i < n_inputs1 ; i++, j++) inputs[j] = inputs1[p1][i];
150 			}
151 
152 			ofile->write(inputs, sizeof(real), n_inputs);
153 		   }
154 		}
155 	}
156 	
157 	for(int p = 0 ; p < n_patterns1 ; p++) delete [] inputs1[p];
158 	delete [] inputs1;
159 
160 	for(int p = 0 ; p < n_patterns2 ; p++) delete [] inputs2[p];
161 	delete [] inputs2;
162 
163 	delete [] inputs;
164 	delete [] mean;
165 
166 	delete file1;
167 	delete file2;
168 
169 	return 0;
170 }
171