VCNormalize.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /**
  2. * @file VCNormalize.cpp
  3. // refactor-nice.pl: check this substitution
  4. // old: * @brief Vector Transform
  5. * @brief NICE::Vector Transform
  6. * @author Michael Koch
  7. * @date 11/28/2007
  8. */
  9. #ifdef NOVISUAL
  10. #include <vislearning/nice_nonvis.h>
  11. #else
  12. #include <vislearning/nice.h>
  13. #endif
  14. #include <iostream>
  15. #include <fstream>
  16. #include "vislearning/classifier/vclassifier/VCNormalize.h"
  17. #include "core/basics/FileMgt.h"
  18. #include "vislearning/math/ftransform/PCA.h"
  19. using namespace OBJREC;
  20. using namespace std;
  21. using namespace NICE;
  22. VCNormalize::VCNormalize(const Config *conf, VecClassifier * classifier)
  23. {
  24. this->classifier = classifier;
  25. this->mode = conf->gI("FTransform", "mode", 0);
  26. transformedSet.clear();
  27. }
  28. VCNormalize::~VCNormalize()
  29. {
  30. }
  31. /** classify using simple vector */
  32. ClassificationResult VCNormalize::classify(const NICE::Vector & x) const
  33. {
  34. NICE::Vector transformed_vector = getNormalizedVector(x);
  35. cerr << "VCNormalize: transformed-vector: " << transformed_vector << endl;
  36. return this->classifier->classify(transformed_vector);
  37. }
  38. void VCNormalize::teach(const LabeledSetVector & teachSet)
  39. {
  40. maxClassNo = teachSet.getMaxClassno();
  41. int n = teachSet.count();
  42. int d = teachSet.dimension();
  43. vector_max.resize(d);
  44. vector_min.resize(d);
  45. vector_span.resize(d);
  46. //get input data
  47. uint featurecount = 0;
  48. LOOP_ALL(teachSet)
  49. {
  50. EACH(classno,x);
  51. for (uint k = 0; k < x.size(); ++k)
  52. {
  53. double value = x[k];
  54. if (featurecount == 0)
  55. {
  56. vector_max[k] = value;
  57. vector_min[k] = value;
  58. }
  59. else
  60. {
  61. if (value > vector_max[k])
  62. {
  63. vector_max[k] = value;
  64. }
  65. if (value < vector_min[k])
  66. {
  67. vector_min[k] = value;
  68. }
  69. }
  70. }
  71. ++featurecount;
  72. }
  73. vector_span = vector_max - vector_min;
  74. //save transformed Vectors
  75. LOOP_ALL(teachSet)
  76. {
  77. EACH(classno,x);
  78. NICE::Vector transformed_vector=getNormalizedVector(x);
  79. cerr << "VCNormalize: transformed-vector: " << transformed_vector
  80. << endl;
  81. transformedSet.add(classno, transformed_vector);
  82. }
  83. this->classifier->teach(transformedSet);
  84. }
  85. NICE::Vector VCNormalize::getNormalizedVector(const NICE::Vector &x) const
  86. {
  87. NICE::Vector transformed_vector(x.size());
  88. for (uint k = 0; k < vector_min.size(); ++k)
  89. {
  90. if (vector_span[k] > 1e-10)
  91. {
  92. transformed_vector[k] = (x[k] - vector_min[k]) / vector_span[k];
  93. }
  94. else
  95. {
  96. transformed_vector[k] = 1.0;
  97. }
  98. }
  99. return transformed_vector;
  100. }
  101. void VCNormalize::finishTeaching()
  102. {
  103. this->classifier->finishTeaching();
  104. }
  105. void VCNormalize::restore(std::istream & is, int format)
  106. {
  107. fprintf(stderr, "NOT YET IMPLEMENTED !!\n");
  108. exit(-1);
  109. }
  110. void VCNormalize::store(std::ostream & is, int format) const
  111. {
  112. fprintf(stderr, "NOT YET IMPLEMENTED !!\n");
  113. exit(-1);
  114. }
  115. void VCNormalize::clear()
  116. {
  117. fprintf(stderr, "NOT YET IMPLEMENTED !!\n");
  118. exit(-1);
  119. }