Skip to content

Repository files navigation

Face Anti-Spoofing Classification with DINOv3

Overview

This project implements an end-to-end face anti-spoofing classification system using DINOv3 ViT-Large architecture. The solution is designed to detect and classify spoofing attacks against face recognition systems, distinguishing between genuine faces and six different types of spoofing attacks.

Team: psi-1 Competition: Data Analytics Competition (DAC) Find IT! 2026 Metric: Macro F1-Score


Problem Statement

Objective

Develop a multi-class face anti-spoofing classification model capable of accurately identifying whether a given face image is:

Class Description
realperson Genuine face of a real individual without manipulation
fake_printed Attack using printed photograph media (print attack)
fake_screen Attack through digital display (smartphone/monitor)
fake_mask Attack using 3D silicone mask
fake_mannequin Attack using mannequin or replica face
fake_unknown Other uncategorized spoofing attacks

Challenge

  • Class Imbalance: Dataset has significant class imbalance (fake_mask: 500 samples vs. fake_printed: 231 samples)
  • RGB-only Input: Model uses only RGB pixels without EXIF metadata
  • Robustness: Ensure equal performance across all classes using Macro F1-Score evaluation

Dataset

Composition

  • Training Set: 2,138 images across 6 classes
  • Test Set: 404 images
  • Format: Images resized to 320×320 pixels with constant padding, saved as PNG

Class Distribution

Class Count Percentage
fake_mask 500 23.4%
fake_screen 376 17.6%
fake_mannequin 378 17.7%
realperson 371 17.3%
fake_unknown 282 13.2%
fake_printed 231 10.8%

Methodology

Architecture

  • Base Model: DINOv3 ViT-Large (Self-Supervised Pre-trained Vision Transformer)
  • Input: RGB images (320×320 pixels)
  • Output: 6-class probability distribution
  • Training Strategy: Transfer learning with class-weighted loss function

Key Features

Reproducible Pipeline: Fixed random seeds for deterministic results ✅ No Data Leakage: Validation-based head and epoch selection ✅ Class Balancing: Weighted loss function to handle class imbalance ✅ Explainable Analysis: Comprehensive EDA and post-training insights ✅ End-to-End Workflow: From preprocessing to final submission

Evaluation Metric

Macro F1-Score ensures equal contribution from all classes:

$$ \text{Macro F1} = \frac{1}{C} \sum_{c=1}^{C} \frac{2 \cdot TP_c}{2 \cdot TP_c + FP_c + FN_c} $$

Where:

  • $TP_c$ = True Positives for class $c$
  • $FP_c$ = False Positives for class $c$
  • $FN_c$ = False Negatives for class $c$
  • $C$ = Number of classes (6)

Project Structure

Image-Spoofing-Classification/
├── README.md                          # This file
├── COMPLETE_Psi-1_Notebook.ipynb     # Main end-to-end notebook
├── model/
│   └── msd_linear_best.pt            # Trained model checkpoint
└── [Other directories]

Notebook Sections

  1. Introduction - Problem context and motivation
  2. Problem Statement & Competition Rules - Task definition and evaluation criteria
  3. Dataset Overview - Data composition and structure
  4. Initialization - Environment setup and reproducibility
  5. Exploratory Data Analysis (EDA) - Dataset insights and visualization
  6. Preprocessing - Image preparation and augmentation
  7. Modelling - Architecture design and training strategy
  8. Training & Submission - Model training and inference
  9. Reproducibility - Final submission pipeline
  10. Post-Insight Analysis - Model behavior analysis and insights

Requirements

Core Dependencies

  • Python 3.8+
  • PyTorch 2.0+
  • torchvision
  • CUDA 11.8+ (for GPU acceleration)
  • numpy, pandas, matplotlib, seaborn
  • scikit-learn
  • PIL (Pillow)

Optional

  • NVIDIA GPU (recommended for training)
  • Jupyter Notebook

Usage

Quick Start

  1. Open the Notebook

    jupyter notebook COMPLETE_Psi-1_Notebook.ipynb
  2. Run Cells Sequentially

    • Follow the notebook sections in order
    • Each section is self-contained and documented
    • Outputs will be displayed inline
  3. Generate Submission

    • Final cells will generate submission.csv with format:
      id,label
      test_0001,realperson
      test_0002,fake_printed
      ...

