bindata2pcalda.cc

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