ImageNetData.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /**
  2. * @file ImageNetData.cpp
  3. * @brief wrapper class for matlab IO with ImageNet data
  4. * @author Erik Rodner
  5. * @date 02/03/2012
  6. */
  7. #include <iostream>
  8. #include <vector>
  9. #include <core/basics/Exception.h>
  10. #include <core/vector/VectorT.h>
  11. #include "ImageNetData.h"
  12. using namespace NICE;
  13. using namespace std;
  14. ImageNetData::ImageNetData( const string & imageNetRoot )
  15. {
  16. this->imageNetRoot = imageNetRoot;
  17. }
  18. ImageNetData::~ImageNetData()
  19. {
  20. }
  21. void ImageNetData::getBatchData ( sparse_t & data, Vector & y, const string & fileTag, const string & variableTag )
  22. {
  23. string filename = imageNetRoot + "/demo." + fileTag + ".mat";
  24. string vn_data = variableTag + "_instance_matrix";
  25. string vn_y = variableTag + "_label_vector";
  26. MatFileIO matfileIO (filename,MAT_ACC_RDONLY);
  27. matfileIO.getSparseVariableViaName(data,vn_data);
  28. matfileIO.getVectorViaName(y,vn_y);
  29. }
  30. void ImageNetData::preloadData ( const string & fileTag, const string & variableTag )
  31. {
  32. sparse_t m_XPreload;
  33. getBatchData ( m_XPreload, yPreload, fileTag, variableTag );
  34. XPreload.resize ( yPreload.size() );
  35. cerr << "ImageNetData: converting data ... " << yPreload.size() << " examples" << endl;
  36. for ( int i = 0; i < m_XPreload.njc-1; i++ ) //walk over dimensions
  37. {
  38. for ( int j = m_XPreload.jc[i]; j < m_XPreload.jc[i+1] && j < m_XPreload.ndata; j++ )
  39. {
  40. int exampleIndex = m_XPreload.ir[ j];
  41. if ( exampleIndex < 0 || exampleIndex >= (int)XPreload.size() )
  42. fthrow(Exception, "Label and data file seem to mismatch according the sizes: " << XPreload.size() << " vs. " << exampleIndex);
  43. XPreload[exampleIndex].insert ( pair<int, double> ( i, ((double *)m_XPreload.data)[j] ) );
  44. }
  45. }
  46. cerr << "ImageNetData: data conversion finished." << endl;
  47. }
  48. void ImageNetData::loadDataAsLabeledSetVector( OBJREC::LabeledSetVector & lsVector, const std::string & fileTag, const std::string & variableTag )
  49. {
  50. sparse_t m_XPreload;
  51. //load raw data
  52. getBatchData ( m_XPreload, yPreload, fileTag, variableTag );
  53. //tmp storage
  54. std::vector<NICE::Vector> dataTmp;
  55. dataTmp.resize(yPreload.size());
  56. //initialize every entries with zero
  57. NICE::Vector vZero (yPreload.size(), 0.0);
  58. for (uint i = 0; i < yPreload.size(); i++)
  59. {
  60. dataTmp[i] = vZero;
  61. }
  62. //set non-zero entries according to the stored values
  63. std::cerr << "ImageNetData: converting data ... " << yPreload.size() << " examples" << std::endl;
  64. for ( int i = 0; i < m_XPreload.njc-1; i++ ) //walk over dimensions
  65. {
  66. for ( int j = m_XPreload.jc[i]; j < m_XPreload.jc[i+1] && j < m_XPreload.ndata; j++ ) //and over every non-zero entry in this dimension
  67. {
  68. //what is the original index?
  69. int exampleIndex = m_XPreload.ir[ j];
  70. if ( exampleIndex < 0 || exampleIndex >= (int)yPreload.size() )
  71. fthrow(Exception, "Label and data file seem to mismatch according the sizes: " << yPreload.size() << " vs. " << exampleIndex);
  72. //insert at the original index and the corresponding dimension
  73. dataTmp[exampleIndex][i] = ((double *)m_XPreload.data)[j];
  74. }
  75. }
  76. std::cerr << "ImageNetData: data conversion finished." << std::endl;
  77. lsVector.clear();
  78. for (uint i = 0; i < yPreload.size(); i++)
  79. {
  80. lsVector.add( yPreload[i], dataTmp[i] );
  81. }
  82. }
  83. const SparseVector & ImageNetData::getPreloadedExample ( int index ) const
  84. {
  85. if ( index >= (int)XPreload.size() || index < 0 )
  86. fthrow(Exception, "Invalid index!");
  87. return XPreload[index];
  88. }
  89. double ImageNetData::getPreloadedLabel ( int index ) const
  90. {
  91. if ( index < 0 || index >= (int)yPreload.size() )
  92. fthrow(Exception, "Invalid index!");
  93. return yPreload[index];
  94. }
  95. int ImageNetData::getNumPreloadedExamples () const
  96. {
  97. return yPreload.size();
  98. }
  99. void ImageNetData::loadExternalLabels ( const string & fn, int n )
  100. {
  101. if ( n <= 0 && yPreload.size() == 0 ) {
  102. fthrow(Exception, "Please initialize with preloadData() first, or use the second optional argument to give the number of examples.");
  103. }
  104. if ( n >= 0 )
  105. yPreload.resize( n );
  106. ifstream ifs ( fn.c_str(), ios::in );
  107. if ( ! ifs.good() )
  108. fthrow(Exception, "Unable to read " << fn );
  109. int value;
  110. int i = 0;
  111. while ( (i < yPreload.size()) && (ifs >> value) )
  112. yPreload[i++] = value;
  113. ifs.close();
  114. if ( (XPreload.size() > 0) && (yPreload.size() != XPreload.size()) )
  115. fthrow(Exception, "Size of the label vector and the size of the data structure do not match.");
  116. }