testMLP.cc

  1 const char *help = "\
  2 progname: testMLP.cc\n\
  3 code2html: This program tests a MLP.\n\
  4 version: Torch3 vision2.0, 2003-2005\n\
  5 (c) Sebastien Marcel (marcel@idiap.ch)\n";
  6 
  7 /** Torch
  8 */
  9 #include "DiskXFile.h"
 10 
 11 // command-lines
 12 #include "FileListCmdOption.h"
 13 #include "CmdLine.h"
 14 
 15 /** Torch3vision
 16 */
 17 
 18 // datasets
 19 #include "FileBinDataSet.h"
 20 #include "DiskBinDataSet.h"
 21 
 22 // custommachines
 23 #include "MyMLP.h"
 24 #include "MyMeanVarNorm.h"
 25 
 26 // image processing
 27 #include "ipHistoEqual.h"
 28 #include "ipSmoothGaussian3.h"
 29 
 30 using namespace Torch;
 31 
 32 int main(int argc, char **argv)
 33 {
 34 	//
 35   	int n_inputs;
 36   	char *model_file;
 37 
 38 	//
 39 	char *output_basename;
 40 	int n_histo_bins;
 41 	int width_pattern;
 42 	int height_pattern;
 43 	bool image_normalize; 
 44 
 45 	//
 46 	bool verbose;
 47 	bool use_disk;
 48 
 49 	//
 50 	real threshold;
 51 	
 52   	Allocator *allocator = new Allocator;
 53   	DiskXFile::setLittleEndianMode();
 54 
 55   	//=================== The command-line ==========================
 56 
 57         FileListCmdOption filelist_class("file name", "the list files or one data file of positive patterns");
 58         filelist_class.isArgument(true);
 59 
 60   	// Construct the command line
 61   	CmdLine cmd;
 62 	cmd.setBOption("write log", false);
 63 
 64   	// Put the help line at the beginning
 65   	cmd.info(help);
 66 
 67   	// Train mode
 68   	cmd.addText("\nArguments:");
 69   	cmd.addSCmdArg("model", &model_file, "the model file");
 70 	cmd.addCmdOption(&filelist_class);
 71   	cmd.addICmdArg("n_inputs", &n_inputs, "input dimension of the data");
 72   	cmd.addText("\nOptions:");
 73   	cmd.addSCmdOption("-o", &output_basename, "test", "output basename");
 74   	cmd.addICmdOption("-nbins", &n_histo_bins, 100, "number of patterns to output");
 75   	cmd.addBCmdOption("-verbose", &verbose, false, "verbose");
 76   	cmd.addBCmdOption("-usedisk", &use_disk, false, "read data from disk");
 77   	cmd.addRCmdOption("-threshold", &threshold, 0.0, "threshold");
 78 
 79   	cmd.addText("\nImage Options:");
 80   	cmd.addICmdOption("-width", &width_pattern, 19, "the width of the pattern");
 81   	cmd.addICmdOption("-height", &height_pattern, 19, "the height of the pattern");
 82   	cmd.addBCmdOption("-imagenorm", &image_normalize, false, "considers the input pattern as an image and performs a photometric normalization");
 83 
 84   	// Read the command line
 85   	cmd.read(argc, argv);
 86 
 87 	//
 88 	ipHistoEqual *enhancing = NULL;
 89 	ipCore *smoothing = NULL;
 90 	
 91 	if(image_normalize)
 92 	{
 93 		print("Perform photometric normalization ...\n");
 94 
 95 		if(width_pattern * height_pattern != n_inputs) error("incorrect image size.");
 96 		
 97 		print("The input pattern is an %dx%d image.\n", width_pattern, height_pattern);
 98 
 99 		enhancing = new(allocator) ipHistoEqual(width_pattern, height_pattern, "float");
100 		smoothing = new(allocator) ipSmoothGaussian3(width_pattern, height_pattern, "gray", 0.25);
101 	}
102 
103 	//
104         print("n_filenames = %d\n", filelist_class.n_files);
105         for(int i = 0 ; i < filelist_class.n_files ; i++)
106                 print("   filename[%d] = %s\n", i, filelist_class.file_names[i]);
107 
108   	//
109 	MyMLP mlp;
110 
111   	// Create the training dataset (normalize inputs)
112   	MyMeanVarNorm *mv_norm = NULL;  
113     	mv_norm = new(allocator) MyMeanVarNorm(n_inputs, 1);
114    
115 	mlp.load(model_file, mv_norm);
116 	mlp.info();
117 
118 	DataSet *bindata;
119 
120 	if(use_disk)
121 	{
122 		DiskBinDataSet *bindata_ = NULL;
123 		bindata_ = new(allocator) DiskBinDataSet(filelist_class.file_names, filelist_class.n_files, n_inputs, -1);
124 
125 		bindata = bindata_;
126 		bindata_->info(false);	
127 	}
128 	else
129 	{
130     		FileBinDataSet *bindata_ = NULL;
131 		bindata_ = new(allocator) FileBinDataSet(filelist_class.file_names, filelist_class.n_files, n_inputs);
132 
133 		bindata = bindata_;
134 		bindata_->info(false);	
135 	}
136 
137 		
138 
139 	real min_ = 1000.0;
140 	real max_ = -1000.0;
141 
142 	
143 
144 	//
145 	real *outputs = (real *) allocator->alloc(bindata->n_examples * sizeof(real));
146 
147 	//
148 	char output_filename[250];
149 	sprintf(output_filename, "%s.output", output_basename);
150 	DiskXFile *pf_output = new(allocator) DiskXFile(output_filename, "w");
151 
152 	int n_over = 0;
153 
154 	for(int i=0; i< bindata->n_examples; i++)
155 	{
156 	   	if(verbose) 
157 			print("[%d]:\n", i);
158 
159 	   	bindata->setExample(i);
160 		
161 	   	if(verbose) 
162 			print(" Input =   [%2.3f %2.3f %2.3f ...]\n", bindata->inputs->frames[0][0], bindata->inputs->frames[0][1], bindata->inputs->frames[0][2]);
163 
164 		if(image_normalize)
165 		{
166 
167 			enhancing->process(bindata->inputs);
168 			smoothing->process(enhancing->seq_out);
169 
170 			mv_norm->preProcessInputs(smoothing->seq_out);
171 		
172 			mlp.forward(smoothing->seq_out);
173 		}
174 		else
175 		{
176 			mv_norm->preProcessInputs(bindata->inputs);
177 		
178 			mlp.forward(bindata->inputs);
179 		}
180 		
181 		outputs[i] = mlp.outputs->frames[0][0];
182 	
183 		if(outputs[i] >= threshold) n_over++;
184 
185 	   	if(verbose) 
186 			print(" -> %g\n", outputs[i]);
187 
188 		pf_output->printf("%g\n", outputs[i]);
189 		
190 		if(i == 0)
191 		{
192 			min_ = outputs[i];
193 			max_ = outputs[i];
194 		}
195 		else
196 		{
197 			if(outputs[i] < min_) min_ = outputs[i];
198 			if(outputs[i] > max_) max_ = outputs[i];
199 		}
200 	}
201 	
202 	print("min = %g\n", min_);
203 	print("max = %g\n", max_);
204 	real rate = (real) n_over / (real) bindata->n_examples;
205 	print("# patterns >= %g = %g % (%d / %d)\n", threshold, rate*100.0, n_over, bindata->n_examples);
206 
207 
208 	//
209 	int *histo = (int *) allocator->alloc(n_histo_bins * sizeof(int));
210 	for(int i = 0 ; i < n_histo_bins ; i++) histo[i] = 0;
211 
212 	//
213 	real n_1 = n_histo_bins - 1;
214 	for(int i = 0 ; i < bindata->n_examples ; i++)
215 	{
216 		int index = FixI(n_1 * (outputs[i] - min_) / (max_ - min_));
217 
218 		histo[index]++;
219 	}
220 
221 	//
222 	real histo_max_ = 0.0;
223 	for(int i = 0 ; i < n_histo_bins ; i++)
224 		if(histo[i] > histo_max_) histo_max_ = histo[i];
225 
226 	//
227 	char histo_filename[250];
228 	sprintf(histo_filename, "%s.histo", output_basename);
229 	DiskXFile *pf_histo = new(allocator) DiskXFile(histo_filename, "w");
230 
231 	for(int i = 0 ; i < n_histo_bins ; i++)
232 		if(histo[i] != 0)
233 		{
234 			real output_ = (real) i * (max_ - min_) / n_1 + min_;
235 			
236 			real histo_ = (real) histo[i] / histo_max_;
237 
238 			pf_histo->printf("%g %g\n", output_, histo_);
239 		}
240 
241 	//
242   	delete allocator;
243 
244   	return(0);
245 }