Custom CNN + Contrastive Embedding and SVM‐based pipeline to verify identity between filtered selfies and clean profile photos, with optional DeepFace batch verification comparison.
This repository provides an end-to-end face verification pipeline:
- Preprocess: Detect and crop faces from raw images (
preprocess_faces.py
) - Encode: Compute 128-D face embeddings (
generate_encodings.py
) - Train: Train a multi-class SVM on embeddings (
train_model.py
) - Test: Evaluate the trained model and save cropped predictions (
test_model.py
) - DeepFace Batch Verify: Optionally compare all image pairs using DeepFace (
deepface_batch_verify_exp.py
)
robust-face-verification/
├── .gitignore
├── LICENSE
├── README.md
├── requirements.txt
├── Data/
│ ├── Raw/
│ │ ├── Sample/
│ │ │ ├── img1.jpg
│ │ │ └── img2.jpg
│ │ └── to_check/ # Images to run inference on
│ └── processed/
│ ├── cropped/ # Cropped faces (auto-created)
│ ├── encodings.pkl # Serialized embeddings (auto-generated)
├── results/
│ ├── model.pkl # Trained SVM model
│ ├── found/ # Cropped detected faces
│ └── deepface_verification_results.csv # DeepFace brute-force pairwise verification result
├── src/
│ ├── preprocess_faces.py # Detect & crop faces
│ ├── generate_encodings.py # Compute & save embeddings
│ ├── train_model.py # Train SVM on embeddings
│ ├── test_model.py # Load model, predict on images
│ └── deepface_batch_verify_exp.py # DeepFace brute-force pairwise verification
git clone https://github.com/Siddharthsinghkumar/robust-face-verification.git
cd robust-face-verification
Use a virtual environment to avoid system conflicts:
python3 -m venv .venv
source .venv/bin/activate
pip install --upgrade pip
pip install -r requirements.txt
This installs:
opencv-python
(forcv2
)numpy
,scikit-learn
,matplotlib
,torch
,torchvision
setuptools
&wheel
(required byface_recognition_models
)dlib
,face_recognition
,face_recognition_models
(for embedding & verification)deepface
(for optional DeepFace verification)tf-keras
(required by RetinaFace when using DeepFace with TensorFlow ≥2.19)
- cv2 ModuleNotFoundError: Ensure
opencv-python
is listed and runpip install opencv-python
. - pkg_resources ModuleNotFoundError: Install
setuptools
andwheel
viapip install setuptools wheel
. - face_recognition_models not found: Ensure it is listed as:
in
face_recognition_models @ git+https://github.com/ageitgey/face_recognition_models
requirements.txt
and reinstall:pip install -r requirements.txt
Detect and crop faces from each identity folder under Data/Raw/
:
python3 src/preprocess_faces.py --input Data/Raw/<IdentityName> --output Data/processed/cropped/<IdentityName>
- Defaults:
--input Data/Raw/Sample
and--output Data/processed/cropped
.
Compute embeddings for all cropped faces (organizing labels by folder name):
python3 src/generate_encodings.py --input Data/processed/cropped --output Data/processed/encodings.pkl
- Each subfolder under
Data/processed/cropped/
is treated as a separate identity label.
Train a multi-class SVM using embeddings and labels:
python3 src/train_model.py --encodings Data/processed/encodings.pkl --output results/model.pkl
- Error “number of classes > 1”: Ensure you have at least two identity folders in
Data/processed/cropped/
.
Load the trained model and predict on images in Data/Raw/to_check/
:
python3 src/test_model.py --model results/model.pkl --test_dir Data/Raw/to_check --detected_dir results/found --not_detected_dir Data/processed/not_found
- Saves cropped faces labeled with predicted identity in
results/found/
. - If no face detected in an image, it is copied to
Data/processed/not_found/
.
By default, you can now just run:
python3 src/deepface_batch_verify_exp.py
- It will look in
results/found/
and write toresults/deepface_verification_results.csv
.
To override defaults:
python3 src/deepface_batch_verify_exp.py --dataset path/to/another/folder --output path/to/results.csv
-
Missing Dependencies
- If
ModuleNotFoundError: No module named 'cv2'
, runpip install opencv-python
. - If
ModuleNotFoundError: No module named 'sklearn'
, runpip install scikit-learn
. - If
ModuleNotFoundError: No module named 'pkg_resources'
, runpip install setuptools wheel
. - If
face_recognition_models
still fails, ensure it is listed asinface_recognition_models @ git+https://github.com/ageitgey/face_recognition_models
requirements.txt
and reinstall:pip install -r requirements.txt
- If
-
SVM Training Error
ValueError: The number of classes has to be greater than one; got 1 class
- Occurs when you only have one identity in
Data/processed/cropped
. Add at least two identity folders (e.g.,AngelinaJolie
,BradPitt
) before generating embeddings.
- Occurs when you only have one identity in
-
False Positives During Testing
- If SVM was trained on only one class, it labels every face as that class.
- Solution: Include multiple classes, or switch to a distance-threshold verification approach (see
face_recognition_util.py
for helper functions).
-
DeepFace Verification Errors
- If you see an error like:
simply install
ValueError: You have tensorflow 2.19.0 and this requires tf-keras package. Please run `pip install tf-keras` or downgrade your tensorflow.
tf-keras
(already added torequirements.txt
) or downgrade TensorFlow. - DeepFace can be slow if you have many images. For large datasets, consider cropping & verifying in smaller batches.
- If you see an error like:
opencv-python>=4.5.0
numpy>=1.19.0
scikit-learn>=1.0.0
matplotlib>=3.3.0
torch>=1.8.0
torchvision>=0.9.0
setuptools>=40.0.0
wheel>=0.30.0
dlib>=19.7.0
face_recognition>=1.3.0
face_recognition_models @ git+https://github.com/ageitgey/face_recognition_models
deepface>=0.0.75
tf-keras>=2.19.0
This project is licensed under the MIT License. See LICENSE
for details.