This repository has been archived by the owner on Jun 25, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TrainingParameters.h
371 lines (355 loc) · 11.8 KB
/
TrainingParameters.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#pragma once
// This file defines the Training and Program parameters objects which control the
// operation of the program
#include <assert.h>
#include <vector>
#include <string>
#include <algorithm>
#ifdef _OPENMP
#include <omp.h>
#endif
namespace MicrosoftResearch { namespace Cambridge { namespace Sherwood
{
/// <summary>
/// Decision tree training parameters.
/// </summary>
struct TrainingParameters
{
TrainingParameters()
{
// Some sane defaults will need to be changed per application.
NumberOfTrees = 1;
NumberOfCandidateFeatures = 10;
NumberOfCandidateThresholdsPerFeature = 10;
MaxDecisionLevels = 5;
Verbose = false;
MaxThreads = omp_get_max_threads();
}
// Number of trees in a forest
int NumberOfTrees;
// Number of candidate feature response functions (split functions) generated
// for evaluation
int NumberOfCandidateFeatures;
// Number of thresholds tested per candidate feature
unsigned int NumberOfCandidateThresholdsPerFeature;
// maximum number of tree levels
int MaxDecisionLevels;
bool Verbose;
// Maximum threads available for parallel training
int MaxThreads;
};
class ForestDescriptor
{
public:
enum e
{
Classification = 0,
Regression = 1,
ExpertRegressor = 2,
All = 3
};
};
class SplitFunctionDescriptor
{
public:
enum e
{
PixelDifference = 0,
RandomHyperplane = 1
};
};
///<summary>
/// Program params
///</summary>
struct ProgramParameters
{
// training parameters for classification or regression
TrainingParameters Tpc;
TrainingParameters Tpr;
// Forest output file name (suffix will be added)
std::string OutputFilename;
// Full path to training images
std::string TrainingImagesPath;
// first bit of image name eg img for img103ir.png
std::string InputPrefix;
int NumberTrainingImages;
// start number of training images
int TrainingImagesStart;
ForestDescriptor::e ForestType;
SplitFunctionDescriptor::e SplitFunctionType;
// for training an expert regressor
int ExpertClassNo;
// Depth raw is normalised, this'll always be false for
// mm scaled depth images
bool DepthRaw;
// side length of square patch centred on pixel-of-interest
int PatchSize;
// number of depth bins
int Bins;
int ImgHeight;
int ImgWidth;
// set true to include zero IR intensity pixels in training data
bool TrainOnZeroIR;
// maximum range
int MR;
// Threshold value for pre-processing
int Threshold;
// Ir images captured using the modified webcam?
bool Webcam;
// Majority of images within 0-300 mm?
bool Closeup;
ProgramParameters()
{
// Defaults. obviously the default paths and things will need
// overwriting in a config file, and default values may do too.
OutputFilename = "default";
TrainingImagesPath = "/media/james/data_wd/training_realsense";
InputPrefix = "img";
NumberTrainingImages = 10;
TrainingImagesStart = 0;
ForestType = ForestDescriptor::Classification;
SplitFunctionType = SplitFunctionDescriptor::PixelDifference;
ExpertClassNo = -1;
DepthRaw = false;
PatchSize = 25;
Bins = 5;
ImgHeight = 480;
ImgWidth = 640;
TrainOnZeroIR = true;
MR = 1200;
Threshold = 38;
Webcam = false;
Closeup = true;
}
bool setParam(std::string parameter, std::string value)
{
if(parameter.compare("TRAINING_IMAGE_PATH")==0)
{
if(IPUtils::dirExists(value))
TrainingImagesPath = value;
else
throw std::runtime_error("images path not found");
}
else if(parameter.compare("TRAINING_IMAGES") == 0)
{
int n = std::stoi(value);
if( n >= 10 )
{
NumberTrainingImages = n;
}
else
throw std::runtime_error("Number of training images must be at least 10");
}
else if(parameter.compare("IMAGES_START") == 0)
{
int n = std::stoi(value);
TrainingImagesStart = n;
}
else if(parameter.compare("DEPTH_BINS") == 0)
{
int n= std::stoi(value);
if((n>1)&&(n<6))
Bins = n;
else
throw std::runtime_error("Accepted numbers of bins are 2-5");
}
else if(parameter.compare("PATCH_SIZE") == 0)
{
int n= std::stoi(value);
if((n>=3)&&(n<256))
PatchSize = n;
else
throw std::runtime_error("Accepted patch sizes are 3-255");
}
else if(parameter.compare("DEPTH_RAW") == 0)
{
if(value.compare("NO")==0)
DepthRaw = false;
else if(value.compare("YES")==0)
DepthRaw = true;
else
throw std::runtime_error("Invalid value for DEPTH_RAW");
}
else if(parameter.compare("TYPE") == 0)
{
if(value.compare("CLASS")==0)
ForestType = ForestDescriptor::Classification;
else if(value.compare("REG")==0)
ForestType = ForestDescriptor::Regression;
else if(value.compare("EXPREG")==0)
ForestType = ForestDescriptor::ExpertRegressor;
else if(value.compare("ALL")==0)
ForestType = ForestDescriptor::All;
else
throw std::runtime_error("Invalid forest type. Accepted values are CLASS, REG, EXPREG, ALL");
}
else if(parameter.compare("TREES") == 0)
{
int n= std::stoi(value);
Tpc.NumberOfTrees = n;
Tpr.NumberOfTrees = n;
}
else if(parameter.compare("CLASS_LEVELS") == 0)
{
int n= std::stoi(value);
Tpc.MaxDecisionLevels = n;
}
else if(parameter.compare("REG_LEVELS") == 0)
{
int n= std::stoi(value);
Tpr.MaxDecisionLevels = n;
}
else if(parameter.compare("CANDIDATE_FEATURES") == 0)
{
int n= std::stoi(value);
Tpc.NumberOfCandidateFeatures = n;
Tpr.NumberOfCandidateFeatures = n;
}
else if(parameter.compare("THRESHOLDS_PER_FEATURE") == 0)
{
int n= std::stoi(value);
Tpc.NumberOfCandidateThresholdsPerFeature = n;
Tpr.NumberOfCandidateThresholdsPerFeature = n;
}
else if(parameter.compare("VERBOSE") == 0)
{
bool v;
if(value.compare("NO")==0)
v = false;
else if(value.compare("YES")==0)
v = true;
else
throw std::runtime_error("Invalid value for DEPTH_RAW");
Tpc.Verbose = v;
Tpr.Verbose = v;
}
else if(parameter.compare("EXPERT") == 0)
{
int n= std::stoi(value);
if(n>=-1 && n<Bins)
ExpertClassNo = n;
else
throw std::runtime_error("Expert outside resonable range");
}
else if(parameter.compare("MAX_THREADS") == 0)
{
int n= std::stoi(value);
if((n > omp_get_max_threads()) || (n == -1))
{
Tpc.MaxThreads = omp_get_max_threads();
Tpr.MaxThreads = omp_get_max_threads();
}
else if(n < -1)
{
throw std::runtime_error("Invalid number of threads");
}
else
{
Tpc.MaxThreads = n;
Tpr.MaxThreads = n;
}
}
else if(parameter.compare("SPLIT_FUNCTION")==0)
{
if(value.compare("PIXEL_DIFFERENCE") == 0)
SplitFunctionType = SplitFunctionDescriptor::PixelDifference;
else if(value.compare("RANDOM_HYPERPLANE")==0)
SplitFunctionType = SplitFunctionDescriptor::RandomHyperplane;
else
throw std::runtime_error("Invalid value for SPLIT_FUNCTION, accepted values are PIXEL_DIFFERENCE and RANDOM_HYPERPLANE");
}
else if(parameter.compare("FOREST_OUTPUT")==0)
{
OutputFilename = value;
}
else if(parameter.compare("INPUT_PREFIX")==0)
{
InputPrefix = value;
}
else if(parameter.compare("IMG_WIDTH") == 0)
{
int n= std::stoi(value);
ImgWidth = n;
}
else if(parameter.compare("IMG_HEIGHT") == 0)
{
int n= std::stoi(value);
ImgHeight = n;
}
else if(parameter.compare("TRAIN_ON_ZERO_IR") == 0)
{
if(value.compare("YES") == 0)
TrainOnZeroIR = true;
else if(value.compare("NO") == 0)
TrainOnZeroIR = false;
else
throw std::runtime_error("Invalid value for TRAIN_ON_ZERO_IR, expected YES or NO");
}
else if(parameter.compare("MAX_RANGE")==0)
{
int n = std::stoi(value);
if((n <= 1200)&&(n>=500))
MR = n;
else
throw std::runtime_error("Max range must be between 500 and 1200");
}
else if(parameter.compare("TH_VALUE")==0)
{
int n = std::stoi(value);
if((n<=255) && (n>=0))
Threshold = n;
else
throw std::runtime_error("Threshold must be between 0 and 255");
}
else if(parameter.compare("WEBCAM")==0)
{
if(value.compare("YES")==0)
Webcam = true;
else
Webcam = false;
}
else if(parameter.compare("IGNORE_CLOSE") == 0)
{
if(value.compare("YES")==0)
Closeup = false;
else
Closeup = true;
}
else
return false;
return true;
}
void prettyPrint()
{
std::string forestTypes [] = {"Classification", "Regression", "ExpertRegressor", "All"};
std::string splitTypes [] = {"Pixel Difference Response", "Random Hyperplane Response"};
std::cout << "Program Parameters:" << std::endl;
std::cout << std::endl;
std::cout << "Forest Type: \t\t\t" << forestTypes[ForestType] << std::endl;
std::cout << "Split Function Type: \t\t" << splitTypes[SplitFunctionType] << std::endl;
std::cout << "Number of bins: \t\t" << std::to_string(Bins) << std::endl;
std::cout << "Training images path: \t\t" << TrainingImagesPath << std::endl;
std::cout << "Training images file prefix: \t" << InputPrefix << std::endl;
std::cout << "NumberTrainingImages: \t\t" << std::to_string(NumberTrainingImages) << std::endl;
std::cout << "Starting at image: \t\t" << std::to_string(TrainingImagesStart) << std::endl;
std::string dr = DepthRaw? "True" : "False";
std::cout << "Depth Raw: \t\t\t" << dr << std::endl;
std::cout << "Patch Size: \t\t\t" << std::to_string(PatchSize) << std::endl;
std::cout << "Max Depth Range: \t\t" << std::to_string(MR) << std::endl;
std::cout << "Image Width:\t\t\t" << std::to_string(ImgWidth) << std::endl;
std::cout << "Image Height:\t\t\t" << std::to_string(ImgHeight) << std::endl;
std::cout << "Forest output prefix: \t\t" << OutputFilename << std::endl;
std::cout << "Trees per Forest:\t\t" << std::to_string(Tpr.NumberOfTrees) <<std::endl;
std::cout << "Regression Decision Levels: \t" << std::to_string(Tpr.MaxDecisionLevels) << std::endl;
std::cout << "Classification Decision Levels:\t" << std::to_string(Tpc.MaxDecisionLevels) << std::endl;
std::cout << "Candidate Features: \t\t" << std::to_string(Tpr.NumberOfCandidateFeatures) << std::endl;
std::cout << "Thresholds per Features: \t" << std::to_string(Tpr.NumberOfCandidateThresholdsPerFeature) << std::endl;
std::cout << "ExpertClassNo: \t\t\t" << std::to_string(ExpertClassNo) << std::endl;
std::cout << "Verbose: \t\t\t" << (Tpc.Verbose? "Yes" : "no") << std::endl;
std::cout << "Train on zero IR: \t\t" << (TrainOnZeroIR? "Yes" : "no") << std::endl;
std::cout << "IR threshold value: \t\t" << std::to_string(Threshold) << std::endl;
std::cout << "Webcam? \t\t\t" << (Webcam? "Yes" : "No") << std::endl;
std::cout << "Max threads to use: \t\t" << std::to_string(Tpr.MaxThreads) << std::endl;
}
};
} } }