-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathForest.cpp
376 lines (348 loc) · 11.7 KB
/
Forest.cpp
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
#include "Forest.h"
#include "InOut.h"
#include "Sample.h"
#include "utils.h"
RandomForest::RandomForest(int numTrees, int maxDepth, int minSamplesPerLeaf) :
_numTrees(numTrees),
_maxDepth(maxDepth),
_minSamplesPerLeaf(minSamplesPerLeaf),
_trainSample(nullptr),
_forest(_numTrees, nullptr)
{
std::cout << "The number of trees in the forest is: " << _numTrees << std::endl;
std::cout << "The max depth of a single tree is: " << _maxDepth << std::endl;
std::cout << "The minimal number of samples at a leaf node is: " << _minSamplesPerLeaf << std::endl;
}
RandomForest::RandomForest(const char* modelPath)
{
readModel(modelPath);
}
RandomForest::~RandomForest()
{
if (_trainSample != nullptr)
{
delete _trainSample;
_trainSample = nullptr;
}
}
void RandomForest::train(Eigen::MatrixXf *trainset, Eigen::VectorXi *labels,
Eigen::MatrixXi *indices, Eigen::MatrixXf *dists,
int numClasses, int numFeatsPerNode,
Eigen::MatrixXf *cloud)
{
if (_numTrees < 1)
{
std::cout << "Total number of trees must be bigger than 0." << std::endl;
std::cerr << "Training not started." << std::endl;
return;
}
if (_maxDepth < 1)
{
std::cout << "The max depth must be bigger than 0." << std::endl;
std::cerr << "Training not started." << std::endl;
return;
}
if (_minSamplesPerLeaf < 2)
{
std::cout << "The minimal number of samples at a leaf node must be greater than 1." << std::endl;
std::cerr << "Training not started." << std::endl;
return;
}
int _numSamples = trainset->rows();
_numClasses = numClasses;
_numFeatsPerNode = numFeatsPerNode;
// bagging: only about two thirds of the entire dataset is used
// for each tree. This sampling process is with replacement.
int _numSelectedSamples = _numSamples * 0.7;
// initializing the trees
for (int i = 0; i < _numTrees; ++i)
{
_forest[i] = new Tree(_maxDepth, _numFeatsPerNode, _minSamplesPerLeaf);
}
// this object holds the whole training dataset
_trainSample = new Sample(trainset, labels, indices, dists, _numClasses, _numFeatsPerNode, cloud);
// selected samples
Eigen::VectorXi selectedSamplesId(_numSelectedSamples);
// tree training starts
for (int i = 0; i < _numTrees; ++i)
{
std::cout << "Training tree No. " << i << std::endl;
// randomly sample 2/3 of the points with replacement from training set
Sample *sample = new Sample(_trainSample);
sample->randomSampleDataset(selectedSamplesId, _numSelectedSamples);
_forest[i]->train(sample);
std::vector<Node*> nodes = _forest[i]->getTreeNodes();
_forest[i]->computeStats(nodes);
delete sample;
}
}
void RandomForest::predict(const char * testCloudPath, const char * testDataPath, Eigen::VectorXi & predictedLabels)
{
// looking for the nearest neighbors of each point in the test dataset
Eigen::MatrixXf testCloud;
Eigen::MatrixXf testset;
Eigen::MatrixXi testIndices;
Eigen::MatrixXf testDists;
InOut testObj;
testObj.readPoints(testCloudPath, testCloud);
testObj.readPoints(testDataPath, testset);
testObj.searchNN(testCloud, testset, testIndices, testDists);
int numTests = testset.rows();
predictedLabels.resize(numTests);
Sample* testSamples = new Sample(&testset, &predictedLabels, &testIndices, &testDists, _numClasses, _numFeatsPerNode, &testCloud);
std::cout << "Predicting begins ... " << std::endl;
for (int i = 0; i < numTests; ++i)
{
//Eigen::VectorXi datapoint = testSamples->_dataset->row(i);
Eigen::MatrixXf testDataNeigh = testSamples->buildNeighborhood(i);
predictedLabels[i] = predict(testDataNeigh);
}
}
void RandomForest::predict(const char* testDataPath, Eigen::VectorXi &predictedLabels)
{
// looking for the nearest neighbors of each point in the test dataset
Eigen::MatrixXf testset;
Eigen::MatrixXi testIndices;
Eigen::MatrixXf testDists;
InOut testObj;
testObj.readPoints(testDataPath, testset);
testObj.searchNN(testset, testIndices, testDists);
int numTests = testset.rows();
predictedLabels.resize(numTests);
Sample* testSamples = new Sample(&testset, &predictedLabels, &testIndices, &testDists, _numClasses, _numFeatsPerNode, &testset);
std::cout << "Predicting begins ... " << std::endl;
for (int i = 0; i < numTests; ++i)
{
//Eigen::VectorXi datapoint = testSamples->_dataset->row(i);
Eigen::MatrixXf testDataNeigh = testSamples->buildNeighborhood(i);
predictedLabels[i] = predict(testDataNeigh);
}
}
int RandomForest::predict(Eigen::MatrixXf &testNeigh)
{
Eigen::VectorXf predictedProbs(_numClasses);
for (int i = 0; i < _numClasses; ++i)
predictedProbs[i] = 0;
std::vector<float> predictedProbsVec;
// accumulate the class distribution of every tree
for (int i = 0; i < _numTrees; ++i)
{
predictedProbsVec = _forest[i]->predict(testNeigh);
predictedProbs += toEigenVec(predictedProbsVec);
}
// average the class distribution
predictedProbs /= _numTrees;
// find the max value in the distribution and return its index
// as the class label
float prob = predictedProbs[0];
int label = 0;
for (int i = 1; i < _numClasses; ++i)
{
if (predictedProbs[i] > prob)
{
prob = predictedProbs[i];
label = i;
}
}
return label;
}
void RandomForest::saveModel(const char* path, const char* statFilePath)
{
printf("Saving model ... ");
FILE *saveFile = fopen(path, "wb");
FILE *statFile = nullptr;
if (statFilePath != nullptr)
statFile = fopen(statFilePath, "w");
fwrite(&_numTrees, sizeof(int), 1, saveFile);
fwrite(&_maxDepth, sizeof(int), 1, saveFile);
fwrite(&_numClasses, sizeof(int), 1, saveFile);
fprintf(statFile, "There are %d trees in the forest.\n", _numTrees);
fprintf(statFile, "Each tree has a max height of %d.\n", _maxDepth);
int numNodes = static_cast<int>(pow(2.0, _maxDepth) - 1);
int isLeaf = 0;
for (int i = 0; i < _numTrees; ++i)
{
// write some statistics to file
if (statFilePath != nullptr)
{
fprintf(statFile, "----------------------------------Stats for Tree %d-----------------------------:\n", i);
int totalNum = _forest[i]->getTotalNumSamples();
fprintf(statFile, "%d samples are trained in this tree.\n", totalNum);
float balance = _forest[i]->getBalance();
fprintf(statFile, "Tree balance is: %.3f\n", balance);
int numSamplesInLargestLeaf = _forest[i]->getNumSamplesInLargestLeaf();
fprintf(statFile, "The number of samples in the largest leaf: %d\n", numSamplesInLargestLeaf);
float gradeSorting = _forest[i]->getSortingGrade();
fprintf(statFile, "Grade of sorting is: %.3f\n", gradeSorting);
float largestLeafGini = _forest[i]->getLargestLeafGini();
fprintf(statFile, "Gini of the largest leaf node: %.3f\n", largestLeafGini);
std::vector<float> largestLeafDistr = _forest[i]->getLargestLeafDistr();
fprintf(statFile, "The posterior of the largest leaf node: \n");
for (int k = 0; k < largestLeafDistr.size(); ++k)
{
fprintf(statFile, "%.3f ", largestLeafDistr[k]);
}
std::vector<float> bestFeatTypeDistr = _forest[i]->getBestFeatTypeDistr();
fprintf(statFile, "\nThe posterior of the projection type of the selected best feature at each node:\n");
for (int k = 0; k < bestFeatTypeDistr.size(); ++k)
{
fprintf(statFile, "%.3f ", bestFeatTypeDistr[k]);
}
fprintf(statFile, "\n");
}
std::vector<Node*> arr = _forest[i]->getTreeNodes();
isLeaf = 0;
for (int j = 0; j < numNodes; ++j)
{
if (arr[j] != nullptr)
{
if (arr[j]->isLeaf())
{
isLeaf = 1;
fwrite(&isLeaf, sizeof(int), 1, saveFile);
int clas = arr[j]->getClass();
float prob = arr[j]->getProb();
fwrite(&clas, sizeof(int), 1, saveFile);
fwrite(&prob, sizeof(float), 1, saveFile);
// save the posterior distr of each leaf node
// it is again a vector
for (int k = 0; k < _numClasses; ++k)
{
float tmpProb = arr[j]->_probs[k];
fwrite(&tmpProb, sizeof(float), 1, saveFile);
}
}
else
{
isLeaf = 0;
fwrite(&isLeaf, sizeof(int), 1, saveFile);
// Features is a user defined datatype
// saving it is a little complicated
Features bestFeat = arr[j]->getBestFeature();
int numVoxel = bestFeat._numVoxels;
fwrite(&numVoxel, sizeof(int), 1, saveFile);
int featType = bestFeat._featType;
fwrite(&featType, sizeof(int), 1, saveFile);
float thresh = bestFeat._thresh;
fwrite(&thresh, sizeof(float), 1, saveFile);
// save the vector size -> then save its content
int pointIdSize = bestFeat._pointId.size();
fwrite(&pointIdSize, sizeof(int), 1, saveFile);
for (int k = 0; k < pointIdSize; ++k)
{
int pointId = bestFeat._pointId[k];
fwrite(&pointId, sizeof(int), 1, saveFile);
}
/*int voxelSizeSize = bestFeat._voxelSize.size();
fwrite(&voxelSizeSize, sizeof(int), 1, saveFile);
for (int k = 0; k < voxelSizeSize; ++k)
{
int voxelSize = bestFeat._voxelSize[k];
fwrite(&voxelSize, sizeof(int), 1, saveFile);
}*/
}
}
}
}
fclose(statFile);
fclose(saveFile);
printf("Model saved!\n");
}
void RandomForest::readModel(const char* path)
{
printf("Reading model ... \n");
_minSamplesPerLeaf = 0;
_numFeatsPerNode = 0;
FILE* modelFile = fopen(path, "rb");
fread(&_numTrees, sizeof(int), 1, modelFile);
fread(&_maxDepth, sizeof(int), 1, modelFile);
fread(&_numClasses, sizeof(int), 1, modelFile);
int numNodes = static_cast<int>(pow(2.0, _maxDepth) - 1);
_trainSample = nullptr;
printf("The number of trees in the forest is: %d\n", _numTrees);
printf("The max depth of a single tree is: %d\n", _maxDepth);
printf("The number of classes is: %d\n", _numClasses);
_forest.resize(_numTrees);
for (int i = 0; i < _numTrees; ++i)
{
_forest[i] = new Tree(_maxDepth, _numFeatsPerNode, _minSamplesPerLeaf);
}
int* nodeTable = new int[numNodes];
int isLeaf = -1;
Features bestFeat;
int clas = 0;
float prob = 0;
std::vector<float> probs(_numClasses, 0);
for (int i = 0; i < _numTrees; ++i)
{
//std::vector<Node*> treeNodes = _forest[i]->getTreeNodes();
memset(nodeTable, 0, sizeof(int)*numNodes);
nodeTable[0] = 1;
for (int j = 0; j < numNodes; ++j)
{
if (nodeTable[j] == 0)
continue;
fread(&isLeaf, sizeof(int), 1, modelFile);
if(isLeaf == 0)
{
nodeTable[j * 2 + 1] = 1;
nodeTable[j * 2 + 2] = 1;
// read Features members
// first is the numVoxels
int numVoxels = 0;
fread(&numVoxels, sizeof(int), 1, modelFile);
bestFeat._numVoxels = numVoxels;
// then the featType
int featType = 0;
fread(&featType, sizeof(int), 1, modelFile);
bestFeat._featType = featType;
// after that is the thresh
float thresh = 0;
fread(&thresh, sizeof(float), 1, modelFile);
bestFeat._thresh = thresh;
// read the two vectors, a little complicated
// vector I:
int pointIdSize = 0;
fread(&pointIdSize, sizeof(int), 1, modelFile);
// initialize a vector
std::vector<int> pointId(pointIdSize, 0);
for (int k = 0; k < pointIdSize; ++k)
{
int tmpPointId = 0;
fread(&tmpPointId, sizeof(int), 1, modelFile);
pointId[k] = tmpPointId;
}
bestFeat._pointId = pointId;
// vector II:
//int voxelSize = 0;
//fread(&voxelSize, sizeof(int), 1, modelFile);
//// initialize a vector
//std::vector<int> voxels(voxelSize, 0);
//for (int k = 0; k < voxelSize; ++k)
//{
// int tmpVoxel = 0;
// fread(&tmpVoxel, sizeof(int), 1, modelFile);
// voxels[k] = tmpVoxel;
//}
//bestFeat._voxelSize = voxels;
_forest[i]->createNode(j, bestFeat);
}
else
{
fread(&clas, sizeof(int), 1, modelFile);
fread(&prob, sizeof(float), 1, modelFile);
//std::vector<float> probs(_numClasses, 0);
for (int k = 0; k < _numClasses; ++k)
{
float tmpProb = 0;
fread(&tmpProb, sizeof(float), 1, modelFile);
probs[k] = tmpProb;
}
_forest[i]->createLeaf(j, clas, prob, probs);
}
}
}
fclose(modelFile);
delete[] nodeTable;
printf("Model read!\n");
}