ImageNetData.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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::normalizeData ( const string & normTag )
  50. {
  51. if ( normTag.compare("L1") == 0 )
  52. {
  53. for ( std::vector< SparseVector >::iterator it = XPreload.begin(); it != XPreload.end(); it++ )
  54. {
  55. it->normalize();
  56. }
  57. return;
  58. }
  59. if ( normTag.compare("L2") == 0 )
  60. {
  61. double L2norm(0.0);
  62. NICE::SparseVector tmpVec;
  63. for ( std::vector< SparseVector >::iterator it = XPreload.begin(); it != XPreload.end(); it++ )
  64. {
  65. tmpVec = *it;
  66. tmpVec.multiply(*it);
  67. L2norm = tmpVec.sum();
  68. it->divide(L2norm);
  69. }
  70. return;
  71. }
  72. cerr << "ImageNetData::normalizeData: invalid normTag... data was not normalized" << endl;
  73. }
  74. void ImageNetData::loadDataAsLabeledSetVector( OBJREC::LabeledSetVector & lsVector, const std::string & fileTag, const std::string & variableTag )
  75. {
  76. sparse_t m_XPreload;
  77. //load raw data
  78. getBatchData ( m_XPreload, yPreload, fileTag, variableTag );
  79. //tmp storage
  80. std::vector<NICE::Vector> dataTmp;
  81. dataTmp.resize(yPreload.size());
  82. //initialize every entries with zero
  83. NICE::Vector vZero (yPreload.size(), 0.0);
  84. for (uint i = 0; i < yPreload.size(); i++)
  85. {
  86. dataTmp[i] = vZero;
  87. }
  88. //set non-zero entries according to the stored values
  89. std::cerr << "ImageNetData: converting data ... " << yPreload.size() << " examples" << std::endl;
  90. for ( int i = 0; i < m_XPreload.njc-1; i++ ) //walk over dimensions
  91. {
  92. 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
  93. {
  94. //what is the original index?
  95. int exampleIndex = m_XPreload.ir[ j];
  96. if ( exampleIndex < 0 || exampleIndex >= (int)yPreload.size() )
  97. fthrow(Exception, "Label and data file seem to mismatch according the sizes: " << yPreload.size() << " vs. " << exampleIndex);
  98. //insert at the original index and the corresponding dimension
  99. dataTmp[exampleIndex][i] = ((double *)m_XPreload.data)[j];
  100. }
  101. }
  102. std::cerr << "ImageNetData: data conversion finished." << std::endl;
  103. lsVector.clear();
  104. for (uint i = 0; i < yPreload.size(); i++)
  105. {
  106. lsVector.add( yPreload[i], dataTmp[i] );
  107. }
  108. }
  109. const SparseVector & ImageNetData::getPreloadedExample ( int index ) const
  110. {
  111. if ( index >= (int)XPreload.size() || index < 0 )
  112. fthrow(Exception, "Invalid index!");
  113. return XPreload[index];
  114. }
  115. double ImageNetData::getPreloadedLabel ( int index ) const
  116. {
  117. if ( index < 0 || index >= (int)yPreload.size() )
  118. fthrow(Exception, "Invalid index!");
  119. return yPreload[index];
  120. }
  121. int ImageNetData::getNumPreloadedExamples () const
  122. {
  123. return yPreload.size();
  124. }
  125. void ImageNetData::loadExternalLabels ( const string & fn, int n )
  126. {
  127. if ( n <= 0 && yPreload.size() == 0 ) {
  128. fthrow(Exception, "Please initialize with preloadData() first, or use the second optional argument to give the number of examples.");
  129. }
  130. if ( n >= 0 )
  131. yPreload.resize( n );
  132. ifstream ifs ( fn.c_str(), ios::in );
  133. if ( ! ifs.good() )
  134. fthrow(Exception, "Unable to read " << fn );
  135. int value;
  136. int i = 0;
  137. while ( (i < yPreload.size()) && (ifs >> value) )
  138. yPreload[i++] = value;
  139. ifs.close();
  140. if ( (XPreload.size() > 0) && (yPreload.size() != XPreload.size()) )
  141. fthrow(Exception, "Size of the label vector and the size of the data structure do not match.");
  142. }
  143. #endif