testPixRealStump.cc

  1 const char *help = "\
  2 progname: testPixRealStump.cc\n\
  3 code2html: This program tests a linear combinaison of pixel-based real stump classifiers.\n\
  4 version: Torch3 vision2.0, 2004-2005\n\
  5 (c) Sebastien Marcel (marcel@idiap.ch) and Yann Rodriguez (rodrig@idiap.ch)\n";
  6 
  7 // Torch
  8 
  9 // command-lines
 10 #include "FileListCmdOption.h"
 11 #include "CmdLine.h"
 12 
 13 /** Torch3vision
 14 */
 15 
 16 // Stump machines
 17 #include "RealStumpMachine.h"
 18 #include "ImageWeightedSumMachine.h"
 19 
 20 // datasets
 21 #include "FileBinDataSet.h"
 22 
 23 // image processing
 24 #include "ipHistoEqual.h"
 25 #include "ipNormMeanStdvLight.h"
 26 
 27 using namespace Torch;
 28 
 29 #define FixI(v) (int) (v+0.5)
 30 
 31 int main(int argc, char **argv)
 32 {
 33    	//
 34    	int width;
 35 	int height;
 36 
 37 	//
 38 	char *model_filename;
 39 
 40 	//
 41 	bool image_normalize;
 42 	bool equal_histo;
 43 	
 44 	//
 45 	char *output_basename;
 46 	int n_histo_bins;
 47 
 48 	//
 49 	bool verbose;
 50 
 51 	Allocator *allocator = new Allocator;
 52 
 53 	FileListCmdOption filelist_class("file name", "the list files or one data file of patterns");
 54 	filelist_class.isArgument(true);
 55 
 56 	//
 57 	// Prepare the command-line
 58 	CmdLine cmd;
 59 	cmd.setBOption("write log", false);
 60 	cmd.info(help);
 61 	cmd.addText("\nArguments:");
 62 	cmd.addSCmdArg("model", &model_filename, "model filename");
 63 	cmd.addCmdOption(&filelist_class);
 64 	cmd.addICmdArg("width", &width, "width");
 65 	cmd.addICmdArg("height", &height, "height");
 66 	cmd.addText("\nOptions:");
 67 	cmd.addBCmdOption("-imagenorm", &image_normalize, false, "considers the input pattern as an image and performs a photometric normalization");
 68 	cmd.addBCmdOption("-equalh", &equal_histo, false, "perform histogram equalization");
 69   	cmd.addICmdOption("-nbins", &n_histo_bins, 100, "number of patterns to output");
 70   	cmd.addBCmdOption("-verbose", &verbose, false, "verbose");
 71   	cmd.addSCmdOption("-o", &output_basename, "test", "output basename");
 72 
 73 	//
 74 	// Read the command-line
 75 	cmd.read(argc, argv);
 76 
 77 	//
 78         print("n_filenames = %d\n", filelist_class.n_files);
 79         for(int i = 0 ; i < filelist_class.n_files ; i++)
 80                 print("   filename[%d] = %s\n", i, filelist_class.file_names[i]);
 81 
 82 	//
 83 	int n_trainers;
 84 	int n_inputs = width * height;
 85 
 86 	FileBinDataSet *data = NULL;
 87 	data = new(allocator) FileBinDataSet(filelist_class.file_names, filelist_class.n_files, n_inputs);
 88 
 89         data->info(false);
 90 
 91 	//
 92 	if(image_normalize)
 93 	{
 94 	   	ipCore *imachine = NULL;
 95 
 96 		if(equal_histo)
 97 			imachine = new(allocator) ipHistoEqual(width, height, "float");
 98 		else 
 99 			imachine = new(allocator) ipNormMeanStdvLight(width, height, "float");
100 		
101 		for(int i=0; i< data->n_examples; i++)
102                 {
103                         data->setExample(i);
104 
105                         imachine->process(data->inputs);
106                 }
107 
108 	}
109 
110 	//
111 	print("Loading model %s ...\n", model_filename);
112 	
113 	DiskXFile *model = new(allocator) DiskXFile(model_filename, "r");
114 	model->taggedRead(&n_inputs, sizeof(int), 1, "N_INPUTS");
115 	model->taggedRead(&n_trainers, sizeof(int), 1, "N_TRAINERS");
116 
117 	print(" + n_inputs = %d\n", n_inputs);
118 	print(" + n_trainers = %d\n", n_trainers);
119 	
120 	//
121 	Machine **machines = (Machine **)allocator->alloc(n_trainers*sizeof(Machine *));
122 	for(int j = 0 ; j < n_trainers ; j++)
123 		machines[j] = new(allocator) RealStumpMachine(n_inputs);
124 	ImageWeightedSumMachine *iwsm = new(allocator) ImageWeightedSumMachine(machines, n_trainers);
125 
126 	//
127 	iwsm->loadXFile(model);
128 
129 	//
130 	real min_ = 1000.0;
131 	real max_ = -1000.0;
132 
133 	//
134 	real *outputs = (real *) allocator->alloc(data->n_examples * sizeof(real));
135 
136 	//
137 	char output_filename[250];
138 	sprintf(output_filename, "%s.output", output_basename);
139 	DiskXFile *pf_output = new(allocator) DiskXFile(output_filename, "w");
140 
141 	for(int i=0; i<data->n_examples; i++)
142         {
143 		data->setExample(i);
144 		iwsm->forward(data->inputs);
145 
146 		outputs[i] = iwsm->outputs->frames[0][0];
147 
148 	   	if(verbose) 
149 			print(" -> %g\n", outputs[i]);
150 
151 		pf_output->printf("%g\n", outputs[i]);
152 
153 		if(i == 0)
154 		{
155 			min_ = outputs[i];
156 			max_ = outputs[i];
157 		}
158 		else
159 		{
160 			if(outputs[i] < min_) min_ = outputs[i];
161 			if(outputs[i] > max_) max_ = outputs[i];
162 		}
163 	}
164 	
165 	print("min = %g\n", min_);
166 	print("max = %g\n", max_);
167 
168 	//
169 	int *histo = (int *) allocator->alloc(n_histo_bins * sizeof(int));
170 	for(int i = 0 ; i < n_histo_bins ; i++) histo[i] = 0;
171 
172 	//
173 	real n_1 = n_histo_bins - 1;
174 	for(int i = 0 ; i < data->n_examples ; i++)
175 	{
176 		int index = FixI(n_1 * (outputs[i] - min_) / (max_ - min_));
177 
178 		histo[index]++;
179 	}
180 
181 	//
182 	real histo_max_ = 0.0;
183 	for(int i = 0 ; i < n_histo_bins ; i++)
184 		if(histo[i] > histo_max_) histo_max_ = histo[i];
185 
186 	//
187 	char histo_filename[250];
188 	sprintf(histo_filename, "%s.histo", output_basename);
189 	DiskXFile *pf_histo = new(allocator) DiskXFile(histo_filename, "w");
190 
191 	for(int i = 0 ; i < n_histo_bins ; i++)
192 		if(histo[i] != 0)
193 		{
194 			real output_ = (real) i * (max_ - min_) / n_1 + min_;
195 			
196 			real histo_ = (real) histo[i] / histo_max_;
197 
198 			pf_histo->printf("%g %g\n", output_, histo_);
199 		}
200 
201 	//
202   	delete allocator;
203 
204   	return(0);
205 }