ImageNetData.cpp 4.4 KB

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