Point Cloud Library (PCL)  1.12.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 #include <pcl/ml/dt/decision_tree.h>
42 #include <pcl/ml/feature_handler.h>
43 #include <pcl/ml/stats_estimator.h>
44 
45 #include <vector>
46 
47 namespace pcl {
48 
49 template <class FeatureType,
50  class DataSet,
51  class LabelType,
52  class ExampleIndex,
53  class NodeType>
56 {}
57 
58 template <class FeatureType,
59  class DataSet,
60  class LabelType,
61  class ExampleIndex,
62  class NodeType>
65 {}
66 
67 template <class FeatureType,
68  class DataSet,
69  class LabelType,
70  class ExampleIndex,
71  class NodeType>
72 void
77  stats_estimator,
78  DataSet& data_set,
79  std::vector<ExampleIndex>& examples,
80  std::vector<LabelType>& label_data)
81 {
82  const std::size_t num_of_examples = examples.size();
83  label_data.resize(num_of_examples);
84  for (int example_index = 0; example_index < num_of_examples; ++example_index) {
85  NodeType* node = &(tree.getRoot());
86 
87  while (node->sub_nodes.size() != 0) {
88  float feature_result = 0.0f;
89  unsigned char flag = 0;
90  unsigned char branch_index = 0;
91 
92  feature_handler.evaluateFeature(
93  node->feature, data_set, examples[example_index], feature_result, flag);
94  stats_estimator.computeBranchIndex(
95  feature_result, flag, node->threshold, branch_index);
96 
97  node = &(node->sub_nodes[branch_index]);
98  }
99 
100  label_data[example_index] = stats_estimator.getLabelOfNode(*node);
101  }
102 }
103 
104 template <class FeatureType,
105  class DataSet,
106  class LabelType,
107  class ExampleIndex,
108  class NodeType>
109 void
115  stats_estimator,
116  DataSet& data_set,
117  std::vector<ExampleIndex>& examples,
118  std::vector<LabelType>& label_data)
119 {
120  const std::size_t num_of_examples = examples.size();
121  for (int example_index = 0; example_index < num_of_examples; ++example_index) {
122  NodeType* node = &(tree.getRoot());
123 
124  while (node->sub_nodes.size() != 0) {
125  float feature_result = 0.0f;
126  unsigned char flag = 0;
127  unsigned char branch_index = 0;
128 
129  feature_handler.evaluateFeature(
130  node->feature, data_set, examples[example_index], feature_result, flag);
131  stats_estimator.computeBranchIndex(
132  feature_result, flag, node->threshold, branch_index);
133 
134  node = &(node->sub_nodes[branch_index]);
135  }
136 
137  label_data[example_index] += stats_estimator.getLabelOfNode(*node);
138  }
139 }
140 
141 template <class FeatureType,
142  class DataSet,
143  class LabelType,
144  class ExampleIndex,
145  class NodeType>
146 void
151  stats_estimator,
152  DataSet& data_set,
153  ExampleIndex example,
154  NodeType& leave)
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 }
209 
210 } // namespace pcl
virtual ~DecisionTreeEvaluator()
Destructor.
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.
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...
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.
Class representing a decision tree.
Definition: decision_tree.h:49
NodeType & getRoot()
Returns the root node of the tree.
Definition: decision_tree.h:69
Utility class interface which is used for creating and evaluating features.
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.
virtual LabelDataType getLabelOfNode(NodeType &node) const =0
Returns the label of the specified node.
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...
Define standard C methods and C++ classes that are common to all methods.