Face Detection, Verification, and Recognition Pipelines
Module: Computer Vision | Difficulty: Premium
ArcFace Loss (Additive Angular Margin)
where is the angle between embedding and class center, is scale, is margin.
Cosine Similarity
Face Detection: MultiTask Loss
| System | Dataset | Accuracy | Embedding | Year | |--------|---------|----------|-----------|------| | FaceNet | LFW | 99.63% | 128D | 2015 | | ArcFace | LFW | 99.83% | 512D | 2019 | | AdaFace | LFW | 99.82% | 512D | 2022 | | InsightFace | IJB-C | 98.35% | 512D | 2021 |
import torch
import torch.nn as nn
import torch.nn.functional as F
class ArcFaceLoss(nn.Module):
def __init__(self, embedding_dim, num_classes, s=30.0, m=0.5):
super().__init__()
self.s = s
self.m = m
self.weight = nn.Parameter(torch.randn(num_classes, embedding_dim))
nn.init.xavier_uniform_(self.weight)
def forward(self, embeddings, labels):
embeddings = F.normalize(embeddings, dim=1)
weights = F.normalize(self.weight, dim=1)
cosine = F.linear(embeddings, weights)
one_hot = torch.zeros_like(cosine)
one_hot.scatter_(1, labels.view(-1, 1), 1.0)
theta = torch.acos(torch.clamp(cosine, -1.0 + 1e-7, 1.0 - 1e-7))
target_logits = torch.cos(theta + self.m)
logits = one_hot * target_logits + (1 - one_hot) * cosine
logits *= self.s
return F.cross_entropy(logits, labels)
def l2_normalize(x, axis=-1):
return x / (torch.norm(x, dim=axis, keepdim=True) + 1e-8)
Research Insight: Modern face recognition has shifted from learning discriminative losses alone to also considering pose, age, and expression invariance. AdaFace introduced margin-based loss that adapts the margin magnitude based on image quality, giving harder margins to high-quality faces. Self-supervised face recognition (using contrastive learning on unlabeled face collections) is closing the gap with supervised methods, enabling deployment in scenarios where labeled data is scarce or privacy-constrained.