X-Boost  2.3.8
FeatureGenerator.h
Go to the documentation of this file.
1 /* XBoost: Ada-Boost and Friends on Haar/ICF/HOG Features, Library and ToolBox
2  *
3  * Copyright (c) 2008-2014 Paolo Medici <medici@ce.unipr.it>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20 
25 #ifndef _FEATURE_GENERATOR_H
26 #define _FEATURE_GENERATOR_H
27 
28 #include "Utility/timer.h"
29 #include "Utility/random.h"
30 
31 
35 template<class Feature>
37 
38 public:
39 
41  typedef Feature FeatureType;
42 
43 public:
44  virtual ~FeatureGenerator() { }
45 
47  virtual bool CanBePrecomputed() const { return false; }
49  virtual unsigned int Count() const = 0;
51  virtual bool Next(Feature & out) = 0;
53  virtual void Reset() = 0;
54 };
55 
56 
58 
60 template<class T>
61 bool RandomSampleFeature(std::vector<T> & out, const std::vector<T> & in, int n)
62 {
63  std::vector<bool> sampled;
64  if(in.size()<=n)
65  {
66  std::cerr << "Error: too few features" << std::endl;
67  out = in;
68  return false;
69  }
70  out.clear();
71  out.reserve(n);
72  for(int i=0;i<n;++i)
73  out.push_back( in[ nrand(in.size() ) ] );
74  return true;
75 }
76 
78 template<class FeatureGenerator>
79 void GenerateFeatures(std::vector<typename FeatureGenerator::FeatureType> & features, FeatureGenerator & generator)
80 {
81  Timer t;
83 // PatternResponse *pr = 0;
84 
85 // if(prune_weak_classifier)
86 // pr = new PatternResponse[m_validate_templates.size()];
87 //
88  std::cout << "Generating feature from " << generator.Feature() << " haar bases..." << std::endl;
89 
90  int count = 0;
91 
92  t.Start();
93  generator.Reset();
94  while(generator.Next(h) ) {
95 
96  features.push_back(h);
97 
98  count++;
99  if((count & (4096-1))==0)
100  {
101  std::cout << '.';
102  std::cout.flush();
103  }
104  }
105  std::cout << count << " feature generated in " << t.GetTime() << "s. Keeped " << features.size() << "\n";
106 
107 // delete [] pr;
108 }
109 
111 template<class TFeature>
113  typename std::vector<TFeature>::const_iterator i;
114 
115  public:
116 
117  std::vector<TFeature> features;
119  int n_bases;
120 
121  public:
122 
123  typedef TFeature FeatureType;
124 
125  public:
126 
129 
131  unsigned int Feature() const { return n_bases; }
132 
133  void Reset() {
134  i = features.begin();
135  }
136  bool Next(TFeature & out)
137  {
138  if (i==features.end()) return false;
139 
140  out = *i;
141  ++i;
142  return true;
143  }
144 };
145 
146 #endif
Feature FeatureType
The feature type generate by this generator.
Definition: FeatureGenerator.h:41
Definition: timer.h:84
bool RandomSampleFeature(std::vector< T > &out, const std::vector< T > &in, int n)
Definition: FeatureGenerator.h:61
virtual void Reset()=0
reset any interal counters
unsigned int Feature() const
number of bases (cosmetic)
Definition: FeatureGenerator.h:131
Definition: FeatureGenerator.h:36
This generator uses a stored feature list.
Definition: FeatureGenerator.h:112
Cross Platform High Performance timer.
void GenerateFeatures(std::vector< typename FeatureGenerator::FeatureType > &features, FeatureGenerator &generator)
Fill vector of FeatureType using a FeatureGenerator object.
Definition: FeatureGenerator.h:79
virtual bool CanBePrecomputed() const
Test if the feature generator can be precomputed. In this case Count must be valid.
Definition: FeatureGenerator.h:47
virtual bool Next(Feature &out)=0
return the next feature, or return false
int n_bases
only for cosmetic
Definition: FeatureGenerator.h:119
some methods about random number generation
virtual unsigned int Count() const =0
return the count of feature available