Face recognition is a specialized computer vision application built directly on embedding-based learning โ using the exact contrastive and triplet loss ideas covered in the Loss Functions category to map faces into a space where identity is measured by distance.
Verification vs Identification
| Task | Question Answered |
|---|---|
| Face verification | "Are these two face images the same person?" (a 1-to-1 comparison) |
| Face identification | "Which person, among a known database, does this face belong to?" (a 1-to-many comparison) |
The Embedding-Based Approach
Rather than classifying faces into a fixed set of known identities (which wouldn't generalize to new people never seen during training), a face recognition network learns to map any face image into a fixed-length embedding vector, such that images of the same person are close together in that embedding space, and images of different people are far apart โ trained using triplet loss (see Triplet Loss) or contrastive loss (see Contrastive Loss).
Verification via Distance Thresholding
Once two faces are embedded, verification is simply a distance comparison against a chosen threshold \(\tau\) โ the exact same Euclidean distance measure used throughout the triplet/contrastive loss formulas.
Code
import torch
import torch.nn.functional as F
def is_same_person(embedding1, embedding2, threshold=1.0):
distance = F.pairwise_distance(embedding1.unsqueeze(0), embedding2.unsqueeze(0))
return distance.item() < threshold
# embedding1, embedding2 would come from a trained face-embedding network
embedding1 = torch.randn(128)
embedding2 = torch.randn(128)
print(is_same_person(embedding1, embedding2))
Common Mistakes
- Framing face recognition as standard fixed-class classification โ this fails to generalize to new individuals never seen during training; the embedding-based, distance-comparison approach is what enables recognizing people not present in the original training set.
- Choosing a verification threshold \(\tau\) without considering the precision/recall tradeoff (see Precision & Recall) appropriate for the specific application's stakes (e.g. security access vs. photo-tagging convenience).
Interview Relevance
Q: "Why does face recognition typically use an embedding-based approach rather than standard classification?" Classification requires a fixed, known set of classes decided at training time โ it can't recognize a new person never seen during training without retraining the whole classifier. An embedding-based approach, trained with triplet or contrastive loss, learns a general notion of "same person = close in embedding space," which generalizes to verifying or identifying people never encountered during training, as long as at least one reference image of them exists.
Practice Question
Why would face identification (matching against a large known database) typically be more computationally demanding than face verification (a single 1-to-1 comparison)?