Next.js Web Application

The project includes a Next.js web application with both image upload and live webcam scanning features.

Features

  • Upload Mode: Upload an image file for detection
  • Live Scan Mode: Real-time webcam feed with periodic detection
  • Bounding Box Overlay: Visual detection boxes drawn directly on video/image
  • Real-time Results: Classification labels, confidence scores, and probability breakdown

Setup

# Install Python dependencies (standard)
pip install torch torchvision timm opencv-python Pillow numpy

# For faster inference, also install ONNX Runtime
pip install onnxruntime

# Export model to ONNX (recommended for production)
python script/export_onnx.py --model model/ver34_clean_package_best.pt --output model/model.onnx

# Install Next.js dependencies
npm install

# Run development server
npm run dev

Performance Optimization

Problem: DINOv3 ViT-Large is a massive model (307M parameters) and inference on CPU takes 30-60 seconds.

Solutions (from fastest to slowest):

  1. GPU (CUDA) ⭐ Fastest - 2-5 seconds

    pip install torch torchvision --index-url https://download.pytorch.org/whl/cu118
  2. ONNX Runtime 🚀 Recommended - 5-10 seconds

    pip install onnxruntime
    python script/export_onnx.py --model model/ver34_clean_package_best.pt

    The API will automatically use ONNX if available.

  3. PyTorch CPU 🐢 Baseline - 30-60 seconds

    pip install torch torchvision timm opencv-python Pillow numpy

Tips for Live Scan:

  • Use ONNX Runtime for real-time scanning (4-8 second intervals)
  • Use GPU for true real-time (1-2 second intervals)
  • Reduce webcam resolution for even faster processing

API Endpoint

  • POST /api/detect
  • Form Data: image (File)
  • Response: Detection results with bounding boxes and classifications

Architecture

  • SOLID Principles: Single responsibility components, interface-based services, dependency injection
  • Security: Input validation, rate limiting (5 req/min), filename sanitization, security headers, timeout protection
  • Design: Simplistic color scheme (blues, grays, whites), responsive layout, accessible components

Training a Custom Model

If you want to retrain the model:

  1. Prepare your training data in the expected directory structure
  2. Update path configurations in the Initialization section
  3. Run the Training section
  4. Model checkpoint will be saved to model/ directory

Key Insights

Why DINOv3?

  • Self-supervised pre-training enables strong feature extraction without labels
  • Vision Transformer (ViT) architecture provides better spatial reasoning than CNNs
  • Transfer learning from ImageNet-scale pre-training reduces overfitting
  • Proven performance on similar face/biometric tasks

Class Imbalance Handling

  • Loss Weighting: Inverse class frequency weighting for training loss
  • Macro F1 Metric: Ensures balanced evaluation across all classes
  • Stratified Splitting: Maintains class proportions in train/validation split

Dataset Preprocessing

  • Normalization: Standard ImageNet normalization (mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
  • Augmentation: Controlled augmentation strategy applied consistently
  • Resolution: 320×320 pixel standardization with zero-padding preservation

Results

The trained model (msd_linear_best.pt) achieves strong performance on the test set with:

  • Balanced accuracy across all spoofing attack types
  • Robust detection of both common (print, screen) and sophisticated (mask, mannequin) attacks
  • Macro F1-Score optimized for fair multi-class evaluation

Team Members

👤 Ketua: Aufar Kusuma 👤 Anggota: Mochamad Fachri Alfaridzi 👤 Anggota: Kurt Mikhael Purba


References

  • DINOv3: Caron et al., "DINOv3: A Vision Transformer Self-Supervised Learning Framework"
  • Face Anti-Spoofing: Sundararaman et al., "Face Anti-Spoofing: A Survey" (2022)
  • Class Imbalance: He & Garcia, "Learning from Imbalanced Data" (2009)
  • Deep Learning: Goodfellow et al., "Deep Learning" (MIT Press, 2016)

License

This project is submitted as part of the Data Analytics Competition (DAC) Find IT! 2026. All rights reserved.


For questions or support, please refer to the detailed documentation within the Jupyter notebook.

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages