lda2ilda.cc

  1 #include "DiskXFile.h"
  2 #include "ImageGray.h"
  3 
  4 #include "LDAMachine.h"
  5 #include "matrix.h"
  6 #include "MyMeanVarNorm.h"
  7 
  8 #include "CmdLine.h"
  9 
 10 using namespace Torch;
 11 
 12 const char *help = "This program reads a bindata file and a iLDA model and regenerate patterns in the input space\
 13 (c) Sebastien Marcel 2003-2004\n";
 14 
 15 int main(int argc, char *argv[])
 16 {
 17 	char *bindata_filename;
 18 	char *lda_filename;
 19 	char *ilda_filename;
 20 	int n_input;
 21 	char *filename_out;
 22 	bool verbose;
 23 	int verbose_level;
 24 	bool dnorm;
 25 	CmdLine cmd;
 26 	cmd.setBOption("write log", false);
 27 	
 28 	cmd.info(help);
 29 	cmd.addText("\nArguments:");
 30 	cmd.addSCmdArg("bindata filename", &bindata_filename, "bindata filename");
 31 	cmd.addSCmdArg("model filename", &lda_filename, "LDA model filename");
 32 	cmd.addSCmdArg("model filename", &ilda_filename, "iLDA model filename");
 33 	cmd.addICmdArg("n_input", &n_input, "number of inputs");
 34 	cmd.addText("\nOptions:");
 35 	cmd.addSCmdOption("-o", &filename_out, "ilda.bindata", "bindata output file");
 36 	cmd.addBCmdOption("-verbose", &verbose, false, "verbose");
 37 	cmd.addICmdOption("-verbose_level", &verbose_level, 1, "level of verbose");
 38 	cmd.addBCmdOption("-dnorm", &dnorm, false, "use dnorm");
 39 	cmd.read(argc, argv);
 40 
 41 	if(verbose == false) verbose_level = 0;
 42 
 43 
 44 	//
 45 	LDAMachine *lda_machine = NULL;
 46 	lda_machine = new LDAMachine(n_input, dnorm);
 47 
 48 	if(verbose_level >= 1)
 49 		print("Loading LDA model: %s ...\n", lda_filename);
 50 
 51 	DiskXFile *file = NULL;
 52 	file = new DiskXFile(lda_filename, "r");
 53 	lda_machine->loadXFile(file);
 54 	delete file;
 55 	
 56 	//
 57 	lda_machine->setIOption("verbose_level", 1);
 58 	lda_machine->init();
 59 
 60 	//
 61 	Mat *E_inv = NULL;
 62 	E_inv = new Mat(n_input, n_input);
 63 	DiskXFile *pfMxInverse = NULL;
 64 
 65 	print("Loading matrix inverse %s ...\n", ilda_filename);
 66 	pfMxInverse = new DiskXFile(ilda_filename, "r");
 67 	int n_input_;
 68 	pfMxInverse->read(&n_input_, sizeof(int), 1);
 69 	if(n_input_ != n_input)
 70 		error("Number of inputs %d != %d incorrect\n", n_input_, n_input);
 71        	for(int j = 0 ; j < n_input ; j++)
 72 		pfMxInverse->read(E_inv->ptr[j], sizeof(double), n_input);
 73 
 74 	delete pfMxInverse;
 75 
 76 	//
 77 	int dimIn;
 78 	int n_patterns;
 79 		
 80 	DiskXFile *pf = NULL;
 81 	pf = new DiskXFile(bindata_filename, "r");
 82 
 83 	pf->read(&n_patterns, sizeof(int), 1);
 84 	pf->read(&dimIn, sizeof(int), 1);
 85 
 86 	if(verbose_level >= 1)
 87 	{
 88 		print("n_inputs : %d\n", dimIn);
 89 		print("n_patterns : %d\n", n_patterns);  
 90 	}
 91 
 92 	if(dimIn > n_input)
 93 	{
 94 		error("Number of inputs specified (%d) lower than into the file (%d)\n", n_input, dimIn);
 95 	   
 96 		delete pf;
 97 
 98 		return 0;
 99 	}
100 
101 
102 	//
103 	float *realinput = NULL;
104         Sequence *seq;
105 	float *ilda = NULL;
106 
107         realinput = new float [n_input];
108         seq = new Sequence(&realinput, 1, n_input);	
109         ilda = new float [n_input];
110 
111 	//
112 	DiskXFile *pfOutput = NULL;
113 	pfOutput = new DiskXFile(filename_out, "w");
114 	
115 	//
116 	if(verbose_level >= 1)
117 		print("Projection bindata file into iPCA space (%d -> %d) ...\n", dimIn, n_input);
118 
119 	int P = n_patterns;
120 
121 	pfOutput->write(&P, sizeof(int), 1);
122 	pfOutput->write(&n_input, sizeof(int), 1);
123 	
124 	for(int p = 0 ; p < P ; p++)
125 	{
126 	   	//
127 	  	for(int i = 0 ; i < dimIn ; i++)
128 			pf->read(&realinput[i], sizeof(float), 1);   
129 
130 	   	if(verbose_level >= 2)
131 			print(" Seq =     [%2.3f %2.3f %2.3f ...]\n", realinput[0], realinput[1], realinput[2]);
132 
133 		if(dnorm)
134 			for(int i = 0 ; i < n_input ; i++) 
135 		   		realinput[i] += lda_machine->d_m_mean_[i];
136 
137 		for(int i = 0 ; i < n_input ; i++)
138 		{
139 			ilda[i] = 0.0;
140 
141 			for(int j = 0 ; j < dimIn ; j++)
142 				ilda[i] += E_inv->ptr[j][i] * realinput[j];
143 		}
144 		
145 		//
146 		float data;
147 		for(int i = 0 ; i < n_input ; i++)
148 		{
149 		   	data = ilda[i];
150 
151 			pfOutput->write(&data, sizeof(float), 1);   
152 		}
153 	}
154 
155 	//
156 	delete pf;
157 	delete pfOutput;
158 	
159 	//
160 	delete seq;
161 	delete [] realinput;
162 	delete [] ilda;
163 	delete lda_machine;
164 	delete E_inv;
165 	
166   	return 0;
167 }
168