testPCA.cc

  1 const char *help = "\
  2 testPCA (c) Sebastien Marcel 2003\n\
  3 \n\
  4 This program projects data into PCA sub-space\n";
  5 
  6 #include "FileBinDataSet.h"
  7 #include "PCATrainer.h"
  8 #include "MyMeanVarNorm.h"
  9 #include "HistoEqualSmoothImagePreProcessing.h"
 10 #include "FileListCmdOption.h"
 11 #include "CmdLine.h"
 12 
 13 using namespace Torch;
 14 			
 15 int main(int argc, char **argv)
 16 {
 17   	int n_inputs;
 18   	char *model_file;
 19 	bool verbose;
 20 	int verbose_level;
 21 	bool normalize;
 22 	bool image_normalize;
 23 	int width, height;
 24 	
 25   	Allocator *allocator = new Allocator;
 26   	DiskXFile::setLittleEndianMode();
 27 
 28 
 29 	
 30   	//=================== The command-line ==========================
 31 	FileListCmdOption filelist("file name", "the list files or one data file");
 32         filelist.isArgument(true);
 33 
 34   	// Construct the command line
 35   	CmdLine cmd;
 36 	cmd.setBOption("write log", false);
 37 	
 38   	// Put the help line at the beginning
 39   	cmd.info(help);
 40 
 41   	cmd.addText("\nArguments:");
 42   	cmd.addSCmdArg("model_file", &model_file, "model file", true);
 43   	cmd.addCmdOption(&filelist);
 44   	cmd.addICmdArg("n_inputs", &n_inputs, "input dimension of the data", true);
 45   	cmd.addText("\nOptions:");
 46   	cmd.addBCmdOption("-normalize", &normalize, false, "centroid normalization", true);
 47   	cmd.addBCmdOption("-verbose", &verbose, false, "verbose", true);
 48   	cmd.addICmdOption("-verbose_level", &verbose_level, 0, "verbose level", true);
 49 	cmd.addBCmdOption("-imagenorm", &image_normalize, false, "considers the input pattern as an image and performs a photometric normalization");
 50 	cmd.addICmdOption("-width", &width, -1, "width");
 51 	cmd.addICmdOption("-height", &height, -1, "height");
 52 
 53   	// Read the command line
 54   	cmd.read(argc, argv);
 55 
 56 	if(image_normalize)
 57 	{
 58 		print("Perform photometric normalization ...\n");
 59 
 60 		print("The input pattern is an %dx%d image.\n", width, height);
 61 	}
 62 
 63 
 64 		
 65 	//
 66 	if(verbose)
 67 	{
 68 		print(" + n_filenames = %d\n", filelist.n_files);
 69 		for(int i = 0 ; i < filelist.n_files ; i++)
 70 			print("   filename[%d] = %s\n", i, filelist.file_names[i]);
 71 	}
 72 
 73 
 74   	//
 75 	// The PCA Machine
 76 	PCAMachine *pca_machine = NULL;
 77 	pca_machine = new(allocator) PCAMachine(n_inputs);
 78 	pca_machine->setIOption("verbose_level", verbose_level);
 79 
 80 	//
 81 	PreProcessing *preprocess = NULL;
 82 	if(image_normalize)
 83 	{
 84 		print("Pre-processing image %dx%d ...\n", width, height);
 85 		preprocess = new(allocator) HistoEqualSmoothImagePreProcessing(width, height);
 86 	}
 87 
 88 	//
 89 	MyMeanVarNorm *mv_norm = NULL;
 90 	if(normalize)
 91 	{
 92 		print("Loading PCA model and its normalisation: %s ...\n", model_file);
 93 
 94 		mv_norm = new(allocator) MyMeanVarNorm(n_inputs, 1);
 95 
 96 	   	pca_machine->load(model_file, mv_norm);
 97 	}
 98 	else
 99 	{
100 		print("Loading PCA model: %s ...\n", model_file);
101 
102 		DiskXFile *file = NULL;
103 		file = new DiskXFile(model_file, "r");
104 		pca_machine->loadXFile(file);
105 		delete file;
106 	}
107 	
108 
109 	//
110 	pca_machine->setIOption("verbose_level", verbose_level);
111 	pca_machine->setROption("variance", 0.95);
112 	pca_machine->init();
113 
114 	//
115 	// Load all the data in the same dataset
116     	FileBinDataSet *bindata = new(allocator) FileBinDataSet(filelist.file_names, filelist.n_files, 0.0, n_inputs);
117 	bindata->info(false);	
118 
119 
120 	//
121 	real *realinput = NULL;
122         Sequence *seq;
123 
124         realinput = new real [n_inputs];
125         seq = new Sequence(&realinput, 1, n_inputs);
126 	
127 	for(int i=0; i< bindata->n_examples; i++)
128 	{
129 		if(verbose) 
130 	   		print("[%d]:\n", i);
131 
132 		//
133 		bindata->setExample(i);
134 		
135 		if(verbose) 
136 			print(" Input =   [%2.3f %2.3f %2.3f ...]\n", bindata->inputs->frames[0][0], bindata->inputs->frames[0][1], bindata->inputs->frames[0][2]);
137 
138 		//
139 		bindata->inputs->copyTo(realinput);
140 
141 		if(verbose) 
142 			print(" Seq =     [%2.3f %2.3f %2.3f ...]\n", realinput[0], realinput[1], realinput[2]);
143 
144 		if(image_normalize)
145 			preprocess->preProcessInputs(seq);
146 
147 		if(normalize)
148 			mv_norm->preProcessInputs(seq);
149 
150 		//
151 		pca_machine->forward(seq);
152 			
153 		if(verbose) 
154 			print(" Output =   [%2.3f %2.3f %2.3f ...]\n", pca_machine->outputs->frames[0][0], pca_machine->outputs->frames[0][1], pca_machine->outputs->frames[0][2]);
155 	}
156 
157 	//
158 	// Free memory
159 	delete [] realinput;
160 	delete seq;
161   	delete allocator;
162 
163   	return(0);
164 }