A deep learning-based face recognition system using Swin Transformer with ArcFace loss and Grey Wolf Optimizer (GWO) for hyperparameter tuning. The system can detect and recognize specific individuals among multiple people in real-time.
- Multi-Person Detection: Simultaneously detect and recognize multiple faces in camera feed
- High Accuracy: 94.2% test accuracy using Swin Transformer + ArcFace
- Real-Time Performance: Optimized for live camera recognition
- Robust Recognition: Multiple embedding support for improved accuracy
- Hyperparameter Optimization: Grey Wolf Optimizer for automatic tuning
- Mixed Precision Training: Faster training with reduced memory usage
- Comprehensive Visualization: Training curves, confusion matrices, ROC curves, and more
- Architecture
- Installation
- Dataset Preparation
- Training
- Inference
- Project Structure
- Configuration
- Results
- Troubleshooting
-
Backbone: Swin Transformer (Tiny variant)
- Hierarchical vision transformer with shifted windows
- Efficient attention mechanism for image features
-
Loss Function: ArcFace (Additive Angular Margin Loss)
- Enhances discriminative power of embeddings
- Margin-based classification for better separation
-
Optimization: Grey Wolf Optimizer (GWO)
- Bio-inspired metaheuristic algorithm
- Automatic hyperparameter tuning (learning rate, weight decay, etc.)
-
Face Detection: Dual detector support
- Haar Cascade (fast, CPU-friendly)
- DNN detector (more accurate, requires model files)
- Python 3.8+
- CUDA 11.0+ (for GPU training)
- Webcam (for real-time recognition)
# Clone the repository
git clone <repository-url>
cd faceReg
# Install required packages
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118
pip install opencv-python numpy pillow matplotlib seaborn
pip install timm tqdm tensorboard scikit-learn pandasFor improved face detection accuracy, download the DNN model files:
# Download Caffe model files
wget https://raw.githubusercontent.com/opencv/opencv_3rdparty/dnn_samples_face_detector_20170830/res10_300x300_ssd_iter_140000.caffemodel
wget https://raw.githubusercontent.com/opencv/opencv/master/samples/dnn/face_detector/deploy.prototxtOrganize your dataset in the following structure:
data/
โโโ train/
โ โโโ person1/
โ โ โโโ img1.jpg
โ โ โโโ img2.jpg
โ โ โโโ ...
โ โโโ person2/
โ โ โโโ ...
โ โโโ ...
โโโ val/
โ โโโ (same structure)
โโโ test/
โโโ (same structure)
- VGGFace2: Large-scale face recognition dataset
- MS-Celeb-1M: Million-scale celebrity dataset
- Custom Dataset: Minimum 10 images per person, 50+ people recommended
- Image format: JPG, PNG
- Minimum resolution: 112x112 pixels
- Face visibility: Clear, frontal or near-frontal faces
- Lighting: Varied lighting conditions for robustness
# Basic training with default parameters
python train.py# Automatic hyperparameter tuning (recommended)
python train_gwo.pyEdit config.py to customize training parameters:
# Key parameters
NUM_CLASSES = 100 # Number of identities
BATCH_SIZE = 32 # Batch size
LEARNING_RATE = 0.001 # Initial learning rate
NUM_EPOCHS = 30 # Training epochs
EMBEDDING_SIZE = 512 # Face embedding dimension- Mixed Precision: Enabled by default for faster training
- Data Augmentation: Random flips, crops, color jitter
- Learning Rate Scheduling: Cosine annealing with warmup
- Gradient Clipping: Prevents exploding gradients
# Launch TensorBoard
tensorboard --logdir=logs
# View at http://localhost:6006# Run multi-person face recognition
python cam.pyInteractive Controls:
qorESC: Quits: Save current framet: Toggle show all faces+/-: Adjust recognition threshold
When prompted, provide:
- Path to target person's photo(s)
- Name of the person
Tips:
- Use multiple photos for better accuracy
- Include different angles and lighting
- Separate multiple paths with commas
# Test on a single image
python fix_inference.pyfaceReg/
โโโ cam.py # Real-time camera recognition
โโโ train.py # Standard training script
โโโ train_gwo.py # Training with GWO optimization
โโโ config.py # Configuration parameters
โโโ model.py # Model architecture
โโโ dataset.py # Data loading and augmentation
โโโ gwo_optimizer.py # Grey Wolf Optimizer
โโโ fix_inference.py # Single image inference
โโโ utils.py # Utility functions
โโโ generate_results_graphs.py # Generate research graphs
โโโ checkpoints/ # Saved model checkpoints
โ โโโ best_model.pth
โโโ logs/ # TensorBoard logs
โโโ data/ # Dataset directory
โโโ README.md # This file
| Parameter | Default | Description |
|---|---|---|
NUM_CLASSES |
100 | Number of identities in dataset |
BATCH_SIZE |
32 | Training batch size |
LEARNING_RATE |
0.001 | Initial learning rate |
WEIGHT_DECAY |
0.0001 | L2 regularization |
NUM_EPOCHS |
30 | Total training epochs |
EMBEDDING_SIZE |
512 | Face embedding dimension |
ARCFACE_S |
30.0 | ArcFace scale parameter |
ARCFACE_M |
0.50 | ArcFace margin parameter |
IMAGE_SIZE |
224 | Input image size |
MIXED_PRECISION |
True | Use mixed precision training |
| Parameter | Default | Description |
|---|---|---|
N_WOLVES |
5 | Number of search agents |
MAX_ITER |
6 | Maximum iterations |
QUICK_EVAL_EPOCHS |
5 | Epochs for quick evaluation |
- Test Accuracy: 94.2%
- Validation Accuracy: 94.9%
- AUC-ROC: 0.967
- Inference Speed: 23ms per image (GPU)
| Model | Accuracy | Parameters |
|---|---|---|
| ResNet50 + Softmax | 86.9% | 25M |
| EfficientNet-B0 + CosFace | 91.4% | 5M |
| Swin-Tiny + ArcFace | 92.1% | 28M |
| Our Method + GWO | 94.2% | 28M |
# Generate all research paper graphs
python generate_results_graphs.pyGenerated graphs:
- Training/validation curves
- GWO convergence
- Confusion matrix
- Architecture comparison
- ROC curve
- Ablation study
- Performance radar chart
- Inference speed analysis
- t-SNE embedding visualization
1. CUDA Out of Memory
# Reduce batch size in config.py
BATCH_SIZE = 16 # or 82. Camera Not Opening
# Try different camera ID
system.run_camera(camera_id=1) # or 2, 3, etc.3. Low Recognition Accuracy
- Adjust threshold: Press
+/-during camera recognition - Register multiple photos of target person
- Ensure good lighting conditions
- Recommended threshold: 0.40-0.50
4. Model Not Found
# Ensure checkpoint exists
ls checkpoints/best_model.pth
# Or train a new model
python train.py5. Face Not Detected
- Ensure face is clearly visible
- Move closer to camera
- Improve lighting
- Try DNN detector for better accuracy
For Faster Training:
- Enable mixed precision (default)
- Increase batch size if GPU memory allows
- Use fewer data augmentation transforms
For Better Accuracy:
- Train longer (increase NUM_EPOCHS)
- Use GWO optimization
- Increase embedding size
- Add more training data
For Real-Time Inference:
- Use Haar Cascade detector (faster)
- Reduce camera resolution
- Process every N frames instead of all
If you use this project in your research, please cite:
@article{facerec2024,
title={Multi-Person Face Recognition using Swin Transformer with ArcFace and Grey Wolf Optimization},
author={Your Name},
year={2024}
}This project is licensed under the MIT License - see the LICENSE file for details.
Contributions are welcome! Please feel free to submit a Pull Request.
For questions or issues, please open an issue on GitHub or contact [your-email@example.com]
- Swin Transformer: Microsoft Research
- ArcFace: InsightFace
- Grey Wolf Optimizer: Original paper by Mirjalili et al.
- OpenCV: Computer vision library
- PyTorch: Deep learning framework
- Add support for video file input
- Implement face tracking across frames
- Add age and gender recognition
- Support for masked face recognition
- Mobile deployment (ONNX/TensorRT)
- Web interface for easy deployment
- Multi-GPU training support
- Quantization for edge devices
Note: This is a research project. For production deployment, additional security and privacy measures should be implemented.