Optimal Verification Threshold
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:
- For each pair, compute Euclidean distance
- Try different threshold values
- For each threshold, count correct predictions
- Return the threshold with highest accuracy
Accuracy=total pairscorrect predictions​
Example:
pairs = [ ([1, 0], [1.1, 0], True), # Same person, distance ≈ 0.1 ([1, 0], [0, 1], False) # Different person, distance ≈ 1.41 ]
0.5
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
You’re tuning a decision threshold on embedding distances to maximize how often your verification system is right.
1. Background Knowledge
In modern face recognition, a deep model maps each face image to a high-dimensional embedding vector (e.g., 128-D). Faces of the same person should have embeddings that are close together, and embeddings of different people should be far apart. To measure this closeness, we use a distance metric, often Euclidean distance:
d(x1​,x2​)=∥x1​−x2​∥2​Face verification is a binary decision problem on a pair of faces: same or different. You compare the distance to a threshold:
- If d<T: predict same person
- If d≥T: predict different person
The choice of threshold T controls the trade-off between false accepts (different people classified as same) and false rejects (same person classified as different). The task in this problem is to search over possible thresholds and pick the one that yields the highest overall accuracy on a given labeled dataset.
2. Algorithm / Approach Pattern
This is a classic threshold sweep or 1D brute-force search over decision boundaries:
- Compute a scalar score for each input (here: distance between embeddings).
- For many candidate thresholds on this score:
- Convert scores to predictions using the threshold.
- Compare predictions with true labels to compute accuracy.
- Choose the threshold with maximum accuracy.
Because the decision rule is 1D and monotonic in the threshold, you only need to consider thresholds around the observed distances, not arbitrary real numbers.
3. Step-by-Step Strategy
- Compute distances
- For each pair of embeddings (\mathbf{x}i​,\mathbf{y}i​), compute Euclidean distance:
import math
def euclidean_distance(a, b):
return math.sqrt(sum((x - y)**2 for x, y in zip(a, b)))
- Store all distances in a list dists[i].
- Align labels
- You have labels like same[i] (1 or True for same person, 0 or False for different).
- Ensure dists[i] and same[i] have the same index alignment.
- Generate candidate thresholds
- A robust pattern for 1D problems: use unique sorted distances (plus maybe extremes) as candidate breakpoints.
- Example: sort all distances: sorted_dists.
- Candidate thresholds can be:
- Each unique distance, or
- Midpoints between consecutive distinct distances:
Continue the full explanation
You're reading the free preview. Unlock the complete walkthrough, the code editor, test runner and reference solution with Premium.
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.