bindata2lda.cc

  1 const char *help = "\
  2 progname: bindata2lda.cc\n\
  3 code2html: This program reads a bindata file and and LDA model and projects patterns into it.\n\
  4 version: Torch3 vision2.0, 2003-2005\n\
  5 (c) Sebastien Marcel (marcel@idiap.ch)\n";
  6 
  7 #include "ImageGray.h"
  8 #include "LDAMachine.h"
  9 #include "CmdLine.h"
 10 
 11 using namespace Torch;
 12 
 13 int main(int argc, char *argv[])
 14 {
 15 	char *bindata_filename;
 16 	char *model_filename;
 17 	int n_input;
 18 	int n_output;
 19 	char *filename_out;
 20 	real variance;
 21 	bool verbose;
 22 	int verbose_level;
 23 	bool norm_d_mean;
 24 	CmdLine cmd;
 25 	cmd.setBOption("write log", false);
 26 	
 27 	cmd.info(help);
 28 	cmd.addText("\nArguments:");
 29 	cmd.addSCmdArg("bindata filename", &bindata_filename, "bindata filename");
 30 	cmd.addSCmdArg("model filename", &model_filename, "LDA model filename");
 31 	cmd.addICmdArg("n_input", &n_input, "number of inputs");
 32 	cmd.addText("\nOptions:");
 33 	cmd.addSCmdOption("-o", &filename_out, "lda.bindata", "bindata output file");
 34 	cmd.addRCmdOption("-variance", &variance, -1.0, "variance (-1 100\%)");
 35 	cmd.addICmdOption("-n_output", &n_output, -1, "number of outputs (-1 auto)");
 36 	cmd.addBCmdOption("-verbose", &verbose, false, "verbose");
 37 	cmd.addICmdOption("-verbose_level", &verbose_level, 1, "level of verbose");
 38 	cmd.addBCmdOption("-dnorm", &norm_d_mean, false, "norm d mean");
 39 	cmd.read(argc, argv);
 40 
 41 	if(verbose == false) verbose_level = 0;
 42 
 43 	if(n_output > n_input) error("n_output > n_input");
 44 
 45 	//
 46 	LDAMachine *lda_machine = NULL;
 47 	lda_machine = new LDAMachine(n_input, norm_d_mean);
 48 
 49 	if(verbose_level >= 1)
 50 		print("Loading LDA model: %s ...\n", model_filename);
 51 
 52 	DiskXFile *file = NULL;
 53 	file = new DiskXFile(model_filename, "r");
 54 	lda_machine->loadXFile(file);
 55 	delete file;
 56 	   
 57 	//
 58 	lda_machine->setIOption("verbose_level", verbose_level);
 59 	lda_machine->setROption("variance", variance);
 60 	lda_machine->init();
 61 
 62 	if(n_output != -1) lda_machine->n_outputs = n_output;
 63 	else n_output = lda_machine->n_outputs;
 64 
 65 
 66 	//
 67 	int dimIn;
 68 	int n_patterns;
 69 
 70 	DiskXFile *pf = NULL;
 71 	pf = new DiskXFile(bindata_filename, "r");
 72 
 73 	pf->read(&n_patterns, sizeof(int), 1);
 74 	pf->read(&dimIn, sizeof(int), 1);
 75 
 76 	if(verbose_level >= 1)
 77 	{
 78 		print("n_inputs : %d\n", dimIn);
 79 		print("n_patterns : %d\n", n_patterns);  
 80 	}
 81 
 82 	if(n_input > dimIn)
 83 	{
 84 		error("Number of inputs specified (%d) bigger than into the file (%d)", n_input, dimIn);
 85 	   
 86 		delete pf;
 87 
 88 		return 0;
 89 	}
 90 
 91 
 92 	//
 93 	float *realinput = NULL;
 94         Sequence *seq;
 95 
 96         realinput = new float [n_input];
 97         seq = new Sequence(&realinput, 1, n_input);	
 98 
 99 	//
100 	DiskXFile *pfOutput = NULL;
101 	pfOutput = new DiskXFile(filename_out, "w");
102 
103 	//
104 	//
105 	if(verbose_level >= 1)
106 		print("Projection bindata file into PCA space (%d -> %d) ...\n", n_input, n_output);
107 
108 	int P = n_patterns;
109 
110 	pfOutput->write(&P, sizeof(int), 1);
111 	pfOutput->write(&n_output, sizeof(int), 1);
112 
113 	for(int p = 0 ; p < P ; p++)
114 	{
115 	   	//
116 	  	for(int i = 0 ; i < n_input ; i++)
117 			pf->read(&realinput[i], sizeof(float), 1);   
118 
119 		if(verbose_level >= 2) 
120 			print(" Seq =     [%2.3f %2.3f %2.3f ...]\n", realinput[0], realinput[1], realinput[2]);
121 
122 		//
123 		lda_machine->forward(seq);
124 			
125 		if(verbose_level >= 2) 
126 			print(" Output =   [%2.3f %2.3f %2.3f ...]\n", lda_machine->outputs->frames[0][0], lda_machine->outputs->frames[0][1], lda_machine->outputs->frames[0][2]);
127 
128 		float *machine_output = lda_machine->outputs->frames[0];
129 
130 		//
131 		float data;
132 		for(int i = 0 ; i < n_output ; i++)
133 		{
134 		   	data = machine_output[i];
135 
136 			pfOutput->write(&data, sizeof(float), 1);   
137 		}
138 	}
139 
140 	//
141 	delete pf;
142 	delete pfOutput;
143 	
144 	//
145 	delete seq;
146 	delete [] realinput;
147 	delete lda_machine;
148 	
149 
150 	
151   	return 0;
152 }
153