VCDTSVM.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. #endif
  95. #ifndef WRITE
  96. int c2 = ( *counter ) [0];
  97. ( *counter ) [0]++;
  98. FullVector tmp ( results[c2].size() );
  99. for ( int i = 0; i < results[c2].size(); i++ )
  100. {
  101. tmp[i] = results[c2][i];
  102. }
  103. return ClassificationResult ( labels[c2], tmp );
  104. #endif
  105. #if 0
  106. const int buffersize = 65536;
  107. char *buffer = new char [buffersize];
  108. string call = binary + " " + configtest;
  109. FILE *f = popen ( call.c_str(), "r" );
  110. if ( f == NULL )
  111. {
  112. fprintf ( stderr, "VCDTSVM::classify: FATAL ERROR, unable to run the implementation of DTSVM\n" );
  113. exit ( -1 );
  114. }
  115. while ( ! feof ( f ) )
  116. {
  117. if ( fgets ( buffer, buffersize, f ) <= 0 )
  118. {
  119. break;
  120. } else
  121. {
  122. //fprintf (stderr, "VCDTSVM::classify: [INFO] %s", buffer );
  123. }
  124. }
  125. pclose ( f );
  126. #endif
  127. }
  128. void VCDTSVM::teach ( const LabeledSetVector & _teachSet )
  129. {
  130. maxClassNo = _teachSet.getMaxClassno();
  131. //TODO: LabeledSet rausschreiben und classifier anwerfen
  132. #ifdef WRITE
  133. _teachSet.write ( trainfile, LabeledSetVector::FILEFORMAT_INDEX );
  134. /*
  135. string call = binary +" "+configtrain;
  136. const int buffersize = 65536;
  137. char *buffer = new char [buffersize];
  138. FILE *f = popen ( call.c_str(), "r" );
  139. if ( f == NULL )
  140. {
  141. fprintf (stderr, "VCDTSVM::teach: FATAL ERROR, unable to run the implementation of DTSVM\n");
  142. exit(-1);
  143. }
  144. while ( ! feof(f) )
  145. {
  146. if ( fgets ( buffer, buffersize, f ) <= 0 )
  147. {
  148. break;
  149. } else
  150. {
  151. fprintf (stderr, "VCDTSVM::teach: [INFO] %s", buffer );
  152. }
  153. }
  154. pclose(f);*/
  155. #endif
  156. }
  157. void VCDTSVM::finishTeaching()
  158. {
  159. }
  160. /** clone this object */
  161. VCDTSVM *VCDTSVM::clone ( void ) const
  162. {
  163. VCDTSVM *classifier = new VCDTSVM ( *this );
  164. return classifier;
  165. }
  166. void VCDTSVM::clear ()
  167. {
  168. //TODO wieder freigeben
  169. }
  170. void VCDTSVM::read ( const string& s, int format )
  171. {
  172. //TODO: Zielverzeichnis eventuell
  173. }
  174. void VCDTSVM::save ( const string& s, int format ) const
  175. {
  176. //TODO: Zielverzeichnis eventuell
  177. }
  178. void VCDTSVM::store ( std::ostream & os, int format ) const
  179. {
  180. fprintf ( stderr, "VCDTSVM: unable to write to stream! please use read()\n" );
  181. }
  182. void VCDTSVM::restore ( std::istream & is, int format )
  183. {
  184. fprintf ( stderr, "VCDTSVM: unable to read from stream! please use save()\n" );
  185. exit ( -1 );
  186. }