Coding Now – Best AI & Full Stack Courses in Delhi NCR | 100% Placement
Limited Offer: Get 50% OFF on AI & Full Stack Courses
📞 Call Now: +91 9667708830
Back to Deep Learning Notes
Topic #384

Face Recognition

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

TaskQuestion 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

\[ \text{Same person if } \|\mathbf{e}_1-\mathbf{e}_2\| < \tau \]

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)?

Related DL Notes

Want to go beyond the notes?

Join Coding Hubs School of AI's Deep Learning course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available
💬 Talk to Advisor
1
WhatsApp

Latest from Our Blog

Insights on AI, Data Science, Full Stack & Career

View All Articles →