VCFeaturePool.cpp 2.0 KB

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