21 #ifndef _BAYESIAN_STUMP_ORACLE_H
22 #define _BAYESIAN_STUMP_ORACLE_H
32 struct detail_thread_bayesian_stump {
40 template<
class FeatureGenerator,
class Aggregator>
103 for(
unsigned int i =0; i<m_training_set.Size(); ++i)
105 mem[i] = h_best.response( getData1( m_training_set.templates[i], m_training_set), getData2(m_training_set.templates[i], m_training_set) );
108 f_min = f_max = mem[0];
119 for(
int i =0; i<32; ++i) p[i]=n[i]=0.0;
121 h_best.m_featRange = f_max - f_min + 1;
122 h_best.m_minFeatValue = f_min;
123 h_best.m_truthTable = 0;
126 for(
unsigned int i =0; i<m_training_set.Size(); ++i)
128 int bin = (h_best.bin_size*(mem[i] - h_best.m_minFeatValue)) / h_best.m_featRange;
131 if(bin<0 || bin > h_best.bin_size)
133 std::cerr <<
"invalid feature: " << bin <<
"/" << h_best.bin_size <<
" | min = " << f_min <<
" | max = " << f_max <<
" " << h_best.debug_name() << std::endl;
137 if( m_training_set.templates[i].category == 1)
138 p[bin] += m_training_set.templates[i].d;
140 n[bin] += m_training_set.templates[i].d;
147 for(
int i =0; i<h_best.bin_size; ++i) {
150 h_best.m_truthTable |= 1<<i;
162 void Thread_OptimizeClassifier(detail_thread_bayesian_stump<WeakClassifierType> * ptr )
164 ptr->w = OptimizeClassifier(ptr->h, ptr->mem);
172 m_training_set = set;
178 m_featureGenerator = &f;
181 void SetFastHeuristic(
bool enable,
bool reoptimize,
int size) {
189 for(
unsigned int i =0; i<m_training_set.Size(); i++)
190 if( m_training_set.templates[i].d > d)
192 m_training_set.templates[i].d = d;
196 std::cout << n <<
" samples of " << m_training_set.Size() <<
" (" << (n*100)/m_training_set.Size() <<
"%) have been limited in weight (" << d <<
")" << std::endl;
202 return m_training_set;
213 void SetPreloadSize(
int n) {
221 response =
new int [m_training_set.Size() ];
222 w = OptimizeClassifier(h, response);
236 if(m_featureGenerator == 0)
238 std::cerr <<
"No Feature Generator loaded. Use SetFeatureGenerator API before call GetHypothesis" << std::endl;
242 bestH.debug_name(
"internal error");
245 if(m_training_set.Size() == 0)
247 std::cerr <<
"No pattern loaded. Init Trainer Failed" << std::endl;
251 if(m_threadCount<=1) m_threadCount = 1;
256 m_featureGenerator->
Reset();
258 response =
new int [m_training_set.Size() * m_threadCount ];
262 detail_thread_bayesian_stump<WeakClassifierType> * storage =
new detail_thread_bayesian_stump<WeakClassifierType>[m_threadCount];
264 for(
int i =0; i<m_threadCount; ++i)
265 storage[i].mem = response + i * m_training_set.Size();
267 while( m_featureGenerator->
Next( storage[token].h ) )
270 if(token == m_threadCount)
277 for(
int i =0; i<m_threadCount; ++i)
278 thread_pool_.
create_thread(sprint::thread_bind(&BayesianStumpOracle::Thread_OptimizeClassifier,
this, &storage[i]));
283 Thread_OptimizeClassifier(&storage[0]);
286 for(
int i =0; i<m_threadCount; ++i)
289 double w = storage[i].w;
293 bestH = storage[i].h;
294 std::cout << count <<
" (" << (100.0f * (double)count/(
double)m_featureGenerator->
Count()) <<
"%): name:" << bestH.debug_name() <<
" w+:"<< bestW << std::endl;
297 if((count & (32*1024-1))==0)
299 float fs = count/t.GetTime();
300 float pr = (float)count/(
float)m_featureGenerator->
Count();
301 int rem = (m_featureGenerator->
Count() - count)/fs;
302 std::cout << fs <<
" feature/secs: " << (100.0f * pr) <<
"% ";
304 std::cout << rem <<
"s remaining" << std::endl;
306 std::cout << rem/60 <<
"min remaining" << std::endl;
318 for(
int i =0; i<token; ++i)
319 thread_pool_.
create_thread(sprint::thread_bind(&BayesianStumpOracle::Thread_OptimizeClassifier,
this, &storage[i]));
324 Thread_OptimizeClassifier(&storage[0]);
327 for(
int i =0; i<token; ++i)
330 double w = storage[i].w;
334 bestH = storage[i].h;
335 std::cout << count <<
" (" << (100.0f * (double)count/(
double)m_featureGenerator->
Count()) <<
"%): name:" << bestH.debug_name() <<
" w+:"<< bestW << std::endl;
346 while( m_featureGenerator->
Next(h) )
348 double w = OptimizeClassifier(h, response);
356 std::cout << count <<
" (" << (100.0f * (double)count/(
double)m_featureGenerator->
Count()) <<
"%): name:" << bestH.debug_name() <<
" w+:"<< bestW << std::endl;
359 if((count & (32*1024-1))==0)
361 float fs = count/t.GetTime();
362 float pr = (float)count/(
float)m_featureGenerator->
Count();
363 int rem = (m_featureGenerator->
Count() - count)/fs;
364 std::cout << fs <<
" feature/secs: " << (100.0f * pr) <<
"% ";
366 std::cout << rem <<
"s remaining" << std::endl;
368 std::cout << rem/60 <<
"min remaining" << std::endl;
378 std::cout <<
"Expected W:" << bestW<< std::endl;
Feature FeatureType
The feature type generate by this generator.
Definition: FeatureGenerator.h:41
DataSetHandle< Aggregator > & GetTrainingSet()
return R/W the training set
Definition: BayesianStumpOracle.h:200
A classifier composed by a Feature Extractor and an Evaluation Policy A "Second Level" classifier...
Definition: BinaryClassifier.h:38
virtual void Reset()=0
reset any interal counters
double Optimize(ClassifierType &h)
Using current metrics try to recompute parameters associated to this feature.
Definition: BayesianStumpOracle.h:218
FeatureGenerator::FeatureType FeatureType
The Feature Extracted by FeatureGenerator.
Definition: BayesianStumpOracle.h:47
Definition: FeatureGenerator.h:36
BoostableClassifier< WeakClassifierType > ClassifierType
The weak classifier reported by this oracle.
Definition: BayesianStumpOracle.h:53
Definition: thread_group.h:82
image/size TODO namespace
Definition: Types.h:39
void LimitTrainingSetWeight(double d)
Definition: BayesianStumpOracle.h:186
void join_all()
wait all threads terminate
Definition: thread_group.h:114
BinaryClassifier< FeatureType, BayesianStump< int > > WeakClassifierType
The interal weak classifier.
Definition: BayesianStumpOracle.h:50
virtual bool Next(Feature &out)=0
return the next feature, or return false
bool create_thread(const sprint::thread_function &p)
create an additional thread
Definition: thread_group.h:102
void SetNumberOfThreads(int th)
Change the thread number used in Multi Threading training.
Definition: BayesianStumpOracle.h:206
void SetFeatureGenerator(FeatureGenerator &f)
Associate a Feature Generator to Decision Stump Generator.
Definition: BayesianStumpOracle.h:176
void SetTrainingSet(const DataSetHandle< Aggregator > &set)
Set the training set used to recover the threshold.
Definition: BayesianStumpOracle.h:170
Definition: BoostableClassifier.h:40
virtual unsigned int Count() const =0
return the count of feature available
Definition: BayesianStumpOracle.h:41
An hystogram based Bayesian classifier Implements the class BayesianStump for BinaryClassifier.