noisebindata.cc

  1 const char *help = "\
  2 progname: noisebindata.cc\n\
  3 code2html: This program adds random noise to a bindata file.\n\
  4 version: Torch3 vision2.0, 2003-2005\n\
  5 (c) Sebastien Marcel (marcel@idiap.ch)\n";
  6 
  7 #include "DiskXFile.h"
  8 #include "Random.h"
  9 #include "CmdLine.h"
 10 
 11 using namespace Torch;
 12 
 13 int main(int argc, char *argv[])
 14 {
 15 	char *filename_in;
 16 	char *filename_out;
 17 	int the_seed;
 18 	int dim;
 19 	int P;
 20 	real nmin, nmax;
 21 	bool verbose;
 22 	
 23 	CmdLine cmd;
 24 	cmd.setBOption("write log", false);
 25 
 26 	cmd.info(help);
 27 	cmd.addText("\nArguments:");
 28 	cmd.addSCmdArg("filename in", &filename_in, "input bindata filename");
 29 	cmd.addSCmdArg("filename out", &filename_out, "output bindata filename");
 30 	cmd.addText("\nArguments:");
 31 	cmd.addICmdOption("-seed", &the_seed, 9503, "seed");
 32 	cmd.addBCmdOption("-verbose", &verbose, false, "verbose");
 33 	cmd.addRCmdOption("-nmin", &nmin, -0.1, "min random noise");
 34 	cmd.addRCmdOption("-nmax", &nmax, +0.1, "max random noise");
 35 	cmd.addICmdOption("-p", &P, 1, "number of perturbations to generate");
 36 	cmd.addICmdOption("-dim", &dim, -1, "dimension to perturbate");
 37 	cmd.read(argc, argv);
 38 
 39 	DiskXFile *file = NULL;
 40 	DiskXFile *fileout = NULL;
 41   
 42   	// Reading headers
 43 	int n_patterns;
 44 	int n_inputs;
 45 
 46 	file = new DiskXFile(filename_in, "r");
 47 		
 48 	file->read(&n_patterns, sizeof(int), 1);
 49 	file->read(&n_inputs, sizeof(int), 1);
 50 
 51 	if(verbose)
 52 	{
 53 		print("Reading bindata file (%s)\n", filename_in);
 54 		print("   n_inputs = %d\n", n_inputs);
 55 		print("   n_patterns = %d\n", n_patterns);  
 56 	}
 57 
 58   	fileout = new DiskXFile(filename_out, "w");
 59   	if(fileout == NULL)
 60 	{
 61       		error("Opening BinData file %s", filename_out);
 62       
 63       		return 1;
 64 	}
 65 
 66 	int n_patterns_ = n_patterns * P;
 67 
 68 	fileout->write(&n_patterns_, sizeof(int), 1);
 69 	fileout->write(&n_inputs, sizeof(int), 1);
 70 
 71 	if(verbose)
 72 	{
 73   		print("\n");  
 74   		print("Writing bindata file :\n");  
 75   		print("   n_inputs : %d\n", n_inputs);
 76   		print("   n_patterns : %d\n", n_patterns_);  
 77         }
 78 
 79 	real *input = new real [n_inputs];
 80 	real *output = new real [n_inputs];
 81 
 82 	Random::manualSeed(the_seed);
 83 
 84 	for(int p = 0 ; p < n_patterns ; p++)
 85 	{		  
 86 		file->read(input, sizeof(real), n_inputs);
 87 
 88 		for(int j = 0 ; j < P ; j++)
 89 		{
 90 			for(int i = 0 ; i < n_inputs ; i++)
 91 				if((dim == -1) || (i == dim)) output[i] = input[i] + Random::boundedUniform(nmin, nmax);
 92 				else output[i] = input[i];
 93 
 94 			fileout->write(output, sizeof(real), n_inputs);
 95 		}
 96 	}
 97 
 98 	delete [] input;
 99 	delete [] output;
100 	delete fileout;
101 	delete file;
102 
103 	return 0;
104 }
105