Point Cloud Library (PCL)  1.8.1
tree_train.h
1 /* *************************************************
2  *
3  * Copyright (2011) Willow Garage
4  *
5  * Author : Cedric Cagniart
6  * ************************************************* */
7 
8 #ifndef PCL_GPU_PEOPLE_TREE_TRAIN_H_
9 #define PCL_GPU_PEOPLE_TREE_TRAIN_H_
10 
11 #include "tree.h"
12 #include <boost/array.hpp>
13 
14 namespace pcl
15 {
16  namespace gpu
17  {
18  namespace people
19  {
20  namespace trees
21  {
22  // ################################################
23  // ################################################
24  // histogram stuff
25  class Histogram : public boost::array<uint32_t,NUMLABELS> {
26  public :
27  inline Histogram() { std::fill(begin(), end(), 0); }
28  };
29 
30  struct HistogramPair {
31  public :
32  // accumulate on the histograms
33  inline void accumTrue(const Label label ) {
34  m_h_true[label]++;
35  }
36  inline void accumFalse(const Label label ) {
37  m_h_false[label]++;
38  }
39 
40  inline Histogram& h_false() { return m_h_false; }
41  inline Histogram& h_true() { return m_h_true; }
42 
43  inline const Histogram h_false() const { return m_h_false; }
44  inline const Histogram h_true() const { return m_h_true; }
45 
46  protected :
49  };
50 
51  // ###############################################
52  // ###############################################
53  // SplitPoint
54  struct SplitPoint{
55  inline SplitPoint( int ai, Attrib t):attribId(ai), threshold(t){}
56  int attribId;
58  };
59 
60  // ###############################################
61  // ###############################################
62  // Data Structures as stored in binary files
63  struct LabeledAttrib {
64  inline LabeledAttrib(){}
65  inline LabeledAttrib( const Label& label, const Attrib& attrib): l(label), a(attrib){}
68  };
69 
70  // this is only going to be a helper structure
71  struct LabeledFeature { // : boost::noncopyable {
72  // constructors
73  inline LabeledFeature(): l(NOLABEL){
74  }
75  inline LabeledFeature( const LabeledFeature& B){
76  l = B.l;
77  std::copy( B.attribs, B.attribs + NUMATTRIBS, attribs );
78  }
79  Label l; // WARNING the compiler will pad here
80  Attrib attribs[NUMATTRIBS];
81  };
82 
83 
84  // compute the number of elements
85  static inline uint64_t numElements( const Histogram& h ) {
86  uint64_t Ntotal = 0;
87  for(int li=0;li<NUMLABELS;++li) Ntotal += uint64_t(h[li]);
88  return Ntotal;
89  }
90 
91  /**
92  * This is cool
93  */
94  static inline double entropy( const Histogram& h ) {
95  double Ntotal = numElements(h);
96  double entropy = 0.;
97  for(int li=0;li<NUMLABELS;++li) {
98  if( h[li] != 0 ) {
99  double p = double(h[li]) / Ntotal;
100  entropy -= p*log(p);
101  }
102  }
103  return entropy;
104  }
105 
106  /**
107  * This is a little weird.. it will just compute the entropy of the merged histograms
108  */
109  static inline double entropy_merged( const HistogramPair& hp ) {
110  const Histogram& htrue = hp.h_true();
111  const Histogram& hfalse = hp.h_false();
112 
113  double Ntotal = numElements(htrue) + numElements(hfalse);
114  double entropy = 0.;
115  for(int li=0;li<NUMLABELS;++li) {
116  uint64_t Ni = uint64_t(htrue[li]) + uint64_t(hfalse[li]);
117  if( Ni != 0) {
118  double p = double(Ni) / Ntotal;
119  entropy -= p*log(p);
120  }
121  }
122  return entropy;
123  }
124 
125  /**
126  * This will compute the gain in information resulting from the split
127  */
128  static inline double informationGain( const HistogramPair& hp) {
129  double e0 = entropy_merged(hp);
130  double etrue = entropy(hp.h_true());
131  double efalse = entropy(hp.h_false());
132 
133  double Ntrue = numElements(hp.h_true());
134  double Nfalse = numElements(hp.h_false());
135  double Ntotal = Ntrue + Nfalse;
136 
137  // lets avoid division by 0
138  if( Ntotal == 0 ) return 0.;
139  return e0 - (Ntrue/Ntotal)*etrue - (Nfalse/Ntotal)*efalse;
140  }
141 
142  // #########################################
143  // #########################################
144  // Reading and writing histograms
145  static inline std::ostream& operator << (std::ostream& os, const Histogram& h) {
146  for(int li=0;li<NUMLABELS;++li) os<< h[li]<<" ";
147  os<<"\n";
148  return os;
149  }
150 
151  static inline std::istream& operator >> (std::istream& is, Histogram& h) {
152  for(int li=0;li<NUMLABELS;++li) is >> h[li];
153  return is;
154  }
155 
156  // #######################################
157  // #######################################
158  // reading and writing histogram Pairs
159  static inline std::ostream& operator << ( std::ostream& os, const HistogramPair& hp) {
160  os << hp.h_false();
161  os << hp.h_true();
162  return os;
163  }
164 
165  static inline std::istream& operator >> ( std::istream& is, HistogramPair& hp) {
166  is >> hp.h_false();
167  is >> hp.h_true();
168  return is;
169  }
170 
171  // #########################################
172  // #########################################
173  // Reading and writing LabeledFeature Vectors ( label + collection of attrib )
174  static void writeLabeledFeatureVec( std::ostream& os, const std::vector<LabeledFeature>& lfs ){
175  os.write( (const char*)&lfs[0], sizeof(LabeledFeature)*lfs.size() );
176  }
177 
178  // static void readLabeledFeature( std::istream& is, LabeledFeature& lf)
179  // {
180  // is.read( (char*)&lf, sizeof(LabeledFeature) );
181  // if( is.fail() ) throw std::runtime_error();
182  // }
183 
184  // #######################################
185  // #######################################
186  // reading and writing split points
187  inline std::ostream& operator << ( std::ostream& os, const SplitPoint& sp){
188  os<<sp.attribId<<" "<<sp.threshold<<"\n";
189  return os;
190  }
191 
192  inline std::istream& operator >> ( std::istream& is, SplitPoint& sp){
193  is >> sp.attribId >> sp.threshold;
194  return is;
195  }
196 
197  // #######################################
198  // #######################################
199  // reading and writing info files
200  inline void writeInfoFile( const std::string& filename,
201  int attribId,
202  Attrib threshold,
203  double gain,
204  const HistogramPair& HP){
205  std::ofstream fout(filename.c_str() );
206  if( !fout.is_open() ) throw std::runtime_error(std::string("(E) could not open ") + filename );
207 
208  fout<<int(attribId)<<" "<<int(threshold)<<"\n";
209  fout<<gain<<"\n";
210  fout<<HP;
211  }
212 
213  inline void readInfoFile( const std::string& filename,
214  int& attribId,
215  Attrib& threshold,
216  double& gain,
217  HistogramPair& HP ) {
218  std::ifstream fin(filename.c_str() );
219  if( !fin.is_open() ) throw std::runtime_error(std::string("(E) could not open") + filename );
220 
221  fin>>attribId >>threshold>>gain>>HP;
222  if( fin.fail() ) throw std::runtime_error(std::string("(E) malformed splitInfo file ") + filename );
223  }
224 
225 
226  } // end namespace trees
227  } // end namespace people
228  } // end namespace gpu
229 } // end namespace pcl
230 #endif
std::ostream & operator<<(std::ostream &os, const AttribLocation &aloc)
Definition: tree.h:107
const Histogram h_false() const
Definition: tree_train.h:43
std::istream & operator>>(std::istream &is, AttribLocation &aloc)
Definition: tree.h:108
const Histogram h_true() const
Definition: tree_train.h:44
static uint64_t numElements(const Histogram &h)
Definition: tree_train.h:85
static double informationGain(const HistogramPair &hp)
This will compute the gain in information resulting from the split.
Definition: tree_train.h:128
static void writeLabeledFeatureVec(std::ostream &os, const std::vector< LabeledFeature > &lfs)
Definition: tree_train.h:174
void accumTrue(const Label label)
Definition: tree_train.h:33
void readInfoFile(const std::string &filename, int &attribId, Attrib &threshold, double &gain, HistogramPair &HP)
Definition: tree_train.h:213
LabeledFeature(const LabeledFeature &B)
Definition: tree_train.h:75
static double entropy_merged(const HistogramPair &hp)
This is a little weird.
Definition: tree_train.h:109
LabeledAttrib(const Label &label, const Attrib &attrib)
Definition: tree_train.h:65
void accumFalse(const Label label)
Definition: tree_train.h:36
void writeInfoFile(const std::string &filename, int attribId, Attrib threshold, double gain, const HistogramPair &HP)
Definition: tree_train.h:200
Definition: norms.h:55
static double entropy(const Histogram &h)
This is cool.
Definition: tree_train.h:94