pca2ipca.cc

  1 #include "DiskXFile.h"
  2 #include "ImageGray.h"
  3 
  4 #include "PCAMachine.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 iPCA 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 *pca_filename;
 19 	char *ipca_filename;
 20 	int n_input;
 21 	char *filename_out;
 22 	bool verbose;
 23 	int verbose_level;
 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", &pca_filename, "PCA model filename");
 31 	cmd.addSCmdArg("model filename", &ipca_filename, "iPCA model filename");
 32 	cmd.addICmdArg("n_input", &n_input, "number of inputs");
 33 	cmd.addText("\nOptions:");
 34 	cmd.addSCmdOption("-o", &filename_out, "ipca.bindata", "bindata output file");
 35 	cmd.addBCmdOption("-verbose", &verbose, false, "verbose");
 36 	cmd.addICmdOption("-verbose_level", &verbose_level, 1, "level of verbose");
 37 	cmd.read(argc, argv);
 38 
 39 	if(verbose == false) verbose_level = 0;
 40 
 41 
 42 	//
 43 	PCAMachine *pca_machine = NULL;
 44 	pca_machine = new PCAMachine(n_input);
 45 
 46 	if(verbose_level >= 1)
 47 		print("Loading PCA model: %s ...\n", pca_filename);
 48 
 49 	DiskXFile *file = NULL;
 50 	file = new DiskXFile(pca_filename, "r");
 51 	pca_machine->loadXFile(file);
 52 	delete file;
 53 	
 54 	//
 55 	pca_machine->setIOption("verbose_level", 1);
 56 	pca_machine->init();
 57 
 58 	//
 59 	Mat *E_inv = NULL;
 60 	E_inv = new Mat(n_input, n_input);
 61 	DiskXFile *pfMxInverse = NULL;
 62 
 63 	print("Loading matrix inverse %s ...\n", ipca_filename);
 64 	pfMxInverse = new DiskXFile(ipca_filename, "r");
 65 	int n_input_;
 66 	pfMxInverse->read(&n_input_, sizeof(int), 1);
 67 	if(n_input_ != n_input)
 68 		error("Number of inputs %d != %d incorrect\n", n_input_, n_input);
 69        	for(int j = 0 ; j < n_input ; j++)
 70 		pfMxInverse->read(E_inv->ptr[j], sizeof(double), n_input);
 71 
 72 	delete pfMxInverse;
 73 
 74 	//
 75 	int dimIn;
 76 	int n_patterns;
 77 		
 78 	DiskXFile *pf = NULL;
 79 	pf = new DiskXFile(bindata_filename, "r");
 80 
 81 	pf->read(&n_patterns, sizeof(int), 1);
 82 	pf->read(&dimIn, sizeof(int), 1);
 83 
 84 	if(verbose_level >= 1)
 85 	{
 86 		print("n_inputs : %d\n", dimIn);
 87 		print("n_patterns : %d\n", n_patterns);  
 88 	}
 89 
 90 	if(dimIn > n_input)
 91 	{
 92 		error("Number of inputs specified (%d) lower than into the file (%d)\n", n_input, dimIn);
 93 	   
 94 		delete pf;
 95 
 96 		return 0;
 97 	}
 98 
 99 
100 	//
101 	float *realinput = NULL;
102         Sequence *seq;
103 	float *ipca = NULL;
104 
105         realinput = new float [n_input];
106         seq = new Sequence(&realinput, 1, n_input);	
107         ipca = new float [n_input];
108 
109 	//
110 	DiskXFile *pfOutput = NULL;
111 	pfOutput = new DiskXFile(filename_out, "w");
112 	
113 	//
114 	if(verbose_level >= 1)
115 		print("Projection bindata file into iPCA space (%d -> %d) ...\n", dimIn, n_input);
116 
117 	int P = n_patterns;
118 
119 	pfOutput->write(&P, sizeof(int), 1);
120 	pfOutput->write(&n_input, sizeof(int), 1);
121 	
122 	for(int p = 0 ; p < P ; p++)
123 	{
124 	   	//
125 	  	for(int i = 0 ; i < dimIn ; i++)
126 			pf->read(&realinput[i], sizeof(float), 1);   
127 
128 	   	if(verbose_level >= 2)
129 			print(" Seq =     [%2.3f %2.3f %2.3f ...]\n", realinput[0], realinput[1], realinput[2]);
130 
131 		for(int i = 0 ; i < n_input ; i++)
132 		{
133 			ipca[i] = 0.0;
134 
135 			for(int j = 0 ; j < dimIn ; j++)
136 				ipca[i] += E_inv->ptr[j][i] * realinput[j];
137 		}
138 		
139 		for(int i = 0 ; i < n_input ; i++) 
140 		{
141 		   	ipca[i] += pca_machine->Xm[i];
142 
143 			if(ipca[i] < 0.0) ipca[i] = 0.0;
144 			if(ipca[i] > 1.0) ipca[i] = 1.0;
145 		}
146 
147 		//
148 		float data;
149 		for(int i = 0 ; i < n_input ; i++)
150 		{
151 		   	data = ipca[i];
152 
153 			pfOutput->write(&data, sizeof(float), 1);   
154 		}
155 	}
156 
157 	//
158 	delete pf;
159 	delete pfOutput;
160 	
161 	//
162 	delete seq;
163 	delete [] realinput;
164 	delete [] ipca;
165 	delete pca_machine;
166 	delete E_inv;
167 	
168   	return 0;
169 }
170