Loading...
Searching...
No Matches
tree_train.h
/* *************************************************
*
* Copyright (2011) Willow Garage
*
* Author : Cedric Cagniart
* ************************************************* */
#pragma once
#include "tree.h"
#include <boost/array.hpp>
namespace pcl
{
namespace gpu
{
namespace people
{
namespace trees
{
// ################################################
// ################################################
// histogram stuff
class Histogram : public boost::array<std::uint32_t,NUMLABELS> {
public :
inline Histogram() { std::fill(begin(), end(), 0); }
};
struct HistogramPair {
public :
// accumulate on the histograms
inline void accumTrue(const Label label ) {
m_h_true[label]++;
}
inline void accumFalse(const Label label ) {
m_h_false[label]++;
}
inline Histogram& h_false() { return m_h_false; }
inline Histogram& h_true() { return m_h_true; }
inline const Histogram h_false() const { return m_h_false; }
inline const Histogram h_true() const { return m_h_true; }
protected :
Histogram m_h_false;
Histogram m_h_true;
};
// ###############################################
// ###############################################
// SplitPoint
struct SplitPoint{
inline SplitPoint( int ai, Attrib t):attribId(ai), threshold(t){}
int attribId;
Attrib threshold;
};
// ###############################################
// ###############################################
// Data Structures as stored in binary files
struct LabeledAttrib {
inline LabeledAttrib(){}
inline LabeledAttrib( const Label& label, const Attrib& attrib): l(label), a(attrib){}
Label l;
Attrib a;
};
// this is only going to be a helper structure
struct LabeledFeature { // : boost::noncopyable {
// constructors
inline LabeledFeature(): l(NOLABEL){
}
inline LabeledFeature( const LabeledFeature& B){
l = B.l;
std::copy( B.attribs, B.attribs + NUMATTRIBS, attribs );
}
Label l; // WARNING the compiler will pad here
Attrib attribs[NUMATTRIBS];
};
// compute the number of elements
static inline std::uint64_t numElements( const Histogram& h ) {
std::uint64_t Ntotal = 0;
for(int li=0;li<NUMLABELS;++li) Ntotal += std::uint64_t(h[li]);
return Ntotal;
}
/**
* This is cool
*/
static inline double entropy( const Histogram& h ) {
double Ntotal = numElements(h);
double entropy = 0.;
for(int li=0;li<NUMLABELS;++li) {
if( h[li] != 0 ) {
double p = double(h[li]) / Ntotal;
entropy -= p*std::log(p);
}
}
return entropy;
}
/**
* This is a little weird.. it will just compute the entropy of the merged histograms
*/
static inline double entropy_merged( const HistogramPair& hp ) {
const Histogram& htrue = hp.h_true();
const Histogram& hfalse = hp.h_false();
double Ntotal = numElements(htrue) + numElements(hfalse);
double entropy = 0.;
for(int li=0;li<NUMLABELS;++li) {
std::uint64_t Ni = std::uint64_t(htrue[li]) + std::uint64_t(hfalse[li]);
if( Ni != 0) {
double p = double(Ni) / Ntotal;
entropy -= p*std::log(p);
}
}
return entropy;
}
/**
* This will compute the gain in information resulting from the split
*/
static inline double informationGain( const HistogramPair& hp) {
double e0 = entropy_merged(hp);
double etrue = entropy(hp.h_true());
double efalse = entropy(hp.h_false());
double Ntrue = numElements(hp.h_true());
double Nfalse = numElements(hp.h_false());
double Ntotal = Ntrue + Nfalse;
// lets avoid division by 0
if( Ntotal == 0 ) return 0.;
return e0 - (Ntrue/Ntotal)*etrue - (Nfalse/Ntotal)*efalse;
}
// #########################################
// #########################################
// Reading and writing histograms
static inline std::ostream& operator << (std::ostream& os, const Histogram& h) {
for(int li=0;li<NUMLABELS;++li) os<< h[li]<<" ";
os<<"\n";
return os;
}
static inline std::istream& operator >> (std::istream& is, Histogram& h) {
for(int li=0;li<NUMLABELS;++li) is >> h[li];
return is;
}
// #######################################
// #######################################
// reading and writing histogram Pairs
static inline std::ostream& operator << ( std::ostream& os, const HistogramPair& hp) {
os << hp.h_false();
os << hp.h_true();
return os;
}
static inline std::istream& operator >> ( std::istream& is, HistogramPair& hp) {
is >> hp.h_false();
is >> hp.h_true();
return is;
}
// #########################################
// #########################################
// Reading and writing LabeledFeature Vectors ( label + collection of attrib )
static void writeLabeledFeatureVec( std::ostream& os, const std::vector<LabeledFeature>& lfs ){
os.write( (const char*)&lfs[0], sizeof(LabeledFeature)*lfs.size() );
}
// static void readLabeledFeature( std::istream& is, LabeledFeature& lf)
// {
// is.read( (char*)&lf, sizeof(LabeledFeature) );
// if( is.fail() ) throw std::runtime_error();
// }
// #######################################
// #######################################
// reading and writing split points
inline std::ostream& operator << ( std::ostream& os, const SplitPoint& sp){
os<<sp.attribId<<" "<<sp.threshold<<"\n";
return os;
}
inline std::istream& operator >> ( std::istream& is, SplitPoint& sp){
is >> sp.attribId >> sp.threshold;
return is;
}
// #######################################
// #######################################
// reading and writing info files
inline void writeInfoFile( const std::string& filename,
int attribId,
Attrib threshold,
double gain,
const HistogramPair& HP){
std::ofstream fout(filename.c_str() );
if( !fout.is_open() ) throw std::runtime_error(std::string("(E) could not open ") + filename );
fout<<int(attribId)<<" "<<int(threshold)<<"\n";
fout<<gain<<"\n";
fout<<HP;
}
inline void readInfoFile( const std::string& filename,
int& attribId,
Attrib& threshold,
double& gain,
HistogramPair& HP ) {
std::ifstream fin(filename.c_str() );
if( !fin.is_open() ) throw std::runtime_error(std::string("(E) could not open") + filename );
fin>>attribId >>threshold>>gain>>HP;
if( fin.fail() ) throw std::runtime_error(std::string("(E) malformed splitInfo file ") + filename );
}
} // end namespace trees
} // end namespace people
} // end namespace gpu
} // end namespace pcl
pcl::B
@ B
Definition norms.h:54
pcl::gpu::people::trees
Definition tree.h:54
pcl::gpu::people::trees::writeInfoFile
void writeInfoFile(const std::string &filename, int attribId, Attrib threshold, double gain, const HistogramPair &HP)
Definition tree_train.h:199
pcl::gpu::people::trees::entropy
static double entropy(const Histogram &h)
This is cool.
Definition tree_train.h:93
pcl::gpu::people::trees::numElements
static std::uint64_t numElements(const Histogram &h)
Definition tree_train.h:84
pcl::gpu::people::trees::informationGain
static double informationGain(const HistogramPair &hp)
This will compute the gain in information resulting from the split.
Definition tree_train.h:127
pcl::gpu::people::trees::writeLabeledFeatureVec
static void writeLabeledFeatureVec(std::ostream &os, const std::vector< LabeledFeature > &lfs)
Definition tree_train.h:173
pcl::gpu::people::trees::operator>>
std::istream & operator>>(std::istream &is, AttribLocation &aloc)
Definition tree.h:107
pcl::gpu::people::trees::readInfoFile
void readInfoFile(const std::string &filename, int &attribId, Attrib &threshold, double &gain, HistogramPair &HP)
Definition tree_train.h:212
pcl::gpu::people::trees::operator<<
std::ostream & operator<<(std::ostream &os, const AttribLocation &aloc)
Definition tree.h:106
pcl::gpu::people::trees::entropy_merged
static double entropy_merged(const HistogramPair &hp)
This is a little weird.
Definition tree_train.h:108
pcl::gpu::people::trees::Attrib
std::int16_t Attrib
Definition tree.h:72
pcl::gpu::people::trees::Label
std::uint8_t Label
Definition tree.h:73
pcl::gpu::people
Definition bodyparts_detector.h:63
pcl::gpu::people::NOLABEL
@ NOLABEL
Definition label_common.h:108
pcl::gpu
Definition device_array.h:45
pcl
Definition convolution.h:46
pcl::gpu::people::trees::Histogram
Definition tree.h:100
pcl::gpu::people::trees::Histogram::Histogram
Histogram()
Definition tree_train.h:26
pcl::gpu::people::trees::HistogramPair
Definition tree_train.h:29
pcl::gpu::people::trees::HistogramPair::h_false
Histogram & h_false()
Definition tree_train.h:39
pcl::gpu::people::trees::HistogramPair::accumTrue
void accumTrue(const Label label)
Definition tree_train.h:32
pcl::gpu::people::trees::HistogramPair::h_true
Histogram & h_true()
Definition tree_train.h:40
pcl::gpu::people::trees::HistogramPair::accumFalse
void accumFalse(const Label label)
Definition tree_train.h:35
pcl::gpu::people::trees::HistogramPair::m_h_false
Histogram m_h_false
Definition tree_train.h:46
pcl::gpu::people::trees::HistogramPair::m_h_true
Histogram m_h_true
Definition tree_train.h:47
pcl::gpu::people::trees::HistogramPair::h_false
const Histogram h_false() const
Definition tree_train.h:42
pcl::gpu::people::trees::HistogramPair::h_true
const Histogram h_true() const
Definition tree_train.h:43
pcl::gpu::people::trees::LabeledAttrib::l
Label l
Definition tree_train.h:65
pcl::gpu::people::trees::LabeledAttrib::a
Attrib a
Definition tree_train.h:66
pcl::gpu::people::trees::LabeledAttrib::LabeledAttrib
LabeledAttrib()
Definition tree_train.h:63
pcl::gpu::people::trees::LabeledAttrib::LabeledAttrib
LabeledAttrib(const Label &label, const Attrib &attrib)
Definition tree_train.h:64
pcl::gpu::people::trees::LabeledFeature
Definition tree_train.h:70
pcl::gpu::people::trees::LabeledFeature::attribs
Attrib attribs[NUMATTRIBS]
Definition tree_train.h:79
pcl::gpu::people::trees::LabeledFeature::LabeledFeature
LabeledFeature(const LabeledFeature &B)
Definition tree_train.h:74
pcl::gpu::people::trees::LabeledFeature::l
Label l
Definition tree_train.h:78
pcl::gpu::people::trees::LabeledFeature::LabeledFeature
LabeledFeature()
Definition tree_train.h:72
pcl::gpu::people::trees::SplitPoint
Definition tree_train.h:53
pcl::gpu::people::trees::SplitPoint::attribId
int attribId
Definition tree_train.h:55
pcl::gpu::people::trees::SplitPoint::threshold
Attrib threshold
Definition tree_train.h:56
pcl::gpu::people::trees::SplitPoint::SplitPoint
SplitPoint(int ai, Attrib t)
Definition tree_train.h:54