Add margin check to multi-ref matching to reduce false positives

The multi-ref matching method was missing a margin check against other
logos, causing excessive false positives. This fix adds:

- margin parameter to find_best_match_multi_ref() that requires the
  best logo's score to exceed the second-best by a minimum margin
- Test script now passes --margin to both matching methods
- Updated documentation to reflect margin applies to both methods

Also adds run_comparison_tests.sh to run all three matching methods
and compare results.
This commit is contained in:
Rick McEwen
2025-12-31 11:23:47 -05:00
parent ddccf653d2
commit 197e007591
5 changed files with 120 additions and 29 deletions

View File

@ -143,7 +143,8 @@ match = detector.find_best_match_multi_ref(
reference_embeddings, # Dict: logo_name -> list of embeddings
similarity_threshold=0.85,
min_matching_refs=1,
use_mean_similarity=True
use_mean_similarity=True,
margin=0.05
)
# Returns: (label, similarity, num_matching_refs) or None
```
@ -154,9 +155,13 @@ match = detector.find_best_match_multi_ref(
- `similarity_threshold`: Minimum similarity to consider a match (0-1, default: 0.85)
- `min_matching_refs`: Minimum number of references that must match above threshold (default: 1)
- `use_mean_similarity`: If True, use mean similarity; if False, use max (default: True)
- `margin`: Required difference between best and second-best logo scores (default: 0.0)
**Returns:**
- Tuple of (label, similarity, num_matching_refs) for best match, or None if no match meets criteria
- Tuple of (label, similarity, num_matching_refs) for best match, or None if:
- No logo meets the min_matching_refs requirement, OR
- Best score is below threshold, OR
- Best score doesn't exceed second-best by the required margin
**Example:**
```python
@ -171,7 +176,8 @@ match = detector.find_best_match_multi_ref(
multi_ref_embeddings,
similarity_threshold=0.80,
min_matching_refs=2, # At least 2 refs must match
use_mean_similarity=True # Average across all refs
use_mean_similarity=True, # Average across all refs
margin=0.05 # Require 0.05 margin over second-best logo
)
if match: