VCDTSVM.cpp 4.6 KB

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