datatools.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #ifndef DATATOOLSINCLUDE
  2. #define DATATOOLSINCLUDE
  3. #include <iostream>
  4. #include <core/basics/Config.h>
  5. #include <vislearning/baselib/Globals.h>
  6. #include <vislearning/cbaselib/MultiDataset.h>
  7. #include <vislearning/cbaselib/LabeledSet.h>
  8. using namespace std;
  9. using namespace NICE;
  10. using namespace OBJREC;
  11. template <typename MatrixType, typename VectorType>
  12. void readData ( const NICE::Config & conf, const OBJREC::LabeledSet & ls, MatrixType & X, Vector & y, std::string extension = ".txt" )
  13. {
  14. std::string cacheroot = conf.gS("cache", "root");
  15. y.resize ( ls.count() );
  16. X.clear();
  17. LOOP_ALL_S ( ls )
  18. {
  19. EACH_S(classno, imgfn);
  20. Globals::setCurrentImgFN ( imgfn );
  21. std::string cachefn = Globals::getCacheFilename ( cacheroot, Globals::SORT_CATEGORIES ) + extension;
  22. std::cerr << "fn: " << imgfn << " cachefn: " << cachefn << std::endl;
  23. y[ X.size() ] = classno;
  24. VectorType x;
  25. std::ifstream ifs ( cachefn.c_str(), std::ios::in );
  26. if ( ! ifs.good() )
  27. fthrow(Exception, "File not found: " << cachefn );
  28. ifs >> x;
  29. X.push_back ( x );
  30. ifs.close();
  31. }
  32. }
  33. template <typename MatrixType, typename VectorType>
  34. void readData ( const NICE::Config & conf, const OBJREC::LabeledSet & ls, MatrixType & X, Vector & y, std::vector<std::string> & filenames, std::string extension = ".txt" )
  35. {
  36. std::string cacheroot = conf.gS("cache", "root");
  37. y.resize ( ls.count() );
  38. X.clear();
  39. filenames.clear();
  40. LOOP_ALL_S ( ls )
  41. {
  42. EACH_S(classno, imgfn);
  43. Globals::setCurrentImgFN ( imgfn );
  44. std::string cachefn = Globals::getCacheFilename ( cacheroot, Globals::SORT_CATEGORIES ) + extension;
  45. std::cerr << "fn: " << imgfn << " cachefn: " << cachefn << std::endl;
  46. filenames.push_back( imgfn );
  47. y[ X.size() ] = classno;
  48. VectorType x;
  49. std::ifstream ifs ( cachefn.c_str(), std::ios::in );
  50. if ( ! ifs.good() )
  51. fthrow(Exception, "File not found: " << cachefn );
  52. ifs >> x;
  53. X.push_back ( x );
  54. ifs.close();
  55. }
  56. }
  57. void readDataAwA ( const NICE::Config & conf, const OBJREC::LabeledSet & ls, std::vector<NICE::Vector> & X, NICE::Vector & y, std::string extension = ".txt" )
  58. {
  59. std::string cacheroot = conf.gS("cache", "root");
  60. y.resize ( ls.count() );
  61. X.clear();
  62. LOOP_ALL_S ( ls )
  63. {
  64. EACH_S(classno, imgfn);
  65. Globals::setCurrentImgFN ( imgfn );
  66. std::string cachefn = Globals::getCacheFilename ( cacheroot, Globals::SORT_CATEGORIES ) + extension;
  67. std::cerr << "fn: " << imgfn << " cachefn: " << cachefn << std::endl;
  68. y[ X.size() ] = classno;
  69. std::ifstream ifs ( cachefn.c_str(), std::ios::in );
  70. if ( ! ifs.good() )
  71. fthrow(Exception, "File not found: " << cachefn );
  72. NICE::Vector x (ifs,true);
  73. // ifs >> x;
  74. X.push_back ( x );
  75. ifs.close();
  76. }
  77. }
  78. #endif