/
tree.cpp
222 lines (183 loc) · 7.42 KB
/
tree.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
#include "tree.h"
#include "numeric.h"
namespace base {
float32 ComputeInformationGain(const Histogram& parent, const Histogram& left,
const Histogram& right) {
// Our computations use integer variables but must occur at float precision.
float32 parent_total = parent.GetSampleTotal();
return parent.GetEntropy() -
(((left.GetSampleTotal() / parent_total) * (left.GetEntropy())) +
((right.GetSampleTotal() / parent_total) * (right.GetEntropy())));
}
bool DecisionNode::Train(const DecisionTreeParams& params, uint32 depth,
vector<TrainSet>* samples,
const Histogram& sample_histogram, string* error) {
if (!samples) {
if (error) {
*error = "Invalid parameter(s) specified to DecisionNode::Train.";
}
return false;
}
// Cache our incoming histogram, which defines the statitics at the
// current node. This is useful during training, and potentially useful
// for classification if the current node ends up being a leaf.
histogram_ = sample_histogram;
// If we've reached our exit criteria then we early exit, leaving this
// node as a leaf in the tree.
if (depth >= params.max_tree_depth || !samples->size() ||
samples->size() < params.min_sample_count) {
is_leaf_ = true;
return true;
}
float32 node_entropy = histogram_.GetEntropy();
// If our incoming entropy is zero then our data set is of uniform
// type, and we can declare this node a leaf.
if (0.0f == node_entropy || -0.0f == node_entropy) {
is_leaf_ = true;
return true;
}
// Perform a maximum of params.node_trial_count trials to find
// a best candidate split function for this node. We're guaranteed
// to finish with a candidate, even if it's only a local best.
float32 best_info_gain = -1.0f;
Histogram best_left_hist, best_right_hist;
vector<TrainSet> best_left_samples, best_right_samples;
SplitFunction best_split_function, trial_split_function;
for (uint32 i = 0; i < params.node_trial_count; i++) {
Histogram trial_left_hist, trial_right_hist;
vector<TrainSet> trial_left_samples, trial_right_samples;
trial_left_hist.Initialize(params.class_count);
trial_right_hist.Initialize(params.class_count);
trial_left_samples.reserve(samples->size());
trial_right_samples.reserve(samples->size());
trial_split_function.Initialize(params.visual_search_radius);
// Iterate over all samples, performing split. True goes right.
for (uint32 j = 0; j < samples->size(); j++) {
SplitCoord current_coord = samples->at(j).coord;
uint8 sample_label = samples->at(j).data_source->label.GetPixel(
current_coord.x, current_coord.y);
if (trial_split_function.Split(current_coord,
&samples->at(j).data_source->image)) {
trial_right_samples.push_back(samples->at(j));
trial_right_hist.IncrementValue(sample_label);
} else {
trial_left_samples.push_back(samples->at(j));
trial_left_hist.IncrementValue(sample_label);
}
}
float32 current_info_gain =
ComputeInformationGain(histogram_, trial_left_hist, trial_right_hist);
if (current_info_gain >= best_info_gain) {
best_info_gain = current_info_gain;
best_left_hist = trial_left_hist;
best_right_hist = trial_right_hist;
best_left_samples = std::move(trial_left_samples);
best_right_samples = std::move(trial_right_samples);
best_split_function = trial_split_function;
// If our current info gain equals entropy (i.e. both buckets have zero
// entropy), then we can immediately select this as our best option.
if (current_info_gain == node_entropy) {
break;
}
}
}
// Bind the best split function that we found during our trials.
function_ = best_split_function;
is_leaf_ = false;
// We have our best so we allocate children and attempt to train them.
left_child_.reset(new DecisionNode);
right_child_.reset(new DecisionNode);
if (!left_child_ || !right_child_) {
if (error) {
*error = "Failed to allocate child nodes.";
}
return false;
}
if (!left_child_->Train(params, depth + 1, &best_left_samples, best_left_hist,
error) ||
!right_child_->Train(params, depth + 1, &best_right_samples,
best_right_hist, error)) {
return false;
}
return true;
}
bool DecisionNode::Classify(const SplitCoord& coord, Image* data_source,
Histogram* output, string* error) {
if ((!!left_child_) ^ (!!right_child_)) {
if (error) {
*error = "Invalid tree structure.";
}
return false;
}
if (!output || !data_source) {
if (error) {
*error = "Invalid parameter specified to DecisionNode::Classify.";
}
return false;
}
if (is_leaf_) {
*output = histogram_;
return true;
}
if (function_.Split(coord, data_source)) {
return right_child_->Classify(coord, data_source, output);
}
return left_child_->Classify(coord, data_source, output);
}
bool DecisionTree::Train(const DecisionTreeParams& params,
vector<ImageSet>* training_data,
uint32 training_start_index, uint32 training_count,
string* error) {
if (training_data->empty() || training_count > training_data->size()) {
if (error) {
*error = "Invalid parameter specified to DecisionTree::Train.";
}
return false;
}
Histogram initial_histogram(params.class_count);
vector<TrainSet> tree_training_set;
// Cache a copy of our tree params for later use during classification.
params_ = params;
// This is one of the most expensive operations in our system, so we
// estimate the required size and reserve memory for it.
uint64 required_size = training_count * training_data->at(0).image.width *
training_data->at(0).image.height;
tree_training_set.reserve(required_size);
for (uint32 i = 0; i < training_count; i++) {
uint32 index = (training_start_index + i) % training_data->size();
uint32 width = training_data->at(index).image.width;
uint32 height = training_data->at(index).image.height;
for (uint32 y = 0; y < height; y++)
for (uint32 x = 0; x < width; x++) {
uint8 label_value = training_data->at(index).label.GetPixel(x, y);
tree_training_set.emplace_back(&training_data->at(index), x, y);
initial_histogram.IncrementValue(label_value);
}
}
root_node_.reset(new DecisionNode);
if (!root_node_) {
if (error) {
*error = "Failed allocation of decision tree root node.";
}
return false;
}
return root_node_->Train(params, 0, &tree_training_set, initial_histogram);
}
bool DecisionTree::ClassifyPixel(uint32 x, uint32 y, Image* input,
Histogram* output, string* error) {
if (!root_node_) {
if (error) {
*error = "Invalid root node detected.";
}
return false;
}
if (!input || !output) {
if (error) {
*error = "Invalid parameter specified to DecisionTree::ClassifyPixel.";
}
return false;
}
SplitCoord coord = {x, y};
return root_node_->Classify(coord, input, output, error);
}
} // namespace base