VCCrossGeneralization.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /**
  2. * @file VCCrossGeneralization.cpp
  3. * @brief Combination of Classifiers
  4. * @author Erik Rodner
  5. * @date 12/05/2007
  6. */
  7. #include <iostream>
  8. #include "vislearning/classifier/vclassifier/VCCrossGeneralization.h"
  9. #ifdef NICE_USELIB_ICE
  10. using namespace OBJREC;
  11. using namespace std;
  12. using namespace NICE;
  13. VCCrossGeneralization::VCCrossGeneralization( const Config *conf )
  14. : VCLearnFromSC(conf),
  15. gauss(conf), nnclassifier( conf, NULL )
  16. {
  17. simpleGaussianFinished = false;
  18. useVotingNormalization = conf->gB("CrossGeneralization", "use_voting_normalization", true );
  19. }
  20. VCCrossGeneralization::~VCCrossGeneralization()
  21. {
  22. }
  23. void VCCrossGeneralization::normalizeVotings ( NICE::Vector & v ) const
  24. {
  25. double sum = 0.0;
  26. for ( size_t i = 0 ; i < v.size() ; i++ )
  27. {
  28. if ( fabs(v[i]) < 10e-7 )
  29. v[i] = 10e7;
  30. else
  31. v[i] = 1.0 / v[i];
  32. sum += v[i];
  33. }
  34. if ( fabs(sum) > 10e-5 )
  35. for ( size_t i = 0 ; i < v.size() ; i++ )
  36. v[i] /= sum;
  37. }
  38. /** classify using simple vector */
  39. ClassificationResult VCCrossGeneralization::classify ( const NICE::Vector & x ) const
  40. {
  41. NICE::Vector votings_vec;
  42. std::map<int, double> votings;
  43. gauss.getVotings(x, votings);
  44. for ( map<int, double>::const_iterator j = votings.begin();
  45. j != votings.end();
  46. j++ )
  47. {
  48. votings_vec.append(j->second);
  49. }
  50. if ( useVotingNormalization )
  51. normalizeVotings( votings_vec );
  52. return nnclassifier.classify(votings_vec);
  53. }
  54. void VCCrossGeneralization::preTeach ( const LabeledSetVector & teachSet )
  55. {
  56. gauss.teach(teachSet);
  57. }
  58. void VCCrossGeneralization::teach ( const LabeledSetVector & teachSet )
  59. {
  60. maxClassNo = teachSet.getMaxClassno();
  61. if ( ! simpleGaussianFinished )
  62. {
  63. gauss.finishTeaching();
  64. simpleGaussianFinished = true;
  65. }
  66. LOOP_ALL(teachSet)
  67. {
  68. EACH(classno,x);
  69. NICE::Vector votings_vec;
  70. std::map<int, double> votings;
  71. gauss.getVotings(x, votings);
  72. for ( map<int, double>::const_iterator j = votings.begin();
  73. j != votings.end();
  74. j++ )
  75. {
  76. votings_vec.append(j->second);
  77. }
  78. normalizeVotings( votings_vec );
  79. //cerr << votings_vec << endl;
  80. nnclassifier.teach( classno, votings_vec );
  81. }
  82. }
  83. void VCCrossGeneralization::finishTeaching()
  84. {
  85. gauss.finishTeaching();
  86. }
  87. void VCCrossGeneralization::restore ( std::istream & is, int format )
  88. {
  89. fprintf (stderr, "NOT YET IMPLEMENTED !!\n");
  90. exit(-1);
  91. }
  92. void VCCrossGeneralization::store ( std::ostream & is, int format ) const
  93. {
  94. fprintf (stderr, "NOT YET IMPLEMENTED !!\n");
  95. exit(-1);
  96. }
  97. void VCCrossGeneralization::clear ()
  98. {
  99. fprintf (stderr, "NOT YET IMPLEMENTED !!\n");
  100. exit(-1);
  101. }
  102. #endif