ScoringObject.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /**
  2. * File: ScoringObject.cpp
  3. * Date: November 2011
  4. * Author: Dorian Galvez-Lopez
  5. * Description: functions to compute bow scores
  6. * License: see the LICENSE.txt file
  7. *
  8. */
  9. #include <cfloat>
  10. #include "TemplatedVocabulary.h"
  11. #include "BowVector.h"
  12. using namespace DBoW2;
  13. // If you change the type of WordValue, make sure you change also the
  14. // epsilon value (this is needed by the KL method)
  15. const double GeneralScoring::LOG_EPS = log(DBL_EPSILON); // FLT_EPSILON
  16. // ---------------------------------------------------------------------------
  17. // ---------------------------------------------------------------------------
  18. double L1Scoring::score(const BowVector &v1, const BowVector &v2) const
  19. {
  20. BowVector::const_iterator v1_it, v2_it;
  21. const BowVector::const_iterator v1_end = v1.end();
  22. const BowVector::const_iterator v2_end = v2.end();
  23. v1_it = v1.begin();
  24. v2_it = v2.begin();
  25. double score = 0;
  26. while(v1_it != v1_end && v2_it != v2_end)
  27. {
  28. const WordValue& vi = v1_it->second;
  29. const WordValue& wi = v2_it->second;
  30. if(v1_it->first == v2_it->first)
  31. {
  32. score += fabs(vi - wi) - fabs(vi) - fabs(wi);
  33. // move v1 and v2 forward
  34. ++v1_it;
  35. ++v2_it;
  36. }
  37. else if(v1_it->first < v2_it->first)
  38. {
  39. // move v1 forward
  40. v1_it = v1.lower_bound(v2_it->first);
  41. // v1_it = (first element >= v2_it.id)
  42. }
  43. else
  44. {
  45. // move v2 forward
  46. v2_it = v2.lower_bound(v1_it->first);
  47. // v2_it = (first element >= v1_it.id)
  48. }
  49. }
  50. // ||v - w||_{L1} = 2 + Sum(|v_i - w_i| - |v_i| - |w_i|)
  51. // for all i | v_i != 0 and w_i != 0
  52. // (Nister, 2006)
  53. // scaled_||v - w||_{L1} = 1 - 0.5 * ||v - w||_{L1}
  54. score = -score/2.0;
  55. return score; // [0..1]
  56. }
  57. // ---------------------------------------------------------------------------
  58. // ---------------------------------------------------------------------------
  59. double L2Scoring::score(const BowVector &v1, const BowVector &v2) const
  60. {
  61. BowVector::const_iterator v1_it, v2_it;
  62. const BowVector::const_iterator v1_end = v1.end();
  63. const BowVector::const_iterator v2_end = v2.end();
  64. v1_it = v1.begin();
  65. v2_it = v2.begin();
  66. double score = 0;
  67. while(v1_it != v1_end && v2_it != v2_end)
  68. {
  69. const WordValue& vi = v1_it->second;
  70. const WordValue& wi = v2_it->second;
  71. if(v1_it->first == v2_it->first)
  72. {
  73. score += vi * wi;
  74. // move v1 and v2 forward
  75. ++v1_it;
  76. ++v2_it;
  77. }
  78. else if(v1_it->first < v2_it->first)
  79. {
  80. // move v1 forward
  81. v1_it = v1.lower_bound(v2_it->first);
  82. // v1_it = (first element >= v2_it.id)
  83. }
  84. else
  85. {
  86. // move v2 forward
  87. v2_it = v2.lower_bound(v1_it->first);
  88. // v2_it = (first element >= v1_it.id)
  89. }
  90. }
  91. // ||v - w||_{L2} = sqrt( 2 - 2 * Sum(v_i * w_i) )
  92. // for all i | v_i != 0 and w_i != 0 )
  93. // (Nister, 2006)
  94. if(score >= 1) // rounding errors
  95. score = 1.0;
  96. else
  97. score = 1.0 - sqrt(1.0 - score); // [0..1]
  98. return score;
  99. }
  100. // ---------------------------------------------------------------------------
  101. // ---------------------------------------------------------------------------
  102. double ChiSquareScoring::score(const BowVector &v1, const BowVector &v2)
  103. const
  104. {
  105. BowVector::const_iterator v1_it, v2_it;
  106. const BowVector::const_iterator v1_end = v1.end();
  107. const BowVector::const_iterator v2_end = v2.end();
  108. v1_it = v1.begin();
  109. v2_it = v2.begin();
  110. double score = 0;
  111. // all the items are taken into account
  112. while(v1_it != v1_end && v2_it != v2_end)
  113. {
  114. const WordValue& vi = v1_it->second;
  115. const WordValue& wi = v2_it->second;
  116. if(v1_it->first == v2_it->first)
  117. {
  118. // (v-w)^2/(v+w) - v - w = -4 vw/(v+w)
  119. // we move the -4 out
  120. if(vi + wi != 0.0) score += vi * wi / (vi + wi);
  121. // move v1 and v2 forward
  122. ++v1_it;
  123. ++v2_it;
  124. }
  125. else if(v1_it->first < v2_it->first)
  126. {
  127. // move v1 forward
  128. v1_it = v1.lower_bound(v2_it->first);
  129. }
  130. else
  131. {
  132. // move v2 forward
  133. v2_it = v2.lower_bound(v1_it->first);
  134. }
  135. }
  136. // this takes the -4 into account
  137. score = 2. * score; // [0..1]
  138. return score;
  139. }
  140. // ---------------------------------------------------------------------------
  141. // ---------------------------------------------------------------------------
  142. double KLScoring::score(const BowVector &v1, const BowVector &v2) const
  143. {
  144. BowVector::const_iterator v1_it, v2_it;
  145. const BowVector::const_iterator v1_end = v1.end();
  146. const BowVector::const_iterator v2_end = v2.end();
  147. v1_it = v1.begin();
  148. v2_it = v2.begin();
  149. double score = 0;
  150. // all the items or v are taken into account
  151. while(v1_it != v1_end && v2_it != v2_end)
  152. {
  153. const WordValue& vi = v1_it->second;
  154. const WordValue& wi = v2_it->second;
  155. if(v1_it->first == v2_it->first)
  156. {
  157. if(vi != 0 && wi != 0) score += vi * log(vi/wi);
  158. // move v1 and v2 forward
  159. ++v1_it;
  160. ++v2_it;
  161. }
  162. else if(v1_it->first < v2_it->first)
  163. {
  164. // move v1 forward
  165. score += vi * (log(vi) - LOG_EPS);
  166. ++v1_it;
  167. }
  168. else
  169. {
  170. // move v2_it forward, do not add any score
  171. v2_it = v2.lower_bound(v1_it->first);
  172. // v2_it = (first element >= v1_it.id)
  173. }
  174. }
  175. // sum rest of items of v
  176. for(; v1_it != v1_end; ++v1_it)
  177. if(v1_it->second != 0)
  178. score += v1_it->second * (log(v1_it->second) - LOG_EPS);
  179. return score; // cannot be scaled
  180. }
  181. // ---------------------------------------------------------------------------
  182. // ---------------------------------------------------------------------------
  183. double BhattacharyyaScoring::score(const BowVector &v1,
  184. const BowVector &v2) const
  185. {
  186. BowVector::const_iterator v1_it, v2_it;
  187. const BowVector::const_iterator v1_end = v1.end();
  188. const BowVector::const_iterator v2_end = v2.end();
  189. v1_it = v1.begin();
  190. v2_it = v2.begin();
  191. double score = 0;
  192. while(v1_it != v1_end && v2_it != v2_end)
  193. {
  194. const WordValue& vi = v1_it->second;
  195. const WordValue& wi = v2_it->second;
  196. if(v1_it->first == v2_it->first)
  197. {
  198. score += sqrt(vi * wi);
  199. // move v1 and v2 forward
  200. ++v1_it;
  201. ++v2_it;
  202. }
  203. else if(v1_it->first < v2_it->first)
  204. {
  205. // move v1 forward
  206. v1_it = v1.lower_bound(v2_it->first);
  207. // v1_it = (first element >= v2_it.id)
  208. }
  209. else
  210. {
  211. // move v2 forward
  212. v2_it = v2.lower_bound(v1_it->first);
  213. // v2_it = (first element >= v1_it.id)
  214. }
  215. }
  216. return score; // already scaled
  217. }
  218. // ---------------------------------------------------------------------------
  219. // ---------------------------------------------------------------------------
  220. double DotProductScoring::score(const BowVector &v1,
  221. const BowVector &v2) const
  222. {
  223. BowVector::const_iterator v1_it, v2_it;
  224. const BowVector::const_iterator v1_end = v1.end();
  225. const BowVector::const_iterator v2_end = v2.end();
  226. v1_it = v1.begin();
  227. v2_it = v2.begin();
  228. double score = 0;
  229. while(v1_it != v1_end && v2_it != v2_end)
  230. {
  231. const WordValue& vi = v1_it->second;
  232. const WordValue& wi = v2_it->second;
  233. if(v1_it->first == v2_it->first)
  234. {
  235. score += vi * wi;
  236. // move v1 and v2 forward
  237. ++v1_it;
  238. ++v2_it;
  239. }
  240. else if(v1_it->first < v2_it->first)
  241. {
  242. // move v1 forward
  243. v1_it = v1.lower_bound(v2_it->first);
  244. // v1_it = (first element >= v2_it.id)
  245. }
  246. else
  247. {
  248. // move v2 forward
  249. v2_it = v2.lower_bound(v1_it->first);
  250. // v2_it = (first element >= v1_it.id)
  251. }
  252. }
  253. return score; // cannot scale
  254. }
  255. // ---------------------------------------------------------------------------
  256. // ---------------------------------------------------------------------------