Point Cloud Library (PCL)  1.10.0
decision_tree_evaluator.hpp
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2010-2011, Willow Garage, Inc.
6  *
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * * Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * * Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  * * Neither the name of Willow Garage, Inc. nor the names of its
20  * contributors may be used to endorse or promote products derived
21  * from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  *
36  */
37 
38 #pragma once
39 
40 #include <pcl/common/common.h>
41 
42 #include <pcl/ml/dt/decision_tree.h>
43 #include <pcl/ml/feature_handler.h>
44 #include <pcl/ml/stats_estimator.h>
45 
46 #include <vector>
47 
48 template <class FeatureType,
49  class DataSet,
50  class LabelType,
51  class ExampleIndex,
52  class NodeType>
55 {}
56 
57 template <class FeatureType,
58  class DataSet,
59  class LabelType,
60  class ExampleIndex,
61  class NodeType>
64 {}
65 
66 template <class FeatureType,
67  class DataSet,
68  class LabelType,
69  class ExampleIndex,
70  class NodeType>
71 void
76  stats_estimator,
77  DataSet& data_set,
78  std::vector<ExampleIndex>& examples,
79  std::vector<LabelType>& label_data)
80 {
81  const std::size_t num_of_examples = examples.size();
82  label_data.resize(num_of_examples);
83  for (int example_index = 0; example_index < num_of_examples; ++example_index) {
84  NodeType* node = &(tree.getRoot());
85 
86  while (node->sub_nodes.size() != 0) {
87  float feature_result = 0.0f;
88  unsigned char flag = 0;
89  unsigned char branch_index = 0;
90 
91  feature_handler.evaluateFeature(
92  node->feature, data_set, examples[example_index], feature_result, flag);
93  stats_estimator.computeBranchIndex(
94  feature_result, flag, node->threshold, branch_index);
95 
96  node = &(node->sub_nodes[branch_index]);
97  }
98 
99  label_data[example_index] = stats_estimator.getLabelOfNode(*node);
100  }
101 }
102 
103 template <class FeatureType,
104  class DataSet,
105  class LabelType,
106  class ExampleIndex,
107  class NodeType>
108 void
114  stats_estimator,
115  DataSet& data_set,
116  std::vector<ExampleIndex>& examples,
117  std::vector<LabelType>& label_data)
118 {
119  const std::size_t num_of_examples = examples.size();
120  for (int example_index = 0; example_index < num_of_examples; ++example_index) {
121  NodeType* node = &(tree.getRoot());
122 
123  while (node->sub_nodes.size() != 0) {
124  float feature_result = 0.0f;
125  unsigned char flag = 0;
126  unsigned char branch_index = 0;
127 
128  feature_handler.evaluateFeature(
129  node->feature, data_set, examples[example_index], feature_result, flag);
130  stats_estimator.computeBranchIndex(
131  feature_result, flag, node->threshold, branch_index);
132 
133  node = &(node->sub_nodes[branch_index]);
134  }
135 
136  label_data[example_index] += stats_estimator.getLabelOfNode(*node);
137  }
138 }
139 
140 template <class FeatureType,
141  class DataSet,
142  class LabelType,
143  class ExampleIndex,
144  class NodeType>
145 void
150  stats_estimator,
151  DataSet& data_set,
152  ExampleIndex example,
153  NodeType& leave)
154 {
155 
156  NodeType* node = &(tree.getRoot());
157 
158  while (!node->sub_nodes.empty()) {
159  float feature_result = 0.0f;
160  unsigned char flag = 0;
161  unsigned char branch_index = 0;
162 
163  feature_handler.evaluateFeature(
164  node->feature, data_set, example, feature_result, flag);
165  stats_estimator.computeBranchIndex(
166  feature_result, flag, node->threshold, branch_index);
167 
168  node = &(node->sub_nodes[branch_index]);
169  }
170 
171  leave = *node;
172 }
173 
174 template <class FeatureType,
175  class DataSet,
176  class LabelType,
177  class ExampleIndex,
178  class NodeType>
179 void
184  stats_estimator,
185  DataSet& data_set,
186  std::vector<ExampleIndex>& examples,
187  std::vector<NodeType*>& nodes)
188 {
189  const std::size_t num_of_examples = examples.size();
190  for (int example_index = 0; example_index < num_of_examples; ++example_index) {
191  NodeType* node = &(tree.getRoot());
192 
193  while (node->sub_nodes.size() != 0) {
194  float feature_result = 0.0f;
195  unsigned char flag = 0;
196  unsigned char branch_index = 0;
197 
198  feature_handler.evaluateFeature(
199  node->feature, data_set, examples[example_index], feature_result, flag);
200  stats_estimator.computeBranchIndex(
201  feature_result, node->threshold, flag, branch_index);
202 
203  node = &(node->subNodes[branch_index]);
204  }
205 
206  nodes.push_back(node);
207  }
208 }
pcl::DecisionTreeEvaluator::evaluateAndAdd
void evaluateAndAdd(pcl::DecisionTree< NodeType > &tree, pcl::FeatureHandler< FeatureType, DataSet, ExampleIndex > &feature_handler, pcl::StatsEstimator< LabelType, NodeType, DataSet, ExampleIndex > &stats_estimator, DataSet &data_set, std::vector< ExampleIndex > &examples, std::vector< LabelType > &label_data)
Evaluates the specified examples using the supplied tree and adds the results to the supplied results...
Definition: decision_tree_evaluator.hpp:110
pcl::DecisionTree::getRoot
NodeType & getRoot()
Returns the root node of the tree.
Definition: decision_tree.h:69
common.h
pcl::DecisionTreeEvaluator::DecisionTreeEvaluator
DecisionTreeEvaluator()
Constructor.
Definition: decision_tree_evaluator.hpp:54
pcl::FeatureHandler::evaluateFeature
virtual void evaluateFeature(const FeatureType &feature, DataSet &data_set, std::vector< ExampleIndex > &examples, std::vector< float > &results, std::vector< unsigned char > &flags) const =0
Evaluates a feature on the specified data.
pcl::DecisionTree
Class representing a decision tree.
Definition: decision_tree.h:49
pcl::FeatureHandler
Utility class interface which is used for creating and evaluating features.
Definition: feature_handler.h:49
pcl::DecisionTreeEvaluator::evaluate
void evaluate(pcl::DecisionTree< NodeType > &tree, pcl::FeatureHandler< FeatureType, DataSet, ExampleIndex > &feature_handler, pcl::StatsEstimator< LabelType, NodeType, DataSet, ExampleIndex > &stats_estimator, DataSet &data_set, std::vector< ExampleIndex > &examples, std::vector< LabelType > &label_data)
Evaluates the specified examples using the supplied tree.
Definition: decision_tree_evaluator.hpp:73
pcl::StatsEstimator::computeBranchIndex
virtual void computeBranchIndex(const float result, const unsigned char flag, const float threshold, unsigned char &branch_index) const =0
Computes the branch indices obtained by the specified threshold on the supplied feature evaluation re...
pcl::DecisionTreeEvaluator::~DecisionTreeEvaluator
virtual ~DecisionTreeEvaluator()
Destructor.
Definition: decision_tree_evaluator.hpp:63
pcl::StatsEstimator::getLabelOfNode
virtual LabelDataType getLabelOfNode(NodeType &node) const =0
Returns the label of the specified node.
pcl::StatsEstimator< LabelType, NodeType, DataSet, ExampleIndex >
pcl::DecisionTreeEvaluator::getNodes
void getNodes(pcl::DecisionTree< NodeType > &tree, pcl::FeatureHandler< FeatureType, DataSet, ExampleIndex > &feature_handler, pcl::StatsEstimator< LabelType, NodeType, DataSet, ExampleIndex > &stats_estimator, DataSet &data_set, std::vector< ExampleIndex > &examples, std::vector< NodeType * > &nodes)
Evaluates the specified examples using the supplied tree.
Definition: decision_tree_evaluator.hpp:181