/** * @file FeaturePool.cpp * @brief collection of features * @author Erik Rodner * @date 04/21/2008 */ #include #include #include "FeaturePool.h" #include "vislearning/features/fpfeatures/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 ]; #ifdef WIN32 double pos = (double( rand() ) / RAND_MAX )* sum; #else double pos = drand48() * sum; #endif vector::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 >::const_iterator i = begin(); i != end(); i++ ) { sum += i->first; cumsum.push_back ( sum ); } } void FeaturePool::addFeature(Feature *f, double probability) { assert(NICE::isFinite(probability)); assert(probability > 10e-12); push_back(pair (probability, f)); } void FeaturePool::destroy() { for (vector< pair >::iterator i = begin(); i != end(); i++) { assert(i->second != NULL); delete i->second; } vector >::clear(); } void FeaturePool::restore(istream & is, int format) { vector > 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 ( feature_index, weight ) ); } for ( vector >::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 ( 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 ); } }