Face Embeddings
Map each face image into a fixed-dimensional, L2-normalized feature vector.
Learning discriminative face representations with a pretrained ResNet-18 and triplet loss. Transform face images into normalized embedding vectors and compare their similarity for face verification.
01 / Overview
Rather than predicting a fixed identity class, this project learns a feature representation that can be compared across face images.
Map each face image into a fixed-dimensional, L2-normalized feature vector.
Learn to reduce distances between same-identity faces and increase distances between different identities.
Compare two embeddings with cosine similarity and evaluate the trade-off between true and false positives.
02 / Methodology
The workflow covers dataset preparation, embedding extraction, triplet-based training, and evaluation.
Load and preprocess RGB face images from the Labeled Faces in the Wild (LFW) dataset.
Replace the ResNet-18 classification head with a projection layer that generates normalized face embeddings.
Train with anchor-positive-negative triplets and triplet margin loss to learn discriminative representations.
Compare embeddings using cosine similarity and evaluate pairwise discrimination with ROC and AUC.
03 / Model Architecture
The pretrained classification head is replaced with a linear projection layer. L2 normalization constrains each embedding to unit length.
Face Image
RGB input
ResNet-18
Pretrained backbone
Linear Layer
Projection head
L2 Normalize
Unit-length vector
Embedding
Feature representation
def forward(self, x):
features = self.backbone(x)
return F.normalize(features, p=2, dim=1)The output is an embedding vector rather than a probability distribution over known identity classes.
04 / Training
Each training sample contains an anchor, a positive example of the same identity, and a negative example from a different identity.
d(a, p) + m < d(a, n)
Desired relationship between positive and negative distances.
The implemented triplet margin loss penalizes triplets that violate the required margin.
05 / Inference
The trained model converts each face image into a normalized embedding. Cosine similarity then measures the directional similarity between two embeddings.



Example inference outputs from the trained model.
Highest similarity
Moderate similarity
Lowest similarity
Interpretation: Face 1 and Face 2 have the highest similarity (0.8614), followed by Face 1 and Face 3 (0.7970). Face 2 and Face 3 have the lowest similarity (0.5767). These values illustrate relative similarity for this example; they do not establish identity by themselves.
with torch.no_grad():
emb_1 = model(img_1)
emb_2 = model(img_2)
emb_3 = model(img_3)
sim_12 = F.cosine_similarity(emb_1, emb_2).item()
sim_13 = F.cosine_similarity(emb_1, emb_3).item()
sim_23 = F.cosine_similarity(emb_2, emb_3).item()
print(f"Similarity (1, 2): {sim_12:.4f}")
print(f"Similarity (1, 3): {sim_13:.4f}")
print(f"Similarity (2, 3): {sim_23:.4f}")06 / Evaluation
The evaluation notebook uses pairwise verification metrics and dimensionality reduction to examine the learned representations.

The ROC curve shows the relationship between the true positive rate and false positive rate across similarity thresholds. AUC summarizes how well the model ranks same-identity pairs above different-identity pairs.

A two-dimensional projection of the learned embeddings for the ten most frequent identities in the test set. Clusters can help reveal representation structure, but t-SNE is qualitative and does not independently measure verification accuracy.
The evaluation procedure samples 3,000 positive pairs and 3,000 negative pairs from the test embeddings and computes cosine similarity and ROC-AUC.
Visualizes the embedding distribution for the ten most frequent test identities to inspect possible identity-level clustering.
07 / Limitations
This project is an educational metric learning experiment, not a production-ready identity verification system.
LFW is relatively small compared with modern large-scale face recognition training datasets.
Random triplet sampling may generate easy triplets that contribute little to learning.
Verification performance depends on data splits, preprocessing, identity distribution, and threshold calibration.
Results on LFW do not establish production-level accuracy, robustness, or suitability for KYC systems.
08 / Tech Stack
The libraries and frameworks used to build the data, training, inference, and evaluation pipeline.
09 / Project Workflow
The project is organized into six notebooks, each covering a stage of the machine learning workflow.
Load and inspect the LFW dataset.
Define the ResNet-18 embedding network.
Implement triplet sampling and image transformations.
Train the network and save the best checkpoint.
Generate normalized embeddings from face images.
Evaluate verification and visualize embeddings.