trainPixRealStump.cc

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