FeaturePool.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 "vislearning/features/fpfeatures/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. #ifdef WIN32
  32. double pos = (double( rand() ) / RAND_MAX )* sum;
  33. #else
  34. double pos = drand48() * sum;
  35. #endif
  36. vector<double>::const_iterator j = lower_bound(cumsum.begin(), cumsum.end(), pos);
  37. uint index = distance(cumsum.begin(), j);
  38. const_iterator i = begin() + index;
  39. return i->second;
  40. }
  41. void FeaturePool::initRandomFeatureSelection()
  42. {
  43. double sum = 0.0;
  44. cumsum.clear();
  45. cumsum.reserve( size() );
  46. for ( vector< pair<double, Feature *> >::const_iterator i = begin();
  47. i != end();
  48. i++ )
  49. {
  50. sum += i->first;
  51. cumsum.push_back ( sum );
  52. }
  53. }
  54. void FeaturePool::addFeature(Feature *f, double probability)
  55. {
  56. assert(NICE::isFinite(probability));
  57. assert(probability > 10e-12);
  58. push_back(pair<double, Feature *> (probability, f));
  59. }
  60. void FeaturePool::destroy()
  61. {
  62. for (vector< pair<double, Feature *> >::iterator i = begin();
  63. i != end();
  64. i++)
  65. {
  66. assert(i->second != NULL);
  67. delete i->second;
  68. }
  69. vector<pair<double, Feature *> >::clear();
  70. }
  71. void FeaturePool::restore(istream & is, int format)
  72. {
  73. vector<pair<int, double> > tmpPool;
  74. clear();
  75. fprintf(stderr, "FeaturePool::restore: reading plain vector features!\n");
  76. while ( !is.eof() )
  77. {
  78. double weight = 1.0;
  79. if ( !(is >> weight) ) break;
  80. std::string feature_tag;
  81. if ( !(is >> feature_tag) ) break;
  82. if ( feature_tag != "VECTORFEATURE" ) {
  83. fthrow(Exception, "FeaturePool::restore: if you want to read features other than VectorFeature, use createFeatures::...");
  84. }
  85. int feature_index;
  86. if ( !(is >> feature_index) ) break;
  87. tmpPool.push_back ( pair<int, double> ( feature_index, weight ) );
  88. }
  89. for ( vector<pair<int, double> >::const_iterator i = tmpPool.begin();
  90. i != tmpPool.end(); i++ )
  91. {
  92. int feature_index = i->first;
  93. double weight = i->second;
  94. Feature *f = new VectorFeature ( tmpPool.size(), feature_index );
  95. push_back ( pair<double, Feature *> ( weight, f ) );
  96. }
  97. }
  98. void FeaturePool::store(ostream & os, int format) const
  99. {
  100. for ( const_iterator i = begin(); i != end(); i++ )
  101. {
  102. const Feature *f = i->second;
  103. os << i->first << " ";
  104. f->store ( os, format );
  105. os << endl;
  106. }
  107. }
  108. void FeaturePool::clear()
  109. {
  110. destroy();
  111. }
  112. void FeaturePool::calcFeatureVector ( const Example & pe, NICE::Vector & x ) const
  113. {
  114. x.resize ( size() );
  115. int index = 0;
  116. for ( const_iterator i = begin(); i != end(); i++, index++ )
  117. {
  118. const Feature *f = i->second;
  119. x[index] = f->val ( &pe );
  120. }
  121. }