Skip to content

Charanvas/AeroVision

Repository files navigation

Multi-Person Face Recognition System

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.

๐ŸŒŸ Features

  • 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

๐Ÿ“‹ Table of Contents

๐Ÿ—๏ธ Architecture

Model Components

  1. Backbone: Swin Transformer (Tiny variant)

    • Hierarchical vision transformer with shifted windows
    • Efficient attention mechanism for image features
  2. Loss Function: ArcFace (Additive Angular Margin Loss)

    • Enhances discriminative power of embeddings
    • Margin-based classification for better separation
  3. Optimization: Grey Wolf Optimizer (GWO)

    • Bio-inspired metaheuristic algorithm
    • Automatic hyperparameter tuning (learning rate, weight decay, etc.)
  4. Face Detection: Dual detector support

    • Haar Cascade (fast, CPU-friendly)
    • DNN detector (more accurate, requires model files)

๐Ÿš€ Installation

Prerequisites

  • Python 3.8+
  • CUDA 11.0+ (for GPU training)
  • Webcam (for real-time recognition)

Install Dependencies

# 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 pandas

Optional: DNN Face Detector

For 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.prototxt

๐Ÿ“ Dataset Preparation

Dataset Structure

Organize your dataset in the following structure:

data/
โ”œโ”€โ”€ train/
โ”‚   โ”œโ”€โ”€ person1/
โ”‚   โ”‚   โ”œโ”€โ”€ img1.jpg
โ”‚   โ”‚   โ”œโ”€โ”€ img2.jpg
โ”‚   โ”‚   โ””โ”€โ”€ ...
โ”‚   โ”œโ”€โ”€ person2/
โ”‚   โ”‚   โ””โ”€โ”€ ...
โ”‚   โ””โ”€โ”€ ...
โ”œโ”€โ”€ val/
โ”‚   โ””โ”€โ”€ (same structure)
โ””โ”€โ”€ test/
    โ””โ”€โ”€ (same structure)

Recommended Dataset

  • VGGFace2: Large-scale face recognition dataset
  • MS-Celeb-1M: Million-scale celebrity dataset
  • Custom Dataset: Minimum 10 images per person, 50+ people recommended

Data Requirements

  • Image format: JPG, PNG
  • Minimum resolution: 112x112 pixels
  • Face visibility: Clear, frontal or near-frontal faces
  • Lighting: Varied lighting conditions for robustness

๐ŸŽ“ Training

Quick Start Training

# Basic training with default parameters
python train.py

Training with GWO Optimization

# Automatic hyperparameter tuning (recommended)
python train_gwo.py

Configuration

Edit 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

Training Options

  • 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

Monitor Training

# Launch TensorBoard
tensorboard --logdir=logs

# View at http://localhost:6006

๐ŸŽฏ Inference

Real-Time Camera Recognition

# Run multi-person face recognition
python cam.py

Interactive Controls:

  • q or ESC: Quit
  • s: Save current frame
  • t: Toggle show all faces
  • +/-: Adjust recognition threshold

Register Target Person

When prompted, provide:

  1. Path to target person's photo(s)
  2. Name of the person

Tips:

  • Use multiple photos for better accuracy
  • Include different angles and lighting
  • Separate multiple paths with commas

Single Image Inference

# Test on a single image
python fix_inference.py

๐Ÿ“‚ Project Structure

faceReg/
โ”œโ”€โ”€ 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

โš™๏ธ Configuration

Key Configuration Parameters

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

GWO Parameters

Parameter Default Description
N_WOLVES 5 Number of search agents
MAX_ITER 6 Maximum iterations
QUICK_EVAL_EPOCHS 5 Epochs for quick evaluation

๐Ÿ“Š Results

Performance Metrics

  • Test Accuracy: 94.2%
  • Validation Accuracy: 94.9%
  • AUC-ROC: 0.967
  • Inference Speed: 23ms per image (GPU)

Model Comparison

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 Result Graphs

# Generate all research paper graphs
python generate_results_graphs.py

Generated graphs:

  • Training/validation curves
  • GWO convergence
  • Confusion matrix
  • Architecture comparison
  • ROC curve
  • Ablation study
  • Performance radar chart
  • Inference speed analysis
  • t-SNE embedding visualization

๐Ÿ”ง Troubleshooting

Common Issues

1. CUDA Out of Memory

# Reduce batch size in config.py
BATCH_SIZE = 16  # or 8

2. 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.py

5. Face Not Detected

  • Ensure face is clearly visible
  • Move closer to camera
  • Improve lighting
  • Try DNN detector for better accuracy

Performance Optimization

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

๐Ÿ“ Citation

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}
}

๐Ÿ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

๐Ÿค Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

๐Ÿ“ง Contact

For questions or issues, please open an issue on GitHub or contact [your-email@example.com]

๐Ÿ™ Acknowledgments

  • Swin Transformer: Microsoft Research
  • ArcFace: InsightFace
  • Grey Wolf Optimizer: Original paper by Mirjalili et al.
  • OpenCV: Computer vision library
  • PyTorch: Deep learning framework

๐Ÿ”ฎ Future Work

  • 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.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages