Point Cloud Library (PCL)  1.8.0
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 #ifndef PCL_ML_FERNS_FERN_EVALUATOR_HPP_
39 #define PCL_ML_FERNS_FERN_EVALUATOR_HPP_
40 
41 #include <pcl/common/common.h>
42 
43 #include <pcl/ml/ferns/fern.h>
44 #include <pcl/ml/feature_handler.h>
45 #include <pcl/ml/stats_estimator.h>
46 
47 #include <vector>
48 
49 
50 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
51 template <class FeatureType, class DataSet, class LabelType, class ExampleIndex, class NodeType>
53 {
54 }
55 
56 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
57 template <class FeatureType, class DataSet, class LabelType, class ExampleIndex, class NodeType>
59 {
60 }
61 
62 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
63 template <class FeatureType, class DataSet, class LabelType, class ExampleIndex, class NodeType>
64 void
69  DataSet & data_set,
70  std::vector<ExampleIndex> & examples,
71  std::vector<LabelType> & label_data)
72 {
73  const size_t num_of_examples = examples.size ();
74  const size_t num_of_branches = stats_estimator.getNumOfBranches ();
75  const size_t num_of_features = fern.getNumOfFeatures ();
76 
77  label_data.resize (num_of_examples);
78 
79  std::vector<std::vector<float> > results (num_of_features);
80  std::vector<std::vector<unsigned char> > flags (num_of_features);
81  std::vector<std::vector<unsigned char> > branch_indices (num_of_features);
82 
83  for (size_t feature_index = 0; feature_index < num_of_features; ++feature_index)
84  {
85  results[feature_index].reserve (num_of_examples);
86  flags[feature_index].reserve (num_of_examples);
87  branch_indices[feature_index].reserve (num_of_examples);
88 
89  feature_handler.evaluateFeature (fern.accessFeature (feature_index), data_set, examples, results[feature_index], flags[feature_index]);
90  stats_estimator.computeBranchIndices (results[feature_index], flags[feature_index], fern.accessThreshold (feature_index), branch_indices[feature_index]);
91  }
92 
93  for (size_t example_index = 0; example_index < num_of_examples; ++example_index)
94  {
95  size_t node_index = 0;
96  for (size_t feature_index = 0; feature_index < num_of_features; ++feature_index)
97  {
98  node_index *= num_of_branches;
99  node_index += branch_indices[feature_index][example_index];
100  }
101 
102  label_data[example_index] = stats_estimator.getLabelOfNode (fern[node_index]);
103  }
104 }
105 
106 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
107 template <class FeatureType, class DataSet, class LabelType, class ExampleIndex, class NodeType>
108 void
113  DataSet & data_set,
114  std::vector<ExampleIndex> & examples,
115  std::vector<LabelType> & label_data)
116 {
117  const size_t num_of_examples = examples.size ();
118  const size_t num_of_branches = stats_estimator.getNumOfBranches ();
119  const size_t num_of_features = fern.getNumOfFeatures ();
120 
121  std::vector<std::vector<float> > results (num_of_features);
122  std::vector<std::vector<unsigned char> > flags (num_of_features);
123  std::vector<std::vector<unsigned char> > branch_indices (num_of_features);
124 
125  for (size_t feature_index = 0; feature_index < num_of_features; ++feature_index)
126  {
127  results[feature_index].reserve (num_of_examples);
128  flags[feature_index].reserve (num_of_examples);
129  branch_indices[feature_index].reserve (num_of_examples);
130 
131  feature_handler.evaluateFeature (fern.accessFeature (feature_index), data_set, examples, results[feature_index], flags[feature_index]);
132  stats_estimator.computeBranchIndices (results[feature_index], flags[feature_index], fern.accessThreshold (feature_index), branch_indices[feature_index]);
133  }
134 
135  for (size_t example_index = 0; example_index < num_of_examples; ++example_index)
136  {
137  size_t node_index = 0;
138  for (size_t feature_index = 0; feature_index < num_of_features; ++feature_index)
139  {
140  node_index *= num_of_branches;
141  node_index += branch_indices[feature_index][example_index];
142  }
143 
144  label_data[example_index] = stats_estimator.getLabelOfNode (fern[node_index]);
145  }
146 }
147 
148 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
149 template <class FeatureType, class DataSet, class LabelType, class ExampleIndex, class NodeType>
150 void
155  DataSet & data_set,
156  std::vector<ExampleIndex> & examples,
157  std::vector<NodeType*> & nodes)
158 {
159  const size_t num_of_examples = examples.size ();
160  const size_t num_of_branches = stats_estimator.getNumOfBranches ();
161  const size_t num_of_features = fern.getNumOfFeatures ();
162 
163  nodes.reserve (num_of_examples);
164 
165  std::vector<std::vector<float> > results (num_of_features);
166  std::vector<std::vector<unsigned char> > flags (num_of_features);
167  std::vector<std::vector<unsigned char> > branch_indices (num_of_features);
168 
169  for (size_t feature_index = 0; feature_index < num_of_features; ++feature_index)
170  {
171  results[feature_index].reserve (num_of_examples);
172  flags[feature_index].reserve (num_of_examples);
173  branch_indices[feature_index].reserve (num_of_examples);
174 
175  feature_handler.evaluateFeature (fern.accessFeature (feature_index), data_set, examples, results[feature_index], flags[feature_index]);
176  stats_estimator.computeBranchIndices (results[feature_index], flags[feature_index], fern.accessThreshold (feature_index), branch_indices[feature_index]);
177  }
178 
179  for (size_t example_index = 0; example_index < num_of_examples; ++example_index)
180  {
181  size_t node_index = 0;
182  for (size_t feature_index = 0; feature_index < num_of_features; ++feature_index)
183  {
184  node_index *= num_of_branches;
185  node_index += branch_indices[feature_index][example_index];
186  }
187 
188  nodes.push_back (&(fern[node_index]));
189  }
190 }
191 
192 #endif
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.
size_t getNumOfFeatures()
Returns the number of features the Fern has.
Definition: fern.h:88
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.
float & accessThreshold(const size_t threshold_index)
Access operator for thresholds.
Definition: fern.h:186
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...
FernEvaluator()
Constructor.
FeatureType & accessFeature(const size_t feature_index)
Access operator for features.
Definition: fern.h:168
virtual size_t getNumOfBranches() const =0
Returns the number of brances a node can have (e.g.
Class representing a Fern.
Definition: fern.h:51
virtual LabelDataType getLabelOfNode(NodeType &node) const =0
Returns the label of the specified node.
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.
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...
virtual ~FernEvaluator()
Destructor.
Utility class interface which is used for creating and evaluating features.