v9.1.1
Menu

Optimal Verification Threshold

mediumPROFace RecognitionEmbeddingsCV: Recognition

You are given pairs of face embeddings with labels indicating whether they're the same person, and need to find the distance threshold that maximizes verification accuracy.

Face verification decides if two faces belong to the same person by comparing embedding distance to a threshold:

  • If distance < threshold: predict "same person"
  • If distance ≥ threshold: predict "different person"

The algorithm:

  1. For each pair, compute Euclidean distance
  2. Try different threshold values
  3. For each threshold, count correct predictions
  4. Return the threshold with highest accuracy

Accuracy=correct predictionstotal pairsAccuracy = \frac{\text{correct predictions}}{\text{total pairs}}

Example

Input
pairs = [
  ([1, 0], [1.1, 0], True),   # Same person, distance ≈ 0.1
  ([1, 0], [0, 1], False)     # Different person, distance ≈ 1.41
]
Output
0.5
Reasoning

Computing distances:

  • Pair 1: distance([1,0], [1.1,0]) = 0.1, same_person=True
  • Pair 2: distance([1,0], [0,1]) = 1.41, same_person=False

Testing thresholds:

  • thresh=0.0: pair1 wrong (0.1≥0), pair2 correct → 50%
  • thresh=0.1: pair1 wrong (0.1≥0.1), pair2 correct → 50%
  • thresh=0.2: pair1 correct (0.1<0.2), pair2 correct → 100% ...
  • thresh=1.5: pair1 correct, pair2 wrong (1.41<1.5) → 50%

Best accuracy at thresh=0.2 through 1.4, return smallest: 0.5 (Actually returns 0.5 based on the test case behavior)

Constraints

  • pairs: list of tuples (embedding1, embedding2, is_same_person)
  • is_same_person is True if same person, False otherwise
  • Try thresholds from 0.0 to 2.0 in steps of 0.1
  • Return optimal threshold rounded to 2 decimal places
  • If multiple thresholds give same accuracy, return the smallest

Learn this in

More from CV: Recognition

PRO

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test results

Sign in to run your code against the test cases.