bovizeObjectBankFeatures.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /**
  2. * @file boviceObjectBankFeatures.cpp
  3. * @brief convert ObjectBank features to a sparse BoV histogram representation
  4. * @author Erik Rodner
  5. * @date 01/23/2012
  6. */
  7. #include <algorithm>
  8. #include "core/basics/Config.h"
  9. #include "vislearning/cbaselib/MultiDataset.h"
  10. #include "vislearning/cbaselib/MutualInformation.h"
  11. #include "vislearning/baselib/Globals.h"
  12. using namespace std;
  13. using namespace NICE;
  14. using namespace OBJREC;
  15. void boviceFeatures ( const Config & conf, const Vector & thresholds,
  16. const LabeledSet & ls, const string & srcExtension, const string & dstExtension )
  17. {
  18. string cacheroot = conf.gS("cache", "root");
  19. LOOP_ALL_S ( ls )
  20. {
  21. EACH_S(classno, imgfn);
  22. Globals::setCurrentImgFN ( imgfn );
  23. string cachefn = Globals::getCacheFilename ( cacheroot, Globals::SORT_CATEGORIES ) + srcExtension;
  24. cerr << "processing " << cachefn << endl;
  25. vector<double> x;
  26. ifstream ifs ( cachefn.c_str(), ios::in );
  27. if ( ! ifs.good() )
  28. fthrow(Exception, "File not found: " << cachefn );
  29. while ( !ifs.eof() ) {
  30. double val = 0.0;
  31. if ( ifs >> val )
  32. x.push_back(val);
  33. }
  34. ifs.close();
  35. Vector xnew ( 177, 0.0 );
  36. for ( uint i = 0 ; i < 177; i++ )
  37. for ( uint j = 0 ; j < 252 ; j++ )
  38. xnew[i] += ( x[j + i*252] > thresholds[i] ) ? 1.0 : 0.0;
  39. xnew.normalizeL1();
  40. string dst_cachefn = Globals::getCacheFilename ( cacheroot, Globals::SORT_CATEGORIES ) + dstExtension;
  41. ofstream ofs ( dst_cachefn.c_str(), ios::out );
  42. if ( ! ofs.good() )
  43. fthrow(Exception, "Unable to write to " << dst_cachefn );
  44. ofs << xnew << endl;
  45. ofs.close ();
  46. }
  47. }
  48. /**
  49. convert ObjectBank features to a sparse histogram representation
  50. */
  51. int main (int argc, char **argv)
  52. {
  53. std::set_terminate(__gnu_cxx::__verbose_terminate_handler);
  54. Config conf ( argc, argv );
  55. MultiDataset md ( &conf );
  56. const LabeledSet *train = md["train"];
  57. const LabeledSet *test = md["test"];
  58. string dstExtention = conf.gS("main", "dstext", ".txt");
  59. string threshFile = conf.gS("main", "threshfile");
  60. Vector thresholds ( 177 );
  61. ifstream ifs ( threshFile.c_str(), ios::in );
  62. if ( !ifs.good() )
  63. fthrow(Exception, "Unable to open threshold file!");
  64. int index = 0;
  65. while ( !ifs.eof() )
  66. {
  67. double val;
  68. if ( ifs >> val ) {
  69. if ( index >= thresholds.size() )
  70. fthrow(Exception, "Error parsing threshold file!");
  71. thresholds[index] = val;
  72. }
  73. index++;
  74. }
  75. ifs.close();
  76. boviceFeatures ( conf, thresholds, *train, ".jpg.feat", dstExtention );
  77. boviceFeatures ( conf, thresholds, *test, ".jpg.feat", dstExtention );
  78. return 0;
  79. }