testPixStump.cc

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