VCAmitSVM.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /**
  2. * @file VCAmitSVM.cpp
  3. * @brief interface to the svm generalization of Amit et al. 2007
  4. * @author Erik Rodner
  5. * @date 11/28/2007
  6. */
  7. #ifdef NOVISUAL
  8. #include <vislearning/nice_nonvis.h>
  9. #else
  10. #include <vislearning/nice.h>
  11. #endif
  12. #include <iostream>
  13. #include <fstream>
  14. #include "vislearning/classifier/vclassifier/VCAmitSVM.h"
  15. #include "core/basics/FileMgt.h"
  16. using namespace OBJREC;
  17. using namespace std;
  18. // refactor-nice.pl: check this substitution
  19. // old: using namespace ice;
  20. using namespace NICE;
  21. VCAmitSVM::VCAmitSVM( const Config *conf ) : VCLearnFromSC( conf )
  22. {
  23. amitDir = conf->gS( "amit", "amit_dir", "/home/rodner/osl/labor/cvs/vislearning/vclassifier/amitMatlab/");
  24. matlabExec = conf->gS("matlab", "matlab_exec", "/home/rodner/script/matlab");
  25. matlabArgs = conf->gS("matlab", "matlab_args", "-nosplash -nojvm -nodesktop");
  26. C = conf->gD ("amit", "C", 1.0);
  27. gamma = conf->gD ("amit", "gamma", 1.0);
  28. }
  29. VCAmitSVM::~VCAmitSVM()
  30. {
  31. }
  32. /** classify using simple vector */
  33. // refactor-nice.pl: check this substitution
  34. // old: ClassificationResult VCAmitSVM::classify ( const ice::Vector & x ) const
  35. ClassificationResult VCAmitSVM::classify ( const NICE::Vector & x ) const
  36. {
  37. // refactor-nice.pl: check this substitution
  38. // old: Vector voting = W*x;
  39. NICE::Vector voting;
  40. voting.multiply(W, x);
  41. double maxVoting = - std::numeric_limits<double>::max();
  42. int classno = 0;
  43. for ( size_t i = 0 ; i < voting.size() ; i++ )
  44. {
  45. fprintf (stderr, "class %d, voting %f\n", (int)i, voting[i] );
  46. if ( voting[i] > maxVoting )
  47. {
  48. classno = i;
  49. maxVoting = voting[i];
  50. }
  51. }
  52. return ClassificationResult ( classno, maxVoting, maxClassNo );
  53. }
  54. void VCAmitSVM::preTeach ( const LabeledSetVector & teachSet )
  55. {
  56. fprintf (stderr, "VCAmitSVM::preTeach: WARNING not yet implemented!\n");
  57. }
  58. void VCAmitSVM::teach ( const LabeledSetVector & teachSet )
  59. {
  60. maxClassNo = teachSet.getMaxClassno();
  61. // refactor-nice.pl: check this substitution
  62. // old: string vecFN = FileMgt::createTempFile ("/tmp/amitvec_%s.tmp");
  63. std::string vecFN = FileMgt::createTempFile ("/tmp/amitvec_%s.tmp");
  64. ofstream ofs ( vecFN.c_str(), ios::out );
  65. teachSet.store(ofs);
  66. ofs.close();
  67. // refactor-nice.pl: check this substitution
  68. // old: string execCMD = matlabExec + " " + matlabArgs;
  69. std::string execCMD = matlabExec + " " + matlabArgs;
  70. // refactor-nice.pl: check this substitution
  71. // old: string chdirCMD = "cd '" + amitDir + "'\n";
  72. std::string chdirCMD = "cd '" + amitDir + "'\n";
  73. // ----------- routine argument -------------
  74. // refactor-nice.pl: check this substitution
  75. // old: string matFN = FileMgt::createTempFile ("/tmp/amitmat_%s.tmp");
  76. std::string matFN = FileMgt::createTempFile ("/tmp/amitmat_%s.tmp");
  77. // refactor-nice.pl: check this substitution
  78. // old: ostringstream ss;
  79. std::ostringstream ss;
  80. ss << "W = trainAmit('" << vecFN << "', '" << matFN << "'," << C << "," << gamma << ");" << endl;
  81. // refactor-nice.pl: check this substitution
  82. // old: string routineCMD = ss.str();
  83. std::string routineCMD = ss.str();
  84. // ----------- executing matlab -------------
  85. FILE *f = popen ( execCMD.c_str(), "w");
  86. if ( f == NULL )
  87. {
  88. fprintf (stderr, "VCAmitSVM::teach: FATAL ERROR cannot write labeled set!\n");
  89. exit(-1);
  90. }
  91. fputs (chdirCMD.c_str(), f);
  92. fputs (routineCMD.c_str(), f);
  93. pclose(f);
  94. // ----------- reading result ---------------
  95. FILE *g = fopen ( matFN.c_str(), "r" );
  96. if ( g == NULL )
  97. {
  98. fprintf (stderr, "VCAmitSVM::teach: FATAL ERROR can not read W matrix!\n");
  99. exit(-1);
  100. }
  101. int n = teachSet.numClasses();
  102. int d = teachSet.dimension();
  103. W = Matrix(n, d);
  104. for ( int j = 0 ; j < d ; j++ )
  105. {
  106. for ( int i = 0 ; i < n ; i++ )
  107. {
  108. double val;
  109. if ( fread ( &val, sizeof(double), 1, g) <= 0 )
  110. {
  111. fprintf (stderr, "VCAmitSVM::teach: FATAL ERROR reading matrix file\n");
  112. exit(-1);
  113. }
  114. // refactor-nice.pl: check this substitution
  115. // old: W[i][j] = val;
  116. W(i, j) = val;
  117. }
  118. }
  119. fclose(g);
  120. FileMgt::deleteTempFile ( matFN );
  121. FileMgt::deleteTempFile ( vecFN );
  122. // refactor-nice.pl: check this substitution
  123. // old: fprintf (stderr, "VCAmitSVM::teach Matrix successfully loaded !!\n");
  124. fprintf (stderr, "VCAmitSVM::teach NICE::Matrix successfully loaded !!\n");
  125. }
  126. void VCAmitSVM::restore ( std::istream & is, int format )
  127. {
  128. fprintf (stderr, "NOT YET IMPLEMENTED !!\n");
  129. exit(-1);
  130. }
  131. void VCAmitSVM::store ( std::ostream & is, int format ) const
  132. {
  133. fprintf (stderr, "NOT YET IMPLEMENTED !!\n");
  134. exit(-1);
  135. }
  136. void VCAmitSVM::clear ()
  137. {
  138. fprintf (stderr, "NOT YET IMPLEMENTED !!\n");
  139. exit(-1);
  140. }