123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231 |
- #include <iostream>
- #include "vislearning/classifier/vclassifier/VCDTSVM.h"
- #include "core/image/ImageT.h"
- //#include "core/imagedisplay/ImageDisplay.h"
- #include "core/basics/StringTools.h"
- #undef WRITE
- using namespace OBJREC;
- using namespace std;
- using namespace NICE;
- VCDTSVM::VCDTSVM ( const Config *_conf, const string & section )
- : VecClassifier ( _conf )
- {
- binary = _conf->gS ( section, "binary", "./tdsvm" );
- configtrain = _conf->gS ( section, "configtrain", "configtrain.txt" );
- configtest = _conf->gS ( section, "configtest", "configtest.txt" );
- trainfile = _conf->gS ( section, "traindst", "train.txt" );
- testfile = _conf->gS ( section, "testdst", "test.txt" );
- #ifndef WRITE
- counter = new vector<int> ( 1, 0 );
- if ( results.size() == 0 )
- {
- ifstream fin ( "results.txt" );
- const int bufsize = 1024 * 1024;
- char *buf = new char[bufsize];
- std::string buf_s;
- while ( !fin.eof() )
- {
- int bc;
- vector<string> elements;
- fin >> bc;
- fin.get ( buf, bufsize );
- buf_s = buf;
- if ( buf_s.size() <= 0 )
- break;
- StringTools::split ( buf_s, ' ', elements );
- if ( elements.size() <= 1 )
- break;
- vector<double> tmp;
- size_t l = 0;
- // skip first element because of white space
- for ( vector<string>::const_iterator i = elements.begin() + 1; i != elements.end(); i++, l++ )
- {
- double val = atof ( i->c_str() );
- tmp.push_back ( val );
- }
- results.push_back ( tmp );
- labels.push_back ( bc );
- }
- fin.close();
- ( *counter ) [0] = 0;
- //counter = 0;
- }
- #endif
- }
- VCDTSVM::VCDTSVM ( const VCDTSVM & classifier ) : VecClassifier()
- {
- binary = classifier.binary;
- configtrain = classifier.configtrain;
- configtest = classifier.configtest;
- trainfile = classifier.trainfile;
- testfile = classifier.testfile;
- }
- VCDTSVM::~VCDTSVM()
- {
- int c2 = ( *counter ) [0];
- cout << "c2: " << c2 << " matsize " << results.size() << endl;
- }
- ClassificationResult VCDTSVM::classify ( const NICE::Vector & x ) const
- {
- #ifdef WRITE
- ofstream fout ( testfile.c_str(), ios::app );
- fout << 0 << " ";
- int i;
- for ( i = 0; i < x.size() - 1; i++ )
- {
- fout << i + 1 << ":" << x[i] << " ";
- }
- i++;
- fout << i + 1 << ":" << x[i] << endl;
- fout.close();
- FullVector scores ( 10 );
- //scores.read("scores");
- double bval = scores[0];
- int bclass = 0;
- for ( i = 1; i < scores.size(); i++ )
- {
- if ( bval < scores[i] )
- {
- bval = scores[i];
- bclass = i;
- }
- }
- return ClassificationResult ( bclass, scores );
- #else
- int c2 = ( *counter ) [0];
- ( *counter ) [0]++;
- FullVector tmp ( results[c2].size() );
- for ( int i = 0; i < results[c2].size(); i++ )
- {
- tmp[i] = results[c2][i];
- }
- return ClassificationResult ( labels[c2], tmp );
- #endif
- #if 0
- const int buffersize = 65536;
- char *buffer = new char [buffersize];
- string call = binary + " " + configtest;
- FILE *f = popen ( call.c_str(), "r" );
- if ( f == NULL )
- {
- fprintf ( stderr, "VCDTSVM::classify: FATAL ERROR, unable to run the implementation of DTSVM\n" );
- exit ( -1 );
- }
- while ( ! feof ( f ) )
- {
- if ( fgets ( buffer, buffersize, f ) <= 0 )
- {
- break;
- } else
- {
- //fprintf (stderr, "VCDTSVM::classify: [INFO] %s", buffer );
- }
- }
- pclose ( f );
- #endif
- }
- void VCDTSVM::teach ( const LabeledSetVector & _teachSet )
- {
- maxClassNo = _teachSet.getMaxClassno();
- //TODO: LabeledSet rausschreiben und classifier anwerfen
- #ifdef WRITE
- _teachSet.write ( trainfile, LabeledSetVector::FILEFORMAT_INDEX );
- /*
- string call = binary +" "+configtrain;
- const int buffersize = 65536;
- char *buffer = new char [buffersize];
- FILE *f = popen ( call.c_str(), "r" );
- if ( f == NULL )
- {
- fprintf (stderr, "VCDTSVM::teach: FATAL ERROR, unable to run the implementation of DTSVM\n");
- exit(-1);
- }
- while ( ! feof(f) )
- {
- if ( fgets ( buffer, buffersize, f ) <= 0 )
- {
- break;
- } else
- {
- fprintf (stderr, "VCDTSVM::teach: [INFO] %s", buffer );
- }
- }
- pclose(f);*/
- #endif
- }
- void VCDTSVM::finishTeaching()
- {
- }
- /** clone this object */
- VCDTSVM *VCDTSVM::clone ( void ) const
- {
- VCDTSVM *classifier = new VCDTSVM ( *this );
- return classifier;
- }
- void VCDTSVM::clear ()
- {
- //TODO wieder freigeben
- }
- void VCDTSVM::read ( const string& s, int format )
- {
- //TODO: Zielverzeichnis eventuell
- }
- void VCDTSVM::save ( const string& s, int format ) const
- {
- //TODO: Zielverzeichnis eventuell
- }
- void VCDTSVM::store ( std::ostream & os, int format ) const
- {
- fprintf ( stderr, "VCDTSVM: unable to write to stream! please use read()\n" );
- }
- void VCDTSVM::restore ( std::istream & is, int format )
- {
- fprintf ( stderr, "VCDTSVM: unable to read from stream! please use save()\n" );
- exit ( -1 );
- }
|