trainPixStump.cc

  1 const char *help = "\
  2 progname: trainPixStump.cc\n\
  3 code2html: This program trains a linear combination 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 // trainers
 10 #include "TwoClassFormat.h"
 11 #include "Boosting.h"
 12 
 13 // measurers
 14 #include "ClassMeasurer.h"
 15 
 16 // command-lines
 17 #include "FileListCmdOption.h"
 18 #include "CmdLine.h"
 19 
 20 /** Torch3vision
 21 */
 22 
 23 // Stump machines and trainers
 24 #include "StumpMachine.h"
 25 #include "StumpTrainer.h"
 26 #include "ImageWeightedSumMachine.h"
 27 
 28 // datasets
 29 #include "FileBinDataSet.h"
 30 
 31 // image processing
 32 #include "ipHistoEqual.h"
 33 #include "ipNormMeanStdvLight.h"
 34 
 35 using namespace Torch;
 36 
 37 int main(int argc, char **argv)
 38 {
 39    	//
 40    	int width;
 41 	int height;
 42 
 43 	//
 44 	int n_trainers = 10;
 45 	
 46 	//
 47 	char *model_filename;
 48 
 49 	//
 50 	bool image_normalize;
 51 	bool equal_histo;
 52 	
 53 	Allocator *allocator = new Allocator;
 54 
 55 	FileListCmdOption filelist_class1("file name", "the list files or one data file of positive patterns");
 56 	filelist_class1.isArgument(true);
 57 
 58 	FileListCmdOption filelist_class0("file name", "the list files or one data file of negative patterns");
 59 	filelist_class0.isArgument(true);
 60 
 61 	//
 62 	// Prepare the command-line
 63 	CmdLine cmd;
 64 	cmd.setBOption("write log", false);
 65 	cmd.info(help);
 66 	cmd.addText("\nArguments:");
 67 	cmd.addCmdOption(&filelist_class1);
 68 	cmd.addCmdOption(&filelist_class0);
 69 	cmd.addICmdArg("width", &width, "width");
 70 	cmd.addICmdArg("height", &height, "height");
 71 	cmd.addText("\nOptions:");
 72 	cmd.addBCmdOption("-imagenorm", &image_normalize, false, "considers the input pattern as an image and performs a photometric normalization");
 73 	cmd.addBCmdOption("-equalh", &equal_histo, false, "perform histogram equalization");
 74 	cmd.addICmdOption("-n", &n_trainers, 10, "number of classifiers to train");
 75 	cmd.addSCmdOption("-o", &model_filename, "model.wsm", "model filename");
 76 
 77 	//
 78 	// Read the command-line
 79 	cmd.read(argc, argv);
 80 
 81 	//
 82 	print(" + class 1:\n");
 83         print("   n_filenames = %d\n", filelist_class1.n_files);
 84         for(int i = 0 ; i < filelist_class1.n_files ; i++)
 85                 print("   filename[%d] = %s\n", i, filelist_class1.file_names[i]);
 86 
 87         print(" + class 0:\n");
 88         print("   n_filenames = %d\n", filelist_class0.n_files);
 89         for(int i = 0 ; i < filelist_class0.n_files ; i++)
 90                 print("   filename[%d] = %s\n", i, filelist_class0.file_names[i]);
 91 
 92 	int n_inputs = width * height;
 93 
 94 	real the_target = 1.0;
 95 
 96 	FileBinDataSet *data = NULL;
 97 	data = new(allocator) FileBinDataSet(
 98 	      			filelist_class1.file_names, filelist_class1.n_files, the_target,
 99         			filelist_class0.file_names, filelist_class0.n_files, -the_target, n_inputs);
100 
101         data->info(false);
102 
103 	//
104 	if(image_normalize)
105 	{
106 	   	ipCore *imachine = NULL;
107 
108 		if(equal_histo)
109 			imachine = new(allocator) ipHistoEqual(width, height, "float");
110 		else 
111 			imachine = new(allocator) ipNormMeanStdvLight(width, height, "float");
112 	
113 		for(int i=0; i< data->n_examples; i++)
114                 {
115                         data->setExample(i);
116 
117                         imachine->process(data->inputs);
118                 }
119 
120 	}
121 	
122 	//
123 	Trainer **trainers = (Trainer **)allocator->alloc(n_trainers*sizeof(Trainer *));
124 	for(int j = 0 ; j < n_trainers ; j++)
125 	{
126 		StumpMachine *s_machine = new(allocator) StumpMachine(n_inputs);
127 		trainers[j] = new(allocator) StumpTrainer(s_machine);
128 		trainers[j]->setBOption("verbose", true);
129 	}
130 	 
131 	//
132 	ImageWeightedSumMachine *iwsm = new(allocator) ImageWeightedSumMachine(trainers, n_trainers, NULL);
133 
134 	//
135 	TwoClassFormat *class_format = new(allocator) TwoClassFormat(data);
136 	Boosting *boost = new(allocator) Boosting(iwsm, class_format);
137 
138 	//
139 	MeasurerList measurers;
140         ClassMeasurer *class_meas = new(allocator) ClassMeasurer(iwsm->outputs, data, class_format, cmd.getXFile("the_class_err"));
141         measurers.addNode(class_meas);
142 
143 	//
144 	boost->train(data, &measurers);
145 
146 	//
147 	DiskXFile *model = new(allocator) DiskXFile(model_filename, "w");
148 	model->taggedWrite(&n_inputs, sizeof(int), 1, "N_INPUTS");
149 	model->taggedWrite(&n_trainers, sizeof(int), 1, "N_TRAINERS");
150 	iwsm->saveXFile(model);
151 
152 	//
153   	delete allocator;
154 
155   	return(0);
156 }