FeaturePool.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /**
  2. * @file FeaturePool.cpp
  3. * @brief collection of features
  4. * @author Erik Rodner
  5. * @date 04/21/2008
  6. */
  7. #include <iostream>
  8. #include <stdlib.h>
  9. #include "FeaturePool.h"
  10. #include "VectorFeature.h"
  11. using namespace OBJREC;
  12. using namespace std;
  13. using namespace NICE;
  14. FeaturePool::FeaturePool()
  15. {
  16. conf = NULL;
  17. //srand48(time(NULL));
  18. }
  19. FeaturePool::FeaturePool(const Config *conf)
  20. {
  21. this->conf = conf;
  22. }
  23. FeaturePool::~FeaturePool()
  24. {
  25. }
  26. Feature *FeaturePool::getRandomFeature() const
  27. {
  28. assert(size() == cumsum.size());
  29. assert(! empty());
  30. double sum = cumsum[ cumsum.size() - 1 ];
  31. double pos = drand48() * sum;
  32. vector<double>::const_iterator j = lower_bound(cumsum.begin(), cumsum.end(), pos);
  33. uint index = distance(cumsum.begin(), j);
  34. const_iterator i = begin() + index;
  35. return i->second;
  36. }
  37. void FeaturePool::initRandomFeatureSelection()
  38. {
  39. double sum = 0.0;
  40. cumsum.clear();
  41. cumsum.reserve( size() );
  42. for ( vector< pair<double, Feature *> >::const_iterator i = begin();
  43. i != end();
  44. i++ )
  45. {
  46. sum += i->first;
  47. cumsum.push_back ( sum );
  48. }
  49. }
  50. void FeaturePool::addFeature(Feature *f, double probability)
  51. {
  52. assert(finite(probability));
  53. assert(probability > 10e-12);
  54. push_back(pair<double, Feature *> (probability, f));
  55. }
  56. void FeaturePool::destroy()
  57. {
  58. for (vector< pair<double, Feature *> >::iterator i = begin();
  59. i != end();
  60. i++)
  61. {
  62. assert(i->second != NULL);
  63. delete i->second;
  64. }
  65. vector<pair<double, Feature *> >::clear();
  66. }
  67. void FeaturePool::restore(istream & is, int format)
  68. {
  69. vector<pair<int, double> > tmpPool;
  70. clear();
  71. fprintf(stderr, "FeaturePool::restore: reading plain vector features!\n");
  72. while ( !is.eof() )
  73. {
  74. double weight = 1.0;
  75. if ( !(is >> weight) ) break;
  76. std::string feature_tag;
  77. if ( !(is >> feature_tag) ) break;
  78. if ( feature_tag != "VECTORFEATURE" ) {
  79. fthrow(Exception, "FeaturePool::restore: if you want to read features other than VectorFeature, use createFeatures::...");
  80. }
  81. int feature_index;
  82. if ( !(is >> feature_index) ) break;
  83. tmpPool.push_back ( pair<int, double> ( feature_index, weight ) );
  84. }
  85. for ( vector<pair<int, double> >::const_iterator i = tmpPool.begin();
  86. i != tmpPool.end(); i++ )
  87. {
  88. int feature_index = i->first;
  89. double weight = i->second;
  90. Feature *f = new VectorFeature ( tmpPool.size(), feature_index );
  91. push_back ( pair<double, Feature *> ( weight, f ) );
  92. }
  93. }
  94. void FeaturePool::store(ostream & os, int format) const
  95. {
  96. for ( const_iterator i = begin(); i != end(); i++ )
  97. {
  98. const Feature *f = i->second;
  99. os << i->first << " ";
  100. f->store ( os, format );
  101. os << endl;
  102. }
  103. }
  104. void FeaturePool::clear()
  105. {
  106. destroy();
  107. }
  108. void FeaturePool::calcFeatureVector ( const Example & pe, NICE::Vector & x ) const
  109. {
  110. x.resize ( size() );
  111. int index = 0;
  112. for ( const_iterator i = begin(); i != end(); i++, index++ )
  113. {
  114. const Feature *f = i->second;
  115. x[index] = f->val ( &pe );
  116. }
  117. }