VCDTSVM.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. #include <iostream>
  2. #include "vislearning/classifier/vclassifier/VCDTSVM.h"
  3. #include "core/image/ImageT.h"
  4. //#include "core/imagedisplay/ImageDisplay.h"
  5. #include "core/basics/StringTools.h"
  6. #undef WRITE
  7. using namespace OBJREC;
  8. using namespace std;
  9. using namespace NICE;
  10. VCDTSVM::VCDTSVM ( const Config *_conf, const string & section )
  11. : VecClassifier ( _conf )
  12. {
  13. binary = _conf->gS ( section, "binary", "./tdsvm" );
  14. configtrain = _conf->gS ( section, "configtrain", "configtrain.txt" );
  15. configtest = _conf->gS ( section, "configtest", "configtest.txt" );
  16. trainfile = _conf->gS ( section, "traindst", "train.txt" );
  17. testfile = _conf->gS ( section, "testdst", "test.txt" );
  18. #ifndef WRITE
  19. counter = new vector<int> ( 1, 0 );
  20. if ( results.size() == 0 )
  21. {
  22. ifstream fin ( "results.txt" );
  23. const int bufsize = 1024 * 1024;
  24. char *buf = new char[bufsize];
  25. std::string buf_s;
  26. while ( !fin.eof() )
  27. {
  28. int bc;
  29. vector<string> elements;
  30. fin >> bc;
  31. fin.get ( buf, bufsize );
  32. buf_s = buf;
  33. if ( buf_s.size() <= 0 )
  34. break;
  35. StringTools::split ( buf_s, ' ', elements );
  36. if ( elements.size() <= 1 )
  37. break;
  38. vector<double> tmp;
  39. size_t l = 0;
  40. // skip first element because of white space
  41. for ( vector<string>::const_iterator i = elements.begin() + 1; i != elements.end(); i++, l++ )
  42. {
  43. double val = atof ( i->c_str() );
  44. tmp.push_back ( val );
  45. }
  46. results.push_back ( tmp );
  47. labels.push_back ( bc );
  48. }
  49. fin.close();
  50. ( *counter ) [0] = 0;
  51. //counter = 0;
  52. }
  53. #endif
  54. }
  55. VCDTSVM::VCDTSVM ( const VCDTSVM & classifier ) : VecClassifier()
  56. {
  57. binary = classifier.binary;
  58. configtrain = classifier.configtrain;
  59. configtest = classifier.configtest;
  60. trainfile = classifier.trainfile;
  61. testfile = classifier.testfile;
  62. }
  63. VCDTSVM::~VCDTSVM()
  64. {
  65. int c2 = ( *counter ) [0];
  66. cout << "c2: " << c2 << " matsize " << results.size() << endl;
  67. }
  68. ClassificationResult VCDTSVM::classify ( const NICE::Vector & x ) const
  69. {
  70. #ifdef WRITE
  71. ofstream fout ( testfile.c_str(), ios::app );
  72. fout << 0 << " ";
  73. int i;
  74. for ( i = 0; i < x.size() - 1; i++ )
  75. {
  76. fout << i + 1 << ":" << x[i] << " ";
  77. }
  78. i++;
  79. fout << i + 1 << ":" << x[i] << endl;
  80. fout.close();
  81. FullVector scores ( 10 );
  82. //scores.read("scores");
  83. double bval = scores[0];
  84. int bclass = 0;
  85. for ( i = 1; i < scores.size(); i++ )
  86. {
  87. if ( bval < scores[i] )
  88. {
  89. bval = scores[i];
  90. bclass = i;
  91. }
  92. }
  93. return ClassificationResult ( bclass, scores );
  94. #else
  95. int c2 = ( *counter ) [0];
  96. ( *counter ) [0]++;
  97. FullVector tmp ( results[c2].size() );
  98. for ( int i = 0; i < results[c2].size(); i++ )
  99. {
  100. tmp[i] = results[c2][i];
  101. }
  102. return ClassificationResult ( labels[c2], tmp );
  103. #endif
  104. #if 0
  105. const int buffersize = 65536;
  106. char *buffer = new char [buffersize];
  107. string call = binary + " " + configtest;
  108. FILE *f = popen ( call.c_str(), "r" );
  109. if ( f == NULL )
  110. {
  111. fprintf ( stderr, "VCDTSVM::classify: FATAL ERROR, unable to run the implementation of DTSVM\n" );
  112. exit ( -1 );
  113. }
  114. while ( ! feof ( f ) )
  115. {
  116. if ( fgets ( buffer, buffersize, f ) <= 0 )
  117. {
  118. break;
  119. } else
  120. {
  121. //fprintf (stderr, "VCDTSVM::classify: [INFO] %s", buffer );
  122. }
  123. }
  124. pclose ( f );
  125. #endif
  126. }
  127. void VCDTSVM::teach ( const LabeledSetVector & _teachSet )
  128. {
  129. maxClassNo = _teachSet.getMaxClassno();
  130. //TODO: LabeledSet rausschreiben und classifier anwerfen
  131. #ifdef WRITE
  132. _teachSet.write ( trainfile, LabeledSetVector::FILEFORMAT_INDEX );
  133. /*
  134. string call = binary +" "+configtrain;
  135. const int buffersize = 65536;
  136. char *buffer = new char [buffersize];
  137. FILE *f = popen ( call.c_str(), "r" );
  138. if ( f == NULL )
  139. {
  140. fprintf (stderr, "VCDTSVM::teach: FATAL ERROR, unable to run the implementation of DTSVM\n");
  141. exit(-1);
  142. }
  143. while ( ! feof(f) )
  144. {
  145. if ( fgets ( buffer, buffersize, f ) <= 0 )
  146. {
  147. break;
  148. } else
  149. {
  150. fprintf (stderr, "VCDTSVM::teach: [INFO] %s", buffer );
  151. }
  152. }
  153. pclose(f);*/
  154. #endif
  155. }
  156. void VCDTSVM::finishTeaching()
  157. {
  158. }
  159. /** clone this object */
  160. VCDTSVM *VCDTSVM::clone ( void ) const
  161. {
  162. VCDTSVM *classifier = new VCDTSVM ( *this );
  163. return classifier;
  164. }
  165. void VCDTSVM::clear ()
  166. {
  167. //TODO wieder freigeben
  168. }
  169. void VCDTSVM::read ( const string& s, int format )
  170. {
  171. //TODO: Zielverzeichnis eventuell
  172. }
  173. void VCDTSVM::save ( const string& s, int format ) const
  174. {
  175. //TODO: Zielverzeichnis eventuell
  176. }
  177. void VCDTSVM::store ( std::ostream & os, int format ) const
  178. {
  179. fprintf ( stderr, "VCDTSVM: unable to write to stream! please use read()\n" );
  180. }
  181. void VCDTSVM::restore ( std::istream & is, int format )
  182. {
  183. fprintf ( stderr, "VCDTSVM: unable to read from stream! please use save()\n" );
  184. exit ( -1 );
  185. }