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