Point Cloud Library (PCL)  1.14.1-dev
fern_trainer.h
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/feature_handler.h>
42 #include <pcl/ml/ferns/fern.h>
43 #include <pcl/ml/stats_estimator.h>
44 
45 #include <vector>
46 
47 namespace pcl {
48 
49 /** Trainer for a Fern. */
50 template <class FeatureType,
51  class DataSet,
52  class LabelType,
53  class ExampleIndex,
54  class NodeType>
56 
57 public:
58  /** Constructor. */
59  FernTrainer();
60 
61  /** Sets the feature handler used to create and evaluate features.
62  *
63  * \param[in] feature_handler the feature handler
64  */
65  inline void
68  {
69  feature_handler_ = &feature_handler;
70  }
71 
72  /** Sets the object for estimating the statistics for tree nodes.
73  *
74  * \param[in] stats_estimator the statistics estimator
75  */
76  inline void
79  {
80  stats_estimator_ = &stats_estimator;
81  }
82 
83  /** Sets the maximum depth of the learned tree.
84  *
85  * \param[in] fern_depth maximum depth of the learned tree
86  */
87  inline void
88  setFernDepth(const std::size_t fern_depth)
89  {
90  fern_depth_ = fern_depth;
91  }
92 
93  /** Sets the number of features used to find optimal decision features.
94  *
95  * \param[in] num_of_features the number of features
96  */
97  inline void
98  setNumOfFeatures(const std::size_t num_of_features)
99  {
100  num_of_features_ = num_of_features;
101  }
102 
103  /** Sets the number of thresholds tested for finding the optimal decision
104  * threshold on the feature responses.
105  *
106  * \param[in] num_of_threshold the number of thresholds
107  */
108  inline void
109  setNumOfThresholds(const std::size_t num_of_threshold)
110  {
111  num_of_thresholds_ = num_of_threshold;
112  }
113 
114  /** Sets the input data set used for training.
115  *
116  * \param[in] data_set the data set used for training
117  */
118  inline void
119  setTrainingDataSet(DataSet& data_set)
120  {
121  data_set_ = data_set;
122  }
123 
124  /** Example indices that specify the data used for training.
125  *
126  * \param[in] examples the examples
127  */
128  inline void
129  setExamples(std::vector<ExampleIndex>& examples)
130  {
131  examples_ = examples;
132  }
133 
134  /** Sets the label data corresponding to the example data.
135  *
136  * \param[in] label_data the label data
137  */
138  inline void
139  setLabelData(std::vector<LabelType>& label_data)
140  {
141  label_data_ = label_data;
142  }
143 
144  /** Trains a decision tree using the set training data and settings.
145  *
146  * \param[out] fern destination for the trained tree
147  */
148  void
149  train(Fern<FeatureType, NodeType>& fern);
150 
151 protected:
152  /** Creates uniformly distributed thresholds over the range of the supplied
153  * values.
154  *
155  * \param[in] num_of_thresholds the number of thresholds to create
156  * \param[in] values the values for estimating the expected value range
157  * \param[out] thresholds the resulting thresholds
158  */
159  static void
160  createThresholdsUniform(const std::size_t num_of_thresholds,
161  std::vector<float>& values,
162  std::vector<float>& thresholds);
163 
164 private:
165  /** Desired depth of the learned fern. */
166  std::size_t fern_depth_;
167  /** Number of features used to find optimal decision features. */
168  std::size_t num_of_features_;
169  /** Number of thresholds. */
170  std::size_t num_of_thresholds_;
171 
172  /** FeatureHandler instance, responsible for creating and evaluating features. */
174  /** StatsEstimator instance, responsible for gathering stats about a node. */
176 
177  /** The training data set. */
178  DataSet data_set_;
179  /** The label data. */
180  std::vector<LabelType> label_data_;
181  /** The example data. */
182  std::vector<ExampleIndex> examples_;
183 };
184 
185 } // namespace pcl
186 
187 #include <pcl/ml/impl/ferns/fern_trainer.hpp>
Utility class interface which is used for creating and evaluating features.
Class representing a Fern.
Definition: fern.h:49
Trainer for a Fern.
Definition: fern_trainer.h:55
void setTrainingDataSet(DataSet &data_set)
Sets the input data set used for training.
Definition: fern_trainer.h:119
void setFernDepth(const std::size_t fern_depth)
Sets the maximum depth of the learned tree.
Definition: fern_trainer.h:88
void setNumOfFeatures(const std::size_t num_of_features)
Sets the number of features used to find optimal decision features.
Definition: fern_trainer.h:98
void setStatsEstimator(pcl::StatsEstimator< LabelType, NodeType, DataSet, ExampleIndex > &stats_estimator)
Sets the object for estimating the statistics for tree nodes.
Definition: fern_trainer.h:77
void setNumOfThresholds(const std::size_t num_of_threshold)
Sets the number of thresholds tested for finding the optimal decision threshold on the feature respon...
Definition: fern_trainer.h:109
void setFeatureHandler(pcl::FeatureHandler< FeatureType, DataSet, ExampleIndex > &feature_handler)
Sets the feature handler used to create and evaluate features.
Definition: fern_trainer.h:66
void setLabelData(std::vector< LabelType > &label_data)
Sets the label data corresponding to the example data.
Definition: fern_trainer.h:139
void setExamples(std::vector< ExampleIndex > &examples)
Example indices that specify the data used for training.
Definition: fern_trainer.h:129
Define standard C methods and C++ classes that are common to all methods.
#define PCL_EXPORTS
Definition: pcl_macros.h:325