Face Verification with a Siamese Network
A Siamese convolutional neural network that decides whether two face images show the same person, trained on a small custom dataset of anchor, positive and negative images with data augmentation.
By Jawher Ennaceur
View on GitHub- Deep Learning
- Computer Vision
- Siamese Networks
- Data Augmentation
- TensorFlow
The Problem
A standard classifier needs many images per person and must be retrained whenever someone new is added. Face verification takes a different approach: given a reference photo and a new photo, decide whether they show the same person. This project explores that approach on a small custom dataset.
The Approach
I built a Siamese network with a shared CNN encoder (four convolutional layers and a 4096-unit embedding layer, about 39M parameters in total) that maps each face to an embedding. A custom layer computes the absolute difference between the two embeddings, and a sigmoid output predicts same or different. I collected anchor, positive and negative image sets, resized the images to 105x105, scaled pixels to [0, 1] and augmented the positives with a rotation and a horizontal flip, which tripled the positive set. Pairing each anchor with a positive (label 1) or a negative (label 0) gave 600 pairs, split 70/30 into training and test sets. I trained for 5 epochs with binary cross-entropy and Adam (learning rate 1e-4), using batches of 16, in a custom TensorFlow training loop built on tf.GradientTape.
Results
- Per-batch training loss fell from about 0.69 at the start to mostly below 0.1 by epoch 5, with a minimum around 0.003
- Running training recall and precision reached about 98.6% and 100% in epoch 5 (training set, not held-out data)
- Built a custom training loop that tracks loss, precision and recall per epoch
- Built 600 labeled image pairs from anchor, positive and negative sets, with rotation and flip augmentation on the positives