VCFeaturePool.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /**
  2. * @file VCFeaturePool.cpp
  3. * @brief standard interface to feature pool classifiers
  4. * @author Erik Rodner
  5. * @date 07.09.2007
  6. */
  7. #include <iostream>
  8. #include "VCFeaturePool.h"
  9. #include "vislearning/cbaselib/VectorFeature.h"
  10. using namespace OBJREC;
  11. using namespace std;
  12. using namespace NICE;
  13. VCFeaturePool::VCFeaturePool(const Config *_conf, FeaturePoolClassifier *_fpc)
  14. : VecClassifier(_conf), fpc(_fpc)
  15. {
  16. dimension = 0;
  17. this->conf = _conf;
  18. readFeaturePool = conf->gB("VCFeaturePool", "read_feature_pool", false);
  19. featurePoolCache = conf->gS("VCFeaturePool", "feature_pool_cache", "features.txt");
  20. }
  21. VCFeaturePool::~VCFeaturePool()
  22. {
  23. }
  24. ClassificationResult VCFeaturePool::classify(const NICE::Vector & x) const
  25. {
  26. NICE::Vector *v = new NICE::Vector(x);
  27. Example pe(v);
  28. ClassificationResult r = fpc->classify(pe);
  29. delete v;
  30. return r;
  31. }
  32. void VCFeaturePool::teach(const LabeledSetVector & teachSet)
  33. {
  34. maxClassNo = teachSet.getMaxClassno();
  35. dimension = teachSet.dimension();
  36. LOOP_ALL(teachSet)
  37. {
  38. EACH(classno, x);
  39. NICE::Vector *v = new Vector(x);
  40. examples.push_back( pair<int, Example> (classno, Example(v)));
  41. }
  42. }
  43. void VCFeaturePool::setMaxClassNo(int maxClassNo)
  44. {
  45. VecClassifier::setMaxClassNo(maxClassNo);
  46. fpc->setMaxClassNo(maxClassNo);
  47. }
  48. void VCFeaturePool::clear()
  49. {
  50. fpc->clear();
  51. examples.clear();
  52. }
  53. void VCFeaturePool::store(std::ostream & os, int format) const
  54. {
  55. fpc->store(os, format);
  56. }
  57. void VCFeaturePool::restore(std::istream & is, int format)
  58. {
  59. fpc->restore(is, format);
  60. }
  61. void VCFeaturePool::finishTeaching()
  62. {
  63. FeaturePool fp(conf);
  64. if (readFeaturePool)
  65. {
  66. fprintf(stderr, "VCFeaturePool: reading features from %s\n", featurePoolCache.c_str());
  67. fp.read(featurePoolCache);
  68. fprintf(stderr, "VCFeaturePool: %d features read\n", (int)fp.size());
  69. fp.initRandomFeatureSelection();
  70. }
  71. else
  72. {
  73. Feature *f = new VectorFeature(dimension);
  74. f->explode(fp);
  75. delete f;
  76. }
  77. fpc->train(fp, examples);
  78. //FIXME: this will destroy all the copies of fp too
  79. //fp.destroy();
  80. examples.clean();
  81. }