123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- /**
- * @file FeaturePool.cpp
- * @brief collection of features
- * @author Erik Rodner
- * @date 04/21/2008
- */
- #include <iostream>
- #include <stdlib.h>
- #include "FeaturePool.h"
- #include "VectorFeature.h"
- using namespace OBJREC;
- using namespace std;
- using namespace NICE;
- FeaturePool::FeaturePool()
- {
- conf = NULL;
- //srand48(time(NULL));
- }
- FeaturePool::FeaturePool(const Config *conf)
- {
- this->conf = conf;
- }
- FeaturePool::~FeaturePool()
- {
- }
- Feature *FeaturePool::getRandomFeature() const
- {
- assert(size() == cumsum.size());
- assert(! empty());
- double sum = cumsum[ cumsum.size() - 1 ];
- double pos = drand48() * sum;
- vector<double>::const_iterator j = lower_bound(cumsum.begin(), cumsum.end(), pos);
- uint index = distance(cumsum.begin(), j);
- const_iterator i = begin() + index;
- return i->second;
- }
- void FeaturePool::initRandomFeatureSelection()
- {
- double sum = 0.0;
- cumsum.clear();
- cumsum.reserve( size() );
- for ( vector< pair<double, Feature *> >::const_iterator i = begin();
- i != end();
- i++ )
- {
- sum += i->first;
- cumsum.push_back ( sum );
- }
- }
- void FeaturePool::addFeature(Feature *f, double probability)
- {
- assert(finite(probability));
- assert(probability > 10e-12);
- push_back(pair<double, Feature *> (probability, f));
- }
- void FeaturePool::destroy()
- {
- for (vector< pair<double, Feature *> >::iterator i = begin();
- i != end();
- i++)
- {
- assert(i->second != NULL);
- delete i->second;
- }
- vector<pair<double, Feature *> >::clear();
- }
- void FeaturePool::restore(istream & is, int format)
- {
- vector<pair<int, double> > tmpPool;
- clear();
- fprintf(stderr, "FeaturePool::restore: reading plain vector features!\n");
- while ( !is.eof() )
- {
- double weight = 1.0;
- if ( !(is >> weight) ) break;
- std::string feature_tag;
- if ( !(is >> feature_tag) ) break;
- if ( feature_tag != "VECTORFEATURE" ) {
- fthrow(Exception, "FeaturePool::restore: if you want to read features other than VectorFeature, use createFeatures::...");
- }
- int feature_index;
- if ( !(is >> feature_index) ) break;
- tmpPool.push_back ( pair<int, double> ( feature_index, weight ) );
- }
-
- for ( vector<pair<int, double> >::const_iterator i = tmpPool.begin();
- i != tmpPool.end(); i++ )
- {
- int feature_index = i->first;
- double weight = i->second;
- Feature *f = new VectorFeature ( tmpPool.size(), feature_index );
- push_back ( pair<double, Feature *> ( weight, f ) );
- }
- }
- void FeaturePool::store(ostream & os, int format) const
- {
- for ( const_iterator i = begin(); i != end(); i++ )
- {
- const Feature *f = i->second;
- os << i->first << " ";
- f->store ( os, format );
- os << endl;
- }
- }
- void FeaturePool::clear()
- {
- destroy();
- }
- void FeaturePool::calcFeatureVector ( const Example & pe, NICE::Vector & x ) const
- {
- x.resize ( size() );
- int index = 0;
- for ( const_iterator i = begin(); i != end(); i++, index++ )
- {
- const Feature *f = i->second;
- x[index] = f->val ( &pe );
- }
- }
|