-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRandomForest.h
581 lines (490 loc) · 14.7 KB
/
RandomForest.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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
// =========================================================================================
// Structured Class-Label in Random Forests. This is a re-implementation of
// the work we presented at ICCV'11 in Barcelona, Spain.
//
// In case of using this code, please cite the following paper:
// P. Kontschieder, S. Rota Bulò, H. Bischof and M. Pelillo.
// Structured Class-Labels in Random Forests for Semantic Image Labelling. In (ICCV), 2011.
//
// Implementation by Peter Kontschieder and Samuel Rota Bulò
// October 2013
//
// =========================================================================================
#ifndef RANDOMFOREST_H
#define RANDOMFOREST_H
#include <vector>
#include <cassert>
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <stdint.h>
#include "Global.h"
using namespace std;
namespace vision
{
// =====================================================================================
// Class: TNode
// Description:
// =====================================================================================
template<class SplitData, class Prediction>
class TNode
{
public:
// ==================== LIFECYCLE =======================================
TNode(int start, int end) :
left(NULL), right(NULL), start(start), end(end), depth(0)
{
static int iNode = 0;
idx = iNode++;
// cout<<endl<<"New node "<<idx<<" "<<hex<<this<<dec<<endl;
}
~TNode()
{
delete left;
delete right;
}
// ==================== ACCESSORS =======================================
bool isLeaf() const
{
return left == NULL;
}
int getStart() const
{
return start;
}
int getEnd() const
{
return end;
}
int getIdx() const
{
return idx;
}
int getNSamples() const
{
return end - start;
}
int getDepth() const
{
return depth;
}
const SplitData &getSplitData() const
{
return splitData;
}
const Prediction &getPrediction() const
{
return prediction;
}
const TNode<SplitData, Prediction>* getLeft() const
{
return left;
}
const TNode<SplitData, Prediction>* getRight() const
{
return right;
}
TNode<SplitData, Prediction>* getLeft()
{
return left;
}
TNode<SplitData, Prediction>* getRight()
{
return right;
}
// ==================== MUTATORS =======================================
void setSplitData(SplitData splitData)
{
this->splitData = splitData;
}
void setPrediction(Prediction prediction)
{
this->prediction = prediction;
}
void setDepth(uint16_t depth)
{
this->depth = depth;
}
void setEnd(uint32_t end)
{
this->end = end;
}
void setStart(uint32_t start)
{
this->start = start;
}
void split(uint32_t start, uint32_t middle)
{
assert(start >= this->start && middle >= start && middle <= end);
if (left == NULL)
{
left = new TNode<SplitData, Prediction>(start, middle);
right = new TNode<SplitData, Prediction>(middle, end);
left->setDepth(depth + 1);
right->setDepth(depth + 1);
}
else
{
left->setStart(start);
left->setEnd(middle);
right->setStart(middle);
}
}
// ==================== OPERATORS =======================================
protected:
// ==================== METHODS =======================================
// ==================== DATA MEMBERS =======================================
private:
// ==================== METHODS =======================================
// ==================== DATA MEMBERS =======================================
public:
TNode<SplitData, Prediction> *left, *right;
SplitData splitData;
Prediction prediction;
uint32_t start, end;
uint16_t depth;
uint32_t idx;
};
// ----- end of class TNode -----
template<class Sample, class Label>
struct LabelledSample
{
Sample sample;
Label label;
};
enum SplitResult
{
SR_LEFT = 0, SR_RIGHT = 1, SR_INVALID = 2
};
typedef vector<SplitResult> SplitResultsVector;
// =====================================================================================
// Class: RandomTree
// Description:
// =====================================================================================
template<class SplitData, class Sample, class Label, class Prediction, class ErrorData>
class RandomTree
{
public:
typedef LabelledSample<Sample, Label> LSample;
typedef vector<LSample> LSamplesVector;
// ==================== LIFECYCLE =======================================
RandomTree() :
root(NULL)
{
}
virtual ~RandomTree()
{
delete root;
root = NULL;
}
// ==================== ACCESSORS =======================================
void save(string filename, bool includeSamples = false) const
{
ofstream out(filename.c_str());
if (out.is_open()==false)
{
cout<<"Failed to open "<<filename<<endl;
return;
}
writeHeader(out);
out << endl;
write(root, out);
out << includeSamples << " ";
if (includeSamples)
write(samples, out);
}
Prediction predict(Sample &sample) const
{
assert(root != NULL);
TNode<SplitData, Prediction> *curNode = root;
SplitResult sr = SR_LEFT;
while (!curNode->isLeaf() && sr != SR_INVALID)
switch (sr = split(curNode->getSplitData(), sample))
{
case SR_LEFT:
curNode = curNode->getLeft();
break;
case SR_RIGHT:
curNode = curNode->getRight();
break;
default:
break;
}
return curNode->getPrediction();
}
// ==================== MUTATORS =======================================
void train(const LSamplesVector &trainingSamples, int nTrials, bool interleavedTraining = false)
{
samples.clear();
samples.resize(trainingSamples.size());
splitResults.resize(trainingSamples.size());
copy(trainingSamples.begin(), trainingSamples.end(), samples.begin());
root = new TNode<SplitData, Prediction>(0, (int)samples.size());
ErrorData errorData;
Prediction rootPrediction;
initialize(root, errorData, rootPrediction);
root->setPrediction(rootPrediction);
vector<TNode<SplitData, Prediction> *> nodeList[2], *curNodeList;
nodeList[0].reserve(samples.size());
nodeList[1].reserve(samples.size());
int nodeListPtr = 0;
curNodeList = &nodeList[nodeListPtr];
curNodeList->push_back(root);
cout<<"nTrials = "<<nTrials<<endl;
while (curNodeList->size() > 0)
{
if (interleavedTraining)
{
//double initialError = getError(errorData);
for (int t = 0; t < nTrials; ++t)
{
for (size_t i = 0; i < curNodeList->size(); ++i)
{
TNode<SplitData, Prediction> *node = (*curNodeList)[i];
tryImprovingSplit(errorData, node);
}
}
/*
double finalError = getError(errorData);
cout << "l " << level << " errorDelta: " << (finalError - initialError);
cout << endl;
*/
}
else
{
// double initialError = getError(errorData);
for (size_t i = 0; i < curNodeList->size(); ++i)
{
for (int t = 0; t < nTrials; ++t)
{
TNode<SplitData, Prediction> *node = (*curNodeList)[i];
// cout<<"Node "<<node->idx<<" try "<<t<<endl;
tryImprovingSplit(errorData, node);
}
}
/*
double finalError = getError(errorData);
cout << "l " << level << " errorDelta: " << (finalError - initialError);
cout << endl;
*/
}
int nextList = (++nodeListPtr) % 2;
nodeList[nextList].clear();
for (size_t i = 0; i < curNodeList->size(); ++i)
{
TNode<SplitData, Prediction> *node = (*curNodeList)[i];
if (!node->isLeaf())
{
#ifdef _DEBUG
cout << setprecision(4) << (float)(node->getLeft()->getEnd() - node->getStart())/(node->getEnd() - node->getStart()) << "(" <<
(node->getLeft()->getEnd() - node->getLeft()->getStart()) << ") / " <<
setprecision(4) << (float)(node->getRight()->getEnd() - node->getRight()->getStart())/(node->getEnd() - node->getStart()) << "(" <<
node->getRight()->getEnd() - node->getRight()->getStart() << ")" << endl;
#endif
nodeList[nextList].push_back(node->getLeft());
nodeList[nextList].push_back(node->getRight());
}
else
{
if(updateLeafPrediction(node, cLeftPrediction))
node->setPrediction(cLeftPrediction);
}
}
nodeListPtr = nextList;
curNodeList = &nodeList[nextList];
}
}
void load(string filename)
{
ifstream in(filename.c_str());
readHeader(in);
root = new TNode<SplitData, Prediction>(0, 0);
read(root, in);
bool includeSamples;
in >> includeSamples;
if (includeSamples)
read(this->samples, in);
}
// ==================== OPERATORS =======================================
protected:
// ==================== METHODS =======================================
//virtual SplitData generateSplit(const TNode<SplitData, Prediction> *node) const=0;
virtual SplitResult split(const SplitData &splitData, Sample &sample) const =0;
virtual bool split(const TNode<SplitData, Prediction> *node, SplitData &splitData,
Prediction &leftPrediction, Prediction &rightPrediction) = 0;
virtual void initialize(const TNode<SplitData, Prediction> *node, ErrorData &errorData,
Prediction &prediction) const = 0;
virtual void updateError(ErrorData &newError, const ErrorData &errorData,
const TNode<SplitData, Prediction> *node, Prediction &newLeft,
Prediction &newRight) const = 0;
virtual double getError(const ErrorData &error) const = 0;
// non-pure virtual function which allows to modify predictions after all node split trials are made
virtual bool updateLeafPrediction(const TNode<SplitData, Prediction> *node, Prediction &newPrediction) const
{
return false;
}
const LSamplesVector &getLSamples() const
{
return samples;
}
LSamplesVector &getLSamples()
{
return samples;
}
SplitResultsVector &getSplitResults()
{
return splitResults;
}
const SplitResultsVector &getSplitResults() const
{
return splitResults;
}
TNode<SplitData,Prediction>* getRoot() const
{
return root;
}
virtual void writeHeader(ostream &out) const=0;
virtual void readHeader(istream &in) =0;
virtual void write(const Sample &sample, ostream &out) const =0;
virtual void read(Sample &sample, istream &in) const =0;
virtual void write(const Prediction &prediction, ostream &out) const=0;
virtual void read(Prediction &prediction, istream &in) const=0;
virtual void write(const Label &label, ostream &out) const=0;
virtual void read(Label &label, istream &in) const=0;
virtual void write(const SplitData &splitData, ostream &out) const=0;
virtual void read(SplitData &splitData, istream &in) const=0;
// ==================== DATA MEMBERS =======================================
protected:
// ==================== METHODS =======================================
bool tryImprovingSplit(ErrorData &errorData, TNode<SplitData, Prediction> *node)
{
bool improved = false;
if (split(node, cSplitData, cLeftPrediction, cRightPrediction))
{
double initialError = getError(errorData);
ErrorData newErrorData;
updateError(newErrorData, errorData, node, cLeftPrediction, cRightPrediction); //do not move this afterwards
double deltaError = getError(newErrorData) - initialError;
if (node->isLeaf() || deltaError < 0)
{
int start, middle;
doSplit(node, start, middle);
node->setSplitData(cSplitData);
node->split(start, middle);
node->getLeft()->setPrediction(cLeftPrediction);
node->getRight()->setPrediction(cRightPrediction);
errorData = newErrorData;
improved = true;
}
}
return improved;
}
void doSplit(const TNode<SplitData, Prediction> *node, int &pInvalid, int &pLeft)
{
pLeft = node->getStart();
pInvalid = node->getStart();
int pRight = node->getEnd() - 1;
while (pLeft <= pRight)
{
LSample s;
switch (splitResults[pLeft])
{
case SR_RIGHT:
s = samples[pRight];
samples[pRight] = samples[pLeft];
samples[pLeft] = s;
splitResults[pLeft] = splitResults[pRight];
splitResults[pRight] = SR_RIGHT; //not necessary
--pRight;
break;
case SR_INVALID:
s = samples[pInvalid];
samples[pInvalid] = samples[pLeft];
samples[pLeft] = s;
splitResults[pLeft] = splitResults[pInvalid];
splitResults[pInvalid] = SR_INVALID;
++pInvalid;
++pLeft;
break;
case SR_LEFT:
++pLeft;
break;
}
}
}
virtual void write(const TNode<SplitData, Prediction> *node, ostream &out) const
{
out << (node->isLeaf() ? "L " : "N ");
out << node->getStart() << " " << node->getEnd() << " " << node->getDepth() << " ";
write(node->getSplitData(), out);
out << " ";
write(node->getPrediction(), out);
out << endl;
if (!node->isLeaf())
{
write(node->getLeft(), out);
write(node->getRight(), out);
}
}
virtual void read(TNode<SplitData, Prediction> *node, istream &in) const
{
char type;
in >> type;
if (type!='L' && type!='N')
{
cout<<"ERROR: Unknown node type: "<<type<<endl;
exit(-1);
}
bool isLeaf = type == 'L';
int start, end, depth;
in >> start;
in >> end;
in >> depth;
node->setStart(start);
node->setEnd(end);
node->setDepth(depth);
SplitData splitData;
read(splitData, in);
node->setSplitData(splitData);
Prediction prediction;
read(prediction, in);
node->setPrediction(prediction);
if (!isLeaf)
{
node->split(node->getStart(), node->getStart());
read(node->getLeft(), in);
read(node->getRight(), in);
}
}
void write(const LSamplesVector &lSamples, ostream &out) const
{
out << lSamples.size() << " ";
for (int i = 0; i < lSamples.size(); ++i)
{
write(lSamples[i].sample, out);
out << " ";
write(lSamples[i].label, out);
out << " ";
}
}
void read(LSamplesVector &lSamples, istream &in) const
{
int nSamples;
in >> nSamples;
lSamples.resize(nSamples);
for (int i = 0; i < nSamples; ++i)
{
read(lSamples[i].sample, in);
read(lSamples[i].label, in);
}
}
// ==================== DATA MEMBERS =======================================
TNode<SplitData, Prediction> *root;
LSamplesVector samples;
SplitResultsVector splitResults;
SplitData cSplitData;
Prediction cLeftPrediction, cRightPrediction;
};
// ----- end of class RandomTree -----
}
#endif