Point Cloud Library (PCL)  1.9.1-dev
fern_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/feature_handler.h>
43 #include <pcl/ml/ferns/fern.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  DataSet& data_set,
77  std::vector<ExampleIndex>& examples,
78  std::vector<LabelType>& label_data)
79 {
80  const std::size_t num_of_examples = examples.size();
81  const std::size_t num_of_branches = stats_estimator.getNumOfBranches();
82  const std::size_t num_of_features = fern.getNumOfFeatures();
83 
84  label_data.resize(num_of_examples);
85 
86  std::vector<std::vector<float>> results(num_of_features);
87  std::vector<std::vector<unsigned char>> flags(num_of_features);
88  std::vector<std::vector<unsigned char>> branch_indices(num_of_features);
89 
90  for (std::size_t feature_index = 0; feature_index < num_of_features;
91  ++feature_index) {
92  results[feature_index].reserve(num_of_examples);
93  flags[feature_index].reserve(num_of_examples);
94  branch_indices[feature_index].reserve(num_of_examples);
95 
96  feature_handler.evaluateFeature(fern.accessFeature(feature_index),
97  data_set,
98  examples,
99  results[feature_index],
100  flags[feature_index]);
101  stats_estimator.computeBranchIndices(results[feature_index],
102  flags[feature_index],
103  fern.accessThreshold(feature_index),
104  branch_indices[feature_index]);
105  }
106 
107  for (std::size_t example_index = 0; example_index < num_of_examples;
108  ++example_index) {
109  std::size_t node_index = 0;
110  for (std::size_t feature_index = 0; feature_index < num_of_features;
111  ++feature_index) {
112  node_index *= num_of_branches;
113  node_index += branch_indices[feature_index][example_index];
114  }
115 
116  label_data[example_index] = stats_estimator.getLabelOfNode(fern[node_index]);
117  }
118 }
119 
120 template <class FeatureType,
121  class DataSet,
122  class LabelType,
123  class ExampleIndex,
124  class NodeType>
125 void
131  stats_estimator,
132  DataSet& data_set,
133  std::vector<ExampleIndex>& examples,
134  std::vector<LabelType>& label_data)
135 {
136  const std::size_t num_of_examples = examples.size();
137  const std::size_t num_of_branches = stats_estimator.getNumOfBranches();
138  const std::size_t num_of_features = fern.getNumOfFeatures();
139 
140  std::vector<std::vector<float>> results(num_of_features);
141  std::vector<std::vector<unsigned char>> flags(num_of_features);
142  std::vector<std::vector<unsigned char>> branch_indices(num_of_features);
143 
144  for (std::size_t feature_index = 0; feature_index < num_of_features;
145  ++feature_index) {
146  results[feature_index].reserve(num_of_examples);
147  flags[feature_index].reserve(num_of_examples);
148  branch_indices[feature_index].reserve(num_of_examples);
149 
150  feature_handler.evaluateFeature(fern.accessFeature(feature_index),
151  data_set,
152  examples,
153  results[feature_index],
154  flags[feature_index]);
155  stats_estimator.computeBranchIndices(results[feature_index],
156  flags[feature_index],
157  fern.accessThreshold(feature_index),
158  branch_indices[feature_index]);
159  }
160 
161  for (std::size_t example_index = 0; example_index < num_of_examples;
162  ++example_index) {
163  std::size_t node_index = 0;
164  for (std::size_t feature_index = 0; feature_index < num_of_features;
165  ++feature_index) {
166  node_index *= num_of_branches;
167  node_index += branch_indices[feature_index][example_index];
168  }
169 
170  label_data[example_index] = stats_estimator.getLabelOfNode(fern[node_index]);
171  }
172 }
173 
174 template <class FeatureType,
175  class DataSet,
176  class LabelType,
177  class ExampleIndex,
178  class NodeType>
179 void
184  DataSet& data_set,
185  std::vector<ExampleIndex>& examples,
186  std::vector<NodeType*>& nodes)
187 {
188  const std::size_t num_of_examples = examples.size();
189  const std::size_t num_of_branches = stats_estimator.getNumOfBranches();
190  const std::size_t num_of_features = fern.getNumOfFeatures();
191 
192  nodes.reserve(num_of_examples);
193 
194  std::vector<std::vector<float>> results(num_of_features);
195  std::vector<std::vector<unsigned char>> flags(num_of_features);
196  std::vector<std::vector<unsigned char>> branch_indices(num_of_features);
197 
198  for (std::size_t feature_index = 0; feature_index < num_of_features;
199  ++feature_index) {
200  results[feature_index].reserve(num_of_examples);
201  flags[feature_index].reserve(num_of_examples);
202  branch_indices[feature_index].reserve(num_of_examples);
203 
204  feature_handler.evaluateFeature(fern.accessFeature(feature_index),
205  data_set,
206  examples,
207  results[feature_index],
208  flags[feature_index]);
209  stats_estimator.computeBranchIndices(results[feature_index],
210  flags[feature_index],
211  fern.accessThreshold(feature_index),
212  branch_indices[feature_index]);
213  }
214 
215  for (std::size_t example_index = 0; example_index < num_of_examples;
216  ++example_index) {
217  std::size_t node_index = 0;
218  for (std::size_t feature_index = 0; feature_index < num_of_features;
219  ++feature_index) {
220  node_index *= num_of_branches;
221  node_index += branch_indices[feature_index][example_index];
222  }
223 
224  nodes.push_back(&(fern[node_index]));
225  }
226 }
void evaluate(pcl::Fern< FeatureType, NodeType > &fern, 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.
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 void computeBranchIndices(std::vector< float > &results, std::vector< unsigned char > &flags, const float threshold, std::vector< unsigned char > &branch_indices) const =0
Computes the branch indices obtained by the specified threshold on the supplied feature evaluation re...
FeatureType & accessFeature(const std::size_t feature_index)
Access operator for features.
Definition: fern.h:164
FernEvaluator()
Constructor.
Define standard C methods and C++ classes that are common to all methods.
void getNodes(pcl::Fern< FeatureType, NodeType > &fern, 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.
virtual std::size_t getNumOfBranches() const =0
Returns the number of brances a node can have (e.g.
Class representing a Fern.
Definition: fern.h:49
virtual LabelDataType getLabelOfNode(NodeType &node) const =0
Returns the label of the specified node.
float & accessThreshold(const std::size_t threshold_index)
Access operator for thresholds.
Definition: fern.h:184
void evaluateAndAdd(pcl::Fern< FeatureType, NodeType > &fern, 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...
std::size_t getNumOfFeatures()
Returns the number of features the Fern has.
Definition: fern.h:79
virtual ~FernEvaluator()
Destructor.
Utility class interface which is used for creating and evaluating features.