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