21 #ifndef _REAL_DECISION_STUMP_ORACLE_H
22 #define _REAL_DECISION_STUMP_ORACLE_H
34 #include "Classifier/RealDecisionStump.h"
50 template<
class FeatureGenerator,
class Aggregator,
class Metric>
92 void Internal_StreamEvaluate_1(
const R * h,
int size,
unsigned int i0,
unsigned int i1);
99 template<
class FeatureExtractor>
107 void StreamEvaluate_1(
const R * h,
int size);
110 void Internal_Sort_Store(
int i0,
int i1)
112 for(
int i=i0; i<i1; ++i)
114 sort_pattern(&m_store[i * m_training_set.Size()], m_training_set.Size());
124 int samples = m_training_set.Size();
128 std::sort(&store[0], &store[samples]);
133 return Metric::optimize(h, store, samples, m_dP, m_dN);
138 void Internal_Optimize(
double *out,
ClassifierType *h,
int i0,
int i1)
140 for(
int i = i0; i< i1; ++i)
142 out[i] = Optimize(&m_store[i * m_training_set.Size()], h[i]);
153 m_featureGenerator(0) { }
160 m_training_set = set;
166 m_featureGenerator = &f;
169 void SetFastHeuristic(
bool enable,
bool reoptimize,
int size) {
176 return m_training_set;
180 void SetNumberOfThreads(
int th) {
201 return Optimize(m_store, h);
209 template<
class FeatureType,
class Aggregator,
class Metric>
216 template<
class FeatureType,
class Aggregator,
class Metric>
224 for(
unsigned int i =0; i<m_training_set.Size(); i++)
225 if(m_training_set.templates[i].category == 1)
226 m_dP += m_training_set.templates[i].d;
228 m_dN += m_training_set.templates[i].d;
232 template<
class FeatureType,
class Aggregator,
class Metric>
239 if(m_featureGenerator == 0)
241 std::cerr <<
"No Feature Generator loaded. Use SetFeatureGenerator API before call GetHypothesis" << std::endl;
245 bestH.debug_name(
"internal error" );
248 if(m_training_set.Size() == 0)
250 std::cerr <<
"No pattern loaded. Init Trainer Failed" << std::endl;
257 std::cout <<
"Train with:" << m_training_set.n_patternP <<
"(+), " << m_training_set.n_patternN <<
"(-) using " << m_featureGenerator->Count() <<
" features.\n";
261 m_featureGenerator->Reset();
263 if(m_preloadCount<=1)
270 while(m_featureGenerator->Next(h))
284 std::cout << count <<
" (" << (100.0f * (double)count/(
double)m_featureGenerator->Count()) <<
"%): name:" << bestH.debug_name() <<
" th:" << bestH.th <<
" p:" << bestH.pr <<
" n:" << bestH.nr <<
" Z:" << 2.0*bestS << std::endl;
287 if((count & (32*1024-1))==0)
289 float fs = count/t.GetTime();
290 float pr = (float)count/(
float)m_featureGenerator->Count();
291 int rem = (m_featureGenerator->Count() - count)/fs;
292 std::cout << fs <<
" feature/secs: " << (100.0f * pr) <<
"% ";
294 std::cout << rem <<
"s remaining" << std::endl;
296 std::cout << rem/60 <<
"min remaining" << std::endl;
305 static const int m_threadCount = 1;
311 m_store =
new PatternResponse [m_training_set.Size() * m_preloadCount];
315 double *responses =
new double[m_preloadCount];
317 std::cout << (
sizeof(
PatternResponse) * m_training_set.Size() * m_preloadCount +
sizeof(
ClassifierType) * m_preloadCount)/(1024*1204) <<
"Mb used for cache ("<< m_preloadCount <<
" features evaluated on " << m_threadCount <<
" threads)" << std::endl;
326 for(
int i =0; i<m_preloadCount; i++)
327 if(m_featureGenerator->Next(h[h_count]) )
334 StreamEvaluate_1(h, h_count);
343 for(
int k=0; k<m_threadCount; ++k)
345 unsigned int i0 = (h_count * k) / m_threadCount;
346 unsigned int i1 = (h_count * (k+1)) / m_threadCount;
348 thread_pool_.
create_thread(sprint::thread_bind(&RealDecisionStumpOracle::Internal_Optimize,
this, responses, h, i0, i1));
355 Internal_Optimize(responses, h, 0, h_count);
357 for(
int i =0; i<h_count; ++i)
359 if(responses[i] < bestS)
361 bestS = responses[i];
364 std::cout << count <<
" (" << (100.0f * (double)count/(
double)m_featureGenerator->Count()) <<
"%): name:" << bestH.debug_name() <<
" th:" << bestH.th <<
" p:" << bestH.pr <<
" n:" << bestH.nr <<
" s:" << bestS << std::endl;
368 if((count & (32*1024-1))==0)
370 float fs = count/t.GetTime();
371 float pr = (float)count/(
float)m_featureGenerator->Count();
372 int rem = (m_featureGenerator->Count() - count)/fs;
373 std::cout << fs <<
" feature/secs: " << (100.0f * pr) <<
"% ";
375 std::cout << rem <<
"s remaining" << std::endl;
377 std::cout << rem/60 <<
"min remaining" << std::endl;
382 }
while(h_count==m_preloadCount);
392 template<
class FeatureType,
class Aggregator,
class Metric>
400 for(
int k=0; k<m_threadCount; ++k)
402 unsigned int i0 = (m_training_set.Size() * k) / m_threadCount;
403 unsigned int i1 = (m_training_set.Size() * (k+1)) / m_threadCount;
405 thread_pool_.
create_thread(sprint::thread_bind(&RealDecisionStumpOracle::template Internal_StreamEvaluate_1<R>,
this, h, size, i0, i1));
412 Internal_StreamEvaluate_1(h, size, 0, m_training_set.Size());
415 template<
class FeatureType,
class Aggregator,
class Metric>
bool GetHypothesis(ClassifierType &bestH)
Definition: RealDecisionStumpOracle.h:233
void SetTrainingSet(const DataSetHandle< Aggregator > &set)
Set the training set used train the classifier.
Definition: RealDecisionStumpOracle.h:158
Feature FeatureType
The feature type generate by this generator.
Definition: FeatureGenerator.h:41
DataSetHandle< Aggregator > & GetTrainingSet()
return R/W the training set
Definition: RealDecisionStumpOracle.h:174
A classifier composed by a Feature Extractor and an Evaluation Policy A "Second Level" classifier...
Definition: BinaryClassifier.h:38
FeatureGenerator::FeatureType FeatureType
The Feature Extracted by FeatureGenerator.
Definition: RealDecisionStumpOracle.h:55
Definition: FeatureGenerator.h:36
void ComputeFeaturesResponse(BinaryWeightedPatternResponse< int > *store, const FeatureExtractor *h, int n_feature, const Set &set, int i0, int i1)
Definition: WeightedPatternResponse.h:85
void ExtractFeature(BinaryWeightedPatternResponse< int > *store, const FeatureExtractor &h, const Set &set)
extract from templates feature using h and put in store, and associating the weighted category d ...
Definition: WeightedPatternResponse.h:105
ClassifierType
Definition: Types.h:31
void SetPreloadSize(int n)
Definition: RealDecisionStumpOracle.h:187
Cross Platform High Performance timer.
Definition: thread_group.h:82
image/size TODO namespace
Definition: Types.h:39
void join_all()
wait all threads terminate
Definition: thread_group.h:114
WeightedPatternResponse methods and utility functions.
BinaryClassifier< FeatureType, RealDecisionStump< int > > ClassifierType
The weak classifier provided by this oracle.
Definition: RealDecisionStumpOracle.h:58
BinaryWeightedPatternResponse< int > PatternResponse
A weighted pattern response with integer values.
Definition: WeightedPatternResponse.h:64
this file declare the virtual feature generator for Haar Features
void sort_pattern(PatternResponse *store, int n)
Definition: RealDecisionStumpOracle.h:51
a FeatureExtractor return a scalar number without relationship with classification ...
Definition: Types.h:35
bool create_thread(const sprint::thread_function &p)
create an additional thread
Definition: thread_group.h:102
A Binary Classifier. Implements the class BinaryClassifier.
void SetFeatureGenerator(FeatureGenerator &f)
Associate a Feature Generator to Decision Stump Generator.
Definition: RealDecisionStumpOracle.h:164
double Optimize(ClassifierType &h)
Using current metrics try to recompute parameters associated to this feature.
Definition: RealDecisionStumpOracle.h:197
Definition: WeightedPatternResponse.h:38