VCAmitSVM.cpp 4.6 KB

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