21 #ifndef _BINARY_CLASSIFIER_ORACLE_H
22 #define _BINARY_CLASSIFIER_ORACLE_H
49 template<
class FeatureGenerator,
class ClassifierPolicy,
class Metric,
class Aggregator>
75 bool m_useFastOptimize;
78 unsigned int m_fastOptimizeBinSize;
97 void Internal_StreamEvaluate_1(
const R * h,
int size,
unsigned int i0,
unsigned int i1);
103 void Internal_StreamEvaluateAndBucket_1(
const R * h,
int size,
unsigned int i0,
unsigned int i1);
106 void Internal_Sort_Store(
int i0,
int i1)
108 for(
int i=i0; i<i1; ++i)
110 sort_pattern(&m_store[i * m_training_set.Size()], m_training_set.Size());
123 unsigned int n = m_training_set.Size();
127 for(
int i=i0; i<i1; ++i)
131 if(m_useFastOptimize)
136 curW = Metric::optimize(h[i], work, m_fastOptimizeBinSize, m_dP, m_dN);
146 curW = Metric::optimize(h[i], &m_store[i * n], n, m_dP, m_dN );
190 template<
class FeatureExtractor>
208 void StreamEvaluate_1(
const R * h,
int size);
213 void StreamEvaluateAndBucket_1(
const R * h,
int size);
220 BinaryClassifierOracle() : m_store(0), m_preloadCount(0), m_useFastOptimize(false), m_fastOptimizeBinSize(1), m_featureGenerator(0)
229 template<
class SourceDataSet>
232 m_training_set = set;
238 m_featureGenerator = &f;
244 return m_training_set;
248 void SetNumberOfThreads(
int th) {
265 std::cerr <<
"SetFastHeuristic called with an invalid parameter: size=" << size << std::endl;
269 m_useFastOptimize = enable;
270 m_fastOptimizeBinSize = size;
284 int *work =
new int [ m_training_set.Size() ];
289 return Metric::optimize(h, m_store, m_fastOptimizeBinSize, m_dP, m_dN);
302 Internal_Sort_Store(0, 1);
306 return Metric::optimize(h, m_store, m_training_set.Size(), m_dP, m_dN);
313 template<
class FeatureType,
class ClassifierPolicy,
class Metric,
class Aggregator>
320 template<
class FeatureType,
class ClassifierPolicy,
class Metric,
class Aggregator>
328 for(
unsigned int i =0; i<m_training_set.Size(); i++)
329 if(m_training_set.templates[i].category == 1)
330 m_dP += m_training_set.templates[i].d;
332 m_dN += m_training_set.templates[i].d;
334 energy = m_dP + m_dN;
335 double f = 1.0 / energy;
338 for(
unsigned int i =0; i<m_training_set.Size(); i++)
339 m_training_set.templates[i].d *= f;
340 std::cout <<
"* W+:" << m_dP <<
" W-:" << m_dN <<
" (reweighted from " << energy <<
")" << std::endl;
343 template<
class FeatureType,
class ClassifierPolicy,
class Metric,
class Aggregator>
369 template<
class FeatureType,
class ClassifierPolicy,
class Metric,
class Aggregator>
377 for(
int k=0; k<m_threadCount; ++k)
379 unsigned int i0 = (m_training_set.Size() * k) / m_threadCount;
380 unsigned int i1 = (m_training_set.Size() * (k+1)) / m_threadCount;
382 thread_pool_.
create_thread(sprint::thread_bind(&BinaryClassifierOracle::template Internal_StreamEvaluate_1<R>,
this, h, size, i0, i1));
389 Internal_StreamEvaluate_1(h, size, 0, m_training_set.Size());
418 template<
class FeatureType,
class ClassifierPolicy,
class Metric,
class Aggregator>
424 int n = m_training_set.Size();
430 if(m_featureGenerator == 0)
432 std::cerr <<
"No Feature Generator loaded. Use SetFeatureGenerator API before call GetHypothesis" << std::endl;
436 bestH.debug_name(
"internal error" );
440 std::cerr <<
"No pattern loaded. Init Trainer Failed" << std::endl;
448 std::cout <<
"Train with:" << m_training_set.n_patternP <<
"(+), " << m_training_set.n_patternN <<
"(-) using " << m_featureGenerator->Count() <<
" features.\n";
454 m_featureGenerator->Reset();
456 int n_feat = m_featureGenerator->Count();
460 last_check = t.GetTime();
463 if(m_preloadCount<=1)
471 m_store =
new PatternResponse [std::max<int>(n, m_fastOptimizeBinSize) ];
474 while(m_featureGenerator->Next(h))
478 if(m_useFastOptimize)
479 curW = Fast_Optimize(h);
490 std::cout << count <<
" (" << (100.0f * (double)count/(
double)n_feat) <<
"%): ";
491 if(bestH.debug_name()) std::cout <<
"name:" << bestH.debug_name() <<
" ";
492 std::cout <<
"parity:" << bestH.parity <<
" th:" << bestH.th <<
" w+:"<< bestW <<
", w-:" << maxW-bestW << std::endl;
496 if ((t.GetTime() - last_check) > 10.0)
498 float fs = count/t.GetTime();
499 float pr = (float)count/(
float)n_feat;
500 int rem = (n_feat - count)/fs;
501 std::cout << fs <<
" feature/secs: " << (100.0f * pr) <<
"% ";
503 std::cout << rem <<
"s remaining" << std::endl;
505 std::cout << rem/60 <<
"min remaining" << std::endl;
507 last_check = t.GetTime();
518 static const int m_threadCount = 1;
521 double *t_bestW =
new double[m_threadCount];
543 for(
int i =0; i<m_preloadCount; i++)
544 if(m_featureGenerator->Next(h[h_count]) )
551 StreamEvaluate_1(h, h_count);
558 for(
int k=0; k<m_threadCount; ++k)
560 unsigned int i0 = (h_count * k) / m_threadCount;
561 unsigned int i1 = (h_count * (k+1)) / m_threadCount;
565 thread_pool_.
create_thread(sprint::thread_bind(&BinaryClassifierOracle::Internal_Optimize,
this, i0, i1, &t_bestH[k], &t_bestW[k], h));
571 for(
int k=0; k<m_threadCount; ++k)
573 if(t_bestW[k] > bestW)
584 Internal_Optimize(0, h_count, &bestH, &bestW, h);
593 float pr = (float)count/(
float)n_feat;
594 std::cout <<
" (" << 100.0f * pr <<
"%) ";
595 if(bestH.debug_name()) std::cout <<
"name:" << bestH.debug_name() <<
" ";
596 std::cout <<
"parity:" << bestH.parity <<
" th:" << bestH.th <<
" w+:"<< bestW <<
" w-:" << maxW - bestW << std::endl;
601 if ((t.GetTime() - last_check) > 10.0)
603 float fs = count/t.GetTime();
604 float pr = (float)count/(
float)n_feat;
605 int rem = (n_feat - count)/fs;
606 std::cout << fs <<
" feature/secs: " << (100.0f * pr) <<
"% ";
608 std::cout << rem <<
"s remaining" << std::endl;
610 std::cout << rem/60 <<
"min remaining" << std::endl;
612 last_check = t.GetTime();
615 }
while(h_count==m_preloadCount);
627 if(m_useFastOptimize)
629 std::cout <<
"[reoptimize] [before] parity:" << bestH.parity <<
" th:" << bestH.th <<
" w+:"<< bestW <<
", w-:" << maxW-bestW << std::endl;
632 bestW = Optimize(bestH);
634 std::cout <<
"[reoptimize] [after] parity:" << bestH.parity <<
" th:" << bestH.th <<
" w+:"<< bestW <<
", w-:" << maxW-bestW << std::endl;
638 std::cout <<
"best classifier: " << bestW <<
"/" << maxW <<
": exit" << std::endl;
641 return bestW>=maxW*0.5;
Feature FeatureType
The feature type generate by this generator.
Definition: FeatureGenerator.h:41
FeatureGenerator::FeatureType FeatureType
The Feature Extracted by FeatureGenerator.
Definition: BinaryClassifierOracle.h:54
A classifier composed by a Feature Extractor and an Evaluation Policy A "Second Level" classifier...
Definition: BinaryClassifier.h:38
a Voting Boostable classifier
bool GetHypothesis(WeakClassifierType &bestH)
Definition: BinaryClassifierOracle.h:419
Definition: FeatureGenerator.h:36
void SetFeatureGenerator(FeatureGenerator &f)
Associate a Feature Generator to Decision Stump Generator.
Definition: BinaryClassifierOracle.h:236
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
void SetTrainingSet(const SourceDataSet &set)
Set the training set used to recover the threshold.
Definition: BinaryClassifierOracle.h:230
void ExtractFeatureAndBucket(BinaryWeightedPatternResponse< int > *store, const FeatureExtractor &h, const Set &set, int n_bucket, int *value)
Definition: WeightedPatternResponse.h:129
Cross Platform High Performance timer.
double Optimize(WeakClassifierType &h)
Using current metrics try to recompute parameters associated to this feature.
Definition: BinaryClassifierOracle.h:293
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.
proposal 1 for thread group
double Fast_Optimize(WeakClassifierType &h)
Definition: BinaryClassifierOracle.h:281
BoostableClassifier< WeakClassifierType > ClassifierType
Additive Regression Final Classifier.
Definition: BinaryClassifierOracle.h:60
Definition: SourceDataSet.h:46
BinaryWeightedPatternResponse< int > PatternResponse
A weighted pattern response with integer values.
Definition: WeightedPatternResponse.h:64
void SetFastHeuristic(bool enable, bool reoptimize, int size)
Definition: BinaryClassifierOracle.h:262
void SetPreloadSize(int n)
Definition: BinaryClassifierOracle.h:255
void sort_pattern(PatternResponse *store, int n)
a FeatureExtractor return a scalar number without relationship with classification ...
Definition: Types.h:35
method to create function pointer for thread call
bool create_thread(const sprint::thread_function &p)
create an additional thread
Definition: thread_group.h:102
A Binary Classifier. Implements the class BinaryClassifier.
Definition: WeightedPatternResponse.h:38
Definition: BinaryClassifierOracle.h:50
Definition: BoostableClassifier.h:40
DataSetHandle< Aggregator > & GetTrainingSet()
return R/W the training set
Definition: BinaryClassifierOracle.h:242
void GenerateBucket(BinaryWeightedPatternResponse< int > *store, int n_bucket, const BinaryWeightedPatternResponse< int > *source, int n_element)
BinaryClassifier< FeatureType, ClassifierPolicy > WeakClassifierType
The weak classifier provided by this oracle.
Definition: BinaryClassifierOracle.h:57