123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315 |
- /**
- * File: ScoringObject.cpp
- * Date: November 2011
- * Author: Dorian Galvez-Lopez
- * Description: functions to compute bow scores
- * License: see the LICENSE.txt file
- *
- */
- #include <cfloat>
- #include "TemplatedVocabulary.h"
- #include "BowVector.h"
- using namespace DBoW2;
- // If you change the type of WordValue, make sure you change also the
- // epsilon value (this is needed by the KL method)
- const double GeneralScoring::LOG_EPS = log(DBL_EPSILON); // FLT_EPSILON
- // ---------------------------------------------------------------------------
- // ---------------------------------------------------------------------------
- double L1Scoring::score(const BowVector &v1, const BowVector &v2) const
- {
- BowVector::const_iterator v1_it, v2_it;
- const BowVector::const_iterator v1_end = v1.end();
- const BowVector::const_iterator v2_end = v2.end();
-
- v1_it = v1.begin();
- v2_it = v2.begin();
-
- double score = 0;
-
- while(v1_it != v1_end && v2_it != v2_end)
- {
- const WordValue& vi = v1_it->second;
- const WordValue& wi = v2_it->second;
-
- if(v1_it->first == v2_it->first)
- {
- score += fabs(vi - wi) - fabs(vi) - fabs(wi);
-
- // move v1 and v2 forward
- ++v1_it;
- ++v2_it;
- }
- else if(v1_it->first < v2_it->first)
- {
- // move v1 forward
- v1_it = v1.lower_bound(v2_it->first);
- // v1_it = (first element >= v2_it.id)
- }
- else
- {
- // move v2 forward
- v2_it = v2.lower_bound(v1_it->first);
- // v2_it = (first element >= v1_it.id)
- }
- }
-
- // ||v - w||_{L1} = 2 + Sum(|v_i - w_i| - |v_i| - |w_i|)
- // for all i | v_i != 0 and w_i != 0
- // (Nister, 2006)
- // scaled_||v - w||_{L1} = 1 - 0.5 * ||v - w||_{L1}
- score = -score/2.0;
- return score; // [0..1]
- }
- // ---------------------------------------------------------------------------
- // ---------------------------------------------------------------------------
- double L2Scoring::score(const BowVector &v1, const BowVector &v2) const
- {
- BowVector::const_iterator v1_it, v2_it;
- const BowVector::const_iterator v1_end = v1.end();
- const BowVector::const_iterator v2_end = v2.end();
-
- v1_it = v1.begin();
- v2_it = v2.begin();
-
- double score = 0;
-
- while(v1_it != v1_end && v2_it != v2_end)
- {
- const WordValue& vi = v1_it->second;
- const WordValue& wi = v2_it->second;
-
- if(v1_it->first == v2_it->first)
- {
- score += vi * wi;
-
- // move v1 and v2 forward
- ++v1_it;
- ++v2_it;
- }
- else if(v1_it->first < v2_it->first)
- {
- // move v1 forward
- v1_it = v1.lower_bound(v2_it->first);
- // v1_it = (first element >= v2_it.id)
- }
- else
- {
- // move v2 forward
- v2_it = v2.lower_bound(v1_it->first);
- // v2_it = (first element >= v1_it.id)
- }
- }
-
- // ||v - w||_{L2} = sqrt( 2 - 2 * Sum(v_i * w_i) )
- // for all i | v_i != 0 and w_i != 0 )
- // (Nister, 2006)
- if(score >= 1) // rounding errors
- score = 1.0;
- else
- score = 1.0 - sqrt(1.0 - score); // [0..1]
- return score;
- }
- // ---------------------------------------------------------------------------
- // ---------------------------------------------------------------------------
- double ChiSquareScoring::score(const BowVector &v1, const BowVector &v2)
- const
- {
- BowVector::const_iterator v1_it, v2_it;
- const BowVector::const_iterator v1_end = v1.end();
- const BowVector::const_iterator v2_end = v2.end();
-
- v1_it = v1.begin();
- v2_it = v2.begin();
-
- double score = 0;
-
- // all the items are taken into account
-
- while(v1_it != v1_end && v2_it != v2_end)
- {
- const WordValue& vi = v1_it->second;
- const WordValue& wi = v2_it->second;
-
- if(v1_it->first == v2_it->first)
- {
- // (v-w)^2/(v+w) - v - w = -4 vw/(v+w)
- // we move the -4 out
- if(vi + wi != 0.0) score += vi * wi / (vi + wi);
-
- // move v1 and v2 forward
- ++v1_it;
- ++v2_it;
- }
- else if(v1_it->first < v2_it->first)
- {
- // move v1 forward
- v1_it = v1.lower_bound(v2_it->first);
- }
- else
- {
- // move v2 forward
- v2_it = v2.lower_bound(v1_it->first);
- }
- }
-
- // this takes the -4 into account
- score = 2. * score; // [0..1]
- return score;
- }
- // ---------------------------------------------------------------------------
- // ---------------------------------------------------------------------------
- double KLScoring::score(const BowVector &v1, const BowVector &v2) const
- {
- BowVector::const_iterator v1_it, v2_it;
- const BowVector::const_iterator v1_end = v1.end();
- const BowVector::const_iterator v2_end = v2.end();
-
- v1_it = v1.begin();
- v2_it = v2.begin();
-
- double score = 0;
-
- // all the items or v are taken into account
-
- while(v1_it != v1_end && v2_it != v2_end)
- {
- const WordValue& vi = v1_it->second;
- const WordValue& wi = v2_it->second;
-
- if(v1_it->first == v2_it->first)
- {
- if(vi != 0 && wi != 0) score += vi * log(vi/wi);
-
- // move v1 and v2 forward
- ++v1_it;
- ++v2_it;
- }
- else if(v1_it->first < v2_it->first)
- {
- // move v1 forward
- score += vi * (log(vi) - LOG_EPS);
- ++v1_it;
- }
- else
- {
- // move v2_it forward, do not add any score
- v2_it = v2.lower_bound(v1_it->first);
- // v2_it = (first element >= v1_it.id)
- }
- }
-
- // sum rest of items of v
- for(; v1_it != v1_end; ++v1_it)
- if(v1_it->second != 0)
- score += v1_it->second * (log(v1_it->second) - LOG_EPS);
-
- return score; // cannot be scaled
- }
- // ---------------------------------------------------------------------------
- // ---------------------------------------------------------------------------
- double BhattacharyyaScoring::score(const BowVector &v1,
- const BowVector &v2) const
- {
- BowVector::const_iterator v1_it, v2_it;
- const BowVector::const_iterator v1_end = v1.end();
- const BowVector::const_iterator v2_end = v2.end();
-
- v1_it = v1.begin();
- v2_it = v2.begin();
-
- double score = 0;
-
- while(v1_it != v1_end && v2_it != v2_end)
- {
- const WordValue& vi = v1_it->second;
- const WordValue& wi = v2_it->second;
-
- if(v1_it->first == v2_it->first)
- {
- score += sqrt(vi * wi);
-
- // move v1 and v2 forward
- ++v1_it;
- ++v2_it;
- }
- else if(v1_it->first < v2_it->first)
- {
- // move v1 forward
- v1_it = v1.lower_bound(v2_it->first);
- // v1_it = (first element >= v2_it.id)
- }
- else
- {
- // move v2 forward
- v2_it = v2.lower_bound(v1_it->first);
- // v2_it = (first element >= v1_it.id)
- }
- }
- return score; // already scaled
- }
- // ---------------------------------------------------------------------------
- // ---------------------------------------------------------------------------
- double DotProductScoring::score(const BowVector &v1,
- const BowVector &v2) const
- {
- BowVector::const_iterator v1_it, v2_it;
- const BowVector::const_iterator v1_end = v1.end();
- const BowVector::const_iterator v2_end = v2.end();
-
- v1_it = v1.begin();
- v2_it = v2.begin();
-
- double score = 0;
-
- while(v1_it != v1_end && v2_it != v2_end)
- {
- const WordValue& vi = v1_it->second;
- const WordValue& wi = v2_it->second;
-
- if(v1_it->first == v2_it->first)
- {
- score += vi * wi;
-
- // move v1 and v2 forward
- ++v1_it;
- ++v2_it;
- }
- else if(v1_it->first < v2_it->first)
- {
- // move v1 forward
- v1_it = v1.lower_bound(v2_it->first);
- // v1_it = (first element >= v2_it.id)
- }
- else
- {
- // move v2 forward
- v2_it = v2.lower_bound(v1_it->first);
- // v2_it = (first element >= v1_it.id)
- }
- }
- return score; // cannot scale
- }
- // ---------------------------------------------------------------------------
- // ---------------------------------------------------------------------------
|