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
|
// Copyright (c) 2014 Stefan Walk
//
// This file is part of CGAL (www.cgal.org).
//
// $URL: https://github.com/CGAL/cgal/blob/v6.1.1/Classification/include/CGAL/Classification/ETHZ/internal/random-forest/node.hpp $
// $Id: include/CGAL/Classification/ETHZ/internal/random-forest/node.hpp 08b27d3db14 $
// SPDX-License-Identifier: LicenseRef-RFL
// License notice in Installation/LICENSE.RFL
//
// Author(s) : Stefan Walk
// Modifications from original library:
// * changed inclusion protection tag
// * moved to namespace CGAL::internal::
// * fix computation of node_dist[label] so that results are always <= 1.0
// * change serialization functions to avoid a bug with boost and some
// compilers (that leads to dereferencing a null pointer)
// * add a method to get feature usage
#ifndef CGAL_INTERNAL_LIBLEARNING_RANDOMFORESTS_NODE_H
#define CGAL_INTERNAL_LIBLEARNING_RANDOMFORESTS_NODE_H
#include "../dataview.h"
#include "common-libraries.hpp"
#include <CGAL/IO/binary_file_io.h>
#if defined(CGAL_LINKED_WITH_BOOST_IOSTREAMS) && defined(CGAL_LINKED_WITH_BOOST_SERIALIZATION)
#include <boost/serialization/scoped_ptr.hpp>
#include <boost/serialization/vector.hpp>
#else
#include <boost/scoped_ptr.hpp>
#include <vector>
#endif
#if VERBOSE_NODE_LEARNING
#include <cstdio>
#endif
namespace CGAL { namespace internal {
namespace liblearning {
namespace RandomForest {
template <typename Derived, typename ParamT, typename Splitter>
class Node {
public:
typedef typename Splitter::FeatureType FeatureType;
bool is_leaf;
size_t n_samples;
size_t depth;
typedef ParamT ParamType;
ParamType const* params;
Splitter splitter;
boost::scoped_ptr<Derived> left;
boost::scoped_ptr<Derived> right;
std::vector<float> node_dist;
Node() : is_leaf(true), n_samples(0), depth(-1), params(0) {}
Node(size_t depth, ParamType const* params) :
is_leaf(true), n_samples(0), depth(depth), params(params)
{}
bool pure(DataView2D<int> labels, int* sample_idxes) const {
if (n_samples < 2)
return true; // an empty node is by definition pure
int first_sample_idx = sample_idxes[0];
int seen_class = labels(first_sample_idx, 0);
// check if all classes are equal to the first class
for (size_t i_sample = 1; i_sample < n_samples; ++i_sample) {
int sample_idx = sample_idxes[i_sample];
if (labels(sample_idx, 0) != seen_class)
return false;
}
return true;
}
float const* votes() const {
return (float const*)&node_dist[0];
}
int partition_samples(DataView2D<FeatureType> samples, int* sample_idxes) {
// sort samples in bag so that left-samples precede right-samples
// works like std::partition
int low = 0;
int high = n_samples;
while (true) {
while (true) {
if (low == high) {
return low;
} else if (!splitter.classify_sample(samples.row_pointer(sample_idxes[low]))) {
++low;
} else {
break;
}
}
--high;
while (true) {
if (low == high) {
return low;
} else if (splitter.classify_sample(samples.row_pointer(sample_idxes[high]))) {
--high;
} else {
break;
}
}
std::swap(sample_idxes[low], sample_idxes[high]);
++low;
}
}
Derived const* split (FeatureType const* sample) const {
if (splitter.classify_sample(sample)) {
return right.get();
} else {
return left.get();
}
}
typedef std::list<Derived const*> NodeList;
NodeList get_all_childs() {
NodeList ret;
ret.push_back(this);
if (!is_leaf) {
NodeList left_childs = left->get_all_childs();
ret.splice(ret.end(), left_childs);
NodeList right_childs = right->get_all_childs();
ret.splice(ret.end(), right_childs);
}
return ret;
}
template<typename SplitGenerator>
void determine_best_split(DataView2D<FeatureType> samples,
DataView2D<int> labels,
int* sample_idxes,
SplitGenerator split_generator,
RandomGen& gen
)
{
typename Splitter::FeatureClassData data_points;
init_feature_class_data(data_points, params->n_classes, n_samples);
float best_loss = std::numeric_limits<float>::infinity();
std::vector<uint64_t> classes_l;
std::vector<uint64_t> classes_r;
// pass information about data to split generator
split_generator.init(samples,
labels,
sample_idxes,
n_samples,
params->n_classes,
gen);
size_t n_proposals = split_generator.num_proposals();
std::pair<FeatureType, float> results; // (threshold, loss)
for (size_t i_proposal = 0; i_proposal < n_proposals; ++i_proposal) {
// generate proposal
Splitter split = split_generator.gen_proposal(gen);
// map samples to numbers using proposal
split.map_points(samples, labels, sample_idxes, n_samples, data_points);
// check best loss using this proposal
results = static_cast<Derived*>(this)->determine_best_threshold(data_points, classes_l, classes_r, gen);
if (results.second < best_loss) {
// Proposal resulted into new optimum
best_loss = results.second;
split.set_threshold(results.first);
splitter = split;
}
}
}
template<typename SplitGenerator>
void train(DataView2D<FeatureType> samples,
DataView2D<int> labels,
int* sample_idxes,
size_t n_samples_,
SplitGenerator const& split_generator,
RandomGen& gen
)
{
n_samples = n_samples_;
node_dist.resize(params->n_classes, 0.0f);
for (size_t i_sample = 0; i_sample < n_samples; ++i_sample) {
int label = labels(sample_idxes[i_sample], 0);
node_dist[label] += 1.0f;
}
if (n_samples != 0)
for (std::size_t i = 0; i < node_dist.size(); ++ i)
node_dist[i] /= n_samples;
bool do_split = // Only split if ...
(n_samples >= params->min_samples_per_node) && // enough samples are available
!pure(labels, sample_idxes) && // this node is not already pure
(depth < params->max_depth); // we did not reach max depth
if (!do_split) {
splitter.threshold = 0.0;
return;
}
is_leaf = false;
#if VERBOSE_NODE_LEARNING
std::printf("Determining the best split at depth %zu/%zu\n", depth, params->max_depth);
#endif
determine_best_split(samples, labels, sample_idxes, split_generator, gen);
left.reset(new Derived(depth + 1, params));
right.reset(new Derived(depth + 1, params));
// sort samples in bag so that left-samples precede right-samples
int low = partition_samples(samples, sample_idxes);
int n_samples_left = low;
int n_samples_right = n_samples - low;
int offset_left = 0;
int offset_right = low;
#ifdef TREE_GRAPHVIZ_STREAM
if (depth <= TREE_GRAPHVIZ_MAX_DEPTH) {
TREE_GRAPHVIZ_STREAM << "p" << std::hex << (unsigned long)this
<< " -> "
<< "p" << std::hex << (unsigned long)left.get()
<< std::dec << " [label=\"" << n_samples_left << "\"];" << std::endl;
TREE_GRAPHVIZ_STREAM << "p" << std::hex << (unsigned long)this
<< " -> "
<< "p" << std::hex << (unsigned long)right.get()
<< std::dec << " [label=\"" << n_samples_right << "\"];" << std::endl;
}
#endif
// train left and right side of split
left->train (samples, labels, sample_idxes + offset_left, n_samples_left, split_generator, gen);
right->train(samples, labels, sample_idxes + offset_right, n_samples_right, split_generator, gen);
}
#if defined(CGAL_LINKED_WITH_BOOST_IOSTREAMS) && defined(CGAL_LINKED_WITH_BOOST_SERIALIZATION)
template <typename Archive>
void serialize(Archive& ar, unsigned /*version*/)
{
ar & BOOST_SERIALIZATION_NVP(is_leaf);
ar & BOOST_SERIALIZATION_NVP(n_samples);
ar & BOOST_SERIALIZATION_NVP(depth);
ar & BOOST_SERIALIZATION_NVP(params);
ar & BOOST_SERIALIZATION_NVP(splitter);
ar & BOOST_SERIALIZATION_NVP(node_dist);
if (!is_leaf)
{
ar & BOOST_SERIALIZATION_NVP(left);
ar & BOOST_SERIALIZATION_NVP(right);
}
}
#endif
void write (std::ostream& os)
{
I_Binary_write_bool (os, is_leaf);
I_Binary_write_size_t_into_uinteger32 (os, n_samples);
I_Binary_write_size_t_into_uinteger32 (os, depth);
splitter.write(os);
for (const float& f : node_dist)
I_Binary_write_float32 (os, f);
if (!is_leaf)
{
left->write(os);
right->write(os);
}
}
void read (std::istream& is)
{
I_Binary_read_bool (is, is_leaf);
I_Binary_read_size_t_from_uinteger32 (is, n_samples);
I_Binary_read_size_t_from_uinteger32 (is, depth);
splitter.read(is);
node_dist.resize(params->n_classes, 0.0f);
for (std::size_t i = 0; i < node_dist.size(); ++ i)
I_Binary_read_float32 (is, node_dist[i]);
if (!is_leaf)
{
left.reset(new Derived(depth + 1, params));
right.reset(new Derived(depth + 1, params));
left->read(is);
right->read(is);
}
}
void get_feature_usage (std::vector<std::size_t>& count) const
{
if (!is_leaf && splitter.feature != -1)
{
count[std::size_t(splitter.feature)] ++;
left->get_feature_usage(count);
right->get_feature_usage(count);
}
}
};
}
}
}} // namespace CGAL::internal::
#endif
|