How hairymax/face-antispoofing works
Project Type & Purpose
• The project implements a facial anti-spoofing system that detects spoofing attacks (e.g., print and replay attacks) using a CNN. It focuses on both training models on the CelebA Spoof dataset and running inference (including on video streams) with pretrained models.
• The system covers data preparation, training, model conversion (from PyTorch to ONNX), and inference. It also includes a face detection component (using YOLO) to locate faces before classification.
Analyzing the File Structure
• Key high-level files:
• Data preparation: data_preparation.py (crops and processes raw data).
• Training: train.py and src/train_main.py (drive model training, utilizing architecture defined in src/NN.py).
• Model conversion: model_to_onnx.py (converts a trained .pth model to ONNX).
• Inference: video_predict.py and src/FaceAntiSpoofing.py (run predictions on videos or webcam input) and supporting code in src/antispoof_pretrained.py.
• Face Detection: Located under src/face_detector (with YOLO.py and associated utilities).
• The 'saved_models' directory includes pretrained models in both ONNX and PyTorch formats.
• The README explains the roles, arguments, and overall workflow of the project.
Main Components & Their Interactions
a. Data Preparation Module:
• Script: data_preparation.py.
b. Dataset Loader / Preprocessing:
• Located in src/dataset_loader.py; prepares PyTorch datasets, possibly applying Fourier transform augmentations for training.
c. Training Engine:
• Core modules: src/train_main.py and src/NN.py.
• Reads data via dataset_loader.py, defines the CNN architecture, and manages model training with configuration details in src/config.py.
• Outputs training snapshots and logs (with tensorboard integration, as mentioned in the README).
d. Model Conversion:
• Script: model_to_onnx.py.
• Function: Converts a trained PyTorch (.pth) model into an ONNX model suitable for deployment.
e. Inference / Prediction Pipeline:
• Primary entry points: video_predict.py and src/FaceAntiSpoofing.py.
• These modules load pretrained ONNX models (or PyTorch ones via antispoof_pretrained.py) to perform real-time or video-based facial anti-spoofing.
• Utilizes the face detection component (src/face_detector, implementing YOLO) to locate faces before applying the anti-spoof detection model.
Architectural Patterns & Design Principles
• Modular structure: Separation of data preparation, model training, conversion, and inference into distinct scripts and modules.
• Reusability: Core functionalities (like face detection and dataset handling) are encapsulated in dedicated modules (in src/).
• Configurability: Use of a configuration module (src/config.py) ensures that parameters can be easily adjusted.
• Scalability: Clear division means additional models, preprocessing steps, or inference enhancements can be integrated.
Final Checklist
• Every high-level module is clearly labeled.