Computer VisionDeep LearningPyTorch

Face Anti-Spoofing

A deep learning classification model built with Vision Transformer (ViT) and PyTorch to distinguish live faces from spoof attacks. The project explores image preprocessing, model architecture, training, and evaluation using the CelebA-Spoof dataset.

Project Overview

Face anti-spoofing is a computer vision task that determines whether a face presented to a camera belongs to a real person or comes from a spoofing attack. This project uses a pretrained Vision Transformer backbone as a feature extractor, followed by a custom multilayer perceptron classifier for binary classification.

Architecture

Vision Transformer

Input Resolution

224 × 224

Output Classes

Live / Spoof

Framework

PyTorch

Model Architecture

The model consists of a pretrained ViT backbone and a custom classification head. The backbone extracts high-level visual representations, while the classifier maps the extracted features into two output logits.

Input Image

224 × 224 RGB

↓

Vision Transformer

Pretrained ViT Feature Extractor

↓

LayerNorm

Feature Normalization

↓

Dropout

Regularization

↓

Linear → GELU

Feature Projection · 512

↓

Dropout → Linear

Binary Classification

↓

Live

Binary Class

Spoof

Binary Class

Backbonetimm Vision Transformer
Feature ExtractorPretrained ViT, num_classes=0
Hidden Dimension512
ActivationGELU
NormalizationLayerNorm
RegularizationDropout

Dataset

The model is trained and evaluated using the Ar4ikov/celebA_spoof dataset hosted on Hugging Face. It contains face images labeled as live or spoof for face anti-spoofing classification.

Dataset LoadingHugging Face
from datasets import load_dataset

ds = load_dataset("Ar4ikov/celebA_spoof")

Dataset Split

SplitAvailableUsed
Train419,935419,935
Test59,19110,000

Test Set Distribution

Live samples

2,774

27.74% of test set

Spoof samples

7,226

72.26% of test set

Preprocessing

  • • Resize input images to 224 × 224 pixels.
  • • Apply preprocessing required by the ViT backbone.
  • • Convert images into tensors for PyTorch inference.

The complete training split was used for training. For evaluation, 10,000 samples were randomly selected from the original test split of 59,191 samples.

Model Weights

The trained model checkpoint is available on Google Drive. Download the model.pth file to use the trained weights with the corresponding model architecture and configuration.

model.pth

PyTorch model checkpoint

Download Checkpoint

Checkpoint Usage

import torch

checkpoint = torch.load(
    "model.pth",
    map_location="cpu",
    weights_only=True
)

model.load_state_dict(checkpoint)
model.eval()

The loading example assumes model.pth contains a state dictionary. If the checkpoint uses a wrapper dictionary, load the corresponding state_dict entry instead. Make sure the model architecture and configuration match the training setup.

Evaluation Results

The model was evaluated on 10,000 test samples using classification metrics and threshold-independent evaluation.

Accuracy

60.73%

ROC-AUC

0.7513

Precision

39.94%

Recall

82.48%

Specificity

52.38%

F1 Score

53.82%

EER

32.19%

FAR

47.62%

The model achieved 60.73% accuracy and a ROC-AUC of 0.7513. Its recall of 82.48% indicates that a substantial proportion of positive-class samples were detected. However, the false acceptance rate of 47.62% highlights the need for further threshold calibration and validation.

Confusion Matrix

The confusion matrix shows the number of correctly and incorrectly classified samples for each class.

Face anti-spoofing confusion matrix
Actual / PredictedLiveSpoof
Live2,288486
Spoof3,4413,785

Example Predictions

Selected test samples with their ground-truth labels, predicted classes, and Live class probabilities. These examples provide a closer look at how the model classifies individual face images, including correct predictions and misclassifications.

Example face anti-spoofing predictions showing face images, actual labels, predicted labels, and Live probabilities

Input Image

Individual face images from the test dataset.

Ground Truth & Prediction

Actual dataset labels compared with model predictions.

Live Probability

Model output probability for the Live class, useful for examining prediction confidence.

ROC Curve

The Receiver Operating Characteristic (ROC) curve illustrates the relationship between the true positive rate and false positive rate across different classification thresholds. The Area Under the Curve (AUC) provides an overall measure of the model's ability to distinguish between classes.

ROC curve for face anti-spoofing model

ROC-AUC Score

0.7513

Indicates discriminatory ability beyond random classification, while leaving room for improvement.

Limitations & Future Work

Limitations

  • • Evaluation uses only 10,000 of 59,191 test samples.
  • • The test dataset has an imbalanced class distribution.
  • • The false acceptance rate remains relatively high.
  • • Generalization to unseen spoof attacks and real-world capture conditions has not been established.

Future Improvements

  • • Evaluate on the complete test split.
  • • Investigate class imbalance and threshold calibration.
  • • Analyze false-positive and false-negative samples.
  • • Evaluate generalization across spoof attack types.
  • • Optimize inference latency and memory usage.

Source Code

Explore the complete implementation, including data preparation, model architecture, dataset and dataloader, training, and evaluation notebooks.

github.com/ridwanFatur/spoof-detection↗