# facenox/face-antispoof-onnx architecture

> Architecture diagram of the GitHub repository facenox/face-antispoof-onnx, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-18). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/facenox/face-antispoof-onnx
- Repository: https://github.com/facenox/face-antispoof-onnx

## Overview

1. **Repository purpose**
   - This is a face anti-spoofing project with two main operating modes: training a lightweight classifier and running low-latency inference on images/webcam frames.
   - The production path is centered on **MiniFASNet V2 SE** with **ONNX/quantized ONNX** deployment.

2. **Top-level architecture**
   - The repo is split into four functional areas:
     - `src/detection/`: face localization
     - `src/inference/`: runtime preprocessing and model execution
     - `src/minifasv2/`: active training implementation
     - `scripts/`: dataset preparation, checkpoint cleanup, export, quantization, training entrypoints
   - `src/mobilenetv4/` is a legacy training branch kept for reference, not the main deployment path.

3. **Inference data flow**
   - `demo.py` is the user-facing entrypoint.
   - Input comes from either a webcam frame or a single image.
   - The frame/image is passed through face detection, then face cropping/resizing, then anti-spoof classification.
   - The classifier returns a **real vs spoof** decision that is rendered as a color-coded bounding box in the demo.

4. **Detection boundary**
   - `src/detection/face.py` is the face detection subsystem.
   - Its job is to isolate a face region before classification; this keeps the spoof model focused on face texture rather than the whole scene.
   - This is a clear architectural boundary: detector first, anti-spoof model second.

5. **Inference/runtime boundary**
   - `src/inference/` contains the runtime pieces that glue detection to model execution:
     - `loader.py` for model loading
     - `preprocess.py` for resizing/normalization and input shaping
     - `inference.py` for prediction orchestration
     - `system.py` for higher-level runtime coordination
   - The runtime is designed around **PyTorch for training artifacts** and **ONNX Runtime for deployment artifacts**.

6. **Model and deployment formats**
   - Trained weights are stored in `models/` in multiple forms:
     - `.pth` for PyTorch training/fine-tuning
     - `.onnx` for general inference
     - quantized `.onnx` for smaller production deployment
   - The repo includes both full and quantized exported models, showing a clear separation between training-time artifacts and inference-time artifacts.

7. **Training pipeline**
   - `scripts/train.py` drives training using the active implementation in `src/minifasv2/`.
   - Training outputs checkpoints and logs into an experiment directory under `MINIFAS/`.
   - Checkpoints are preserved at many epochs plus `checkpoint_best.pth` and `checkpoint_latest.pth`, indicating a standard long-running training workflow with resumability.

8. **Dataset preparation flow**
   - `scripts/prepare_data.py` is the data ingestion/preprocessing boundary.
   - It expects face images plus matching bounding-box files and train/test label metadata.
   - The script crops faces, applies padding via a configurable expansion factor, resizes to the model input size, and organizes the result into train/test folders.
   - This preprocessing step is important because the downstream model expects fixed-size face crops, not raw full images.

9. **MiniFAS model-specific training components**
   - `src/minifasv2/` contains the active model/config/data definitions for MiniFASNet V2 SE.
   - The README notes a Fourier Transform auxiliary loss and SE blocks, which are part of the training-time design to improve spoof-vs-real discrimination.
   - Architecturally, this is the core learning subsystem that produces the final classifier used by inference/export.

10. **Model cleanup and export pipeline**
    - `scripts/prepare_best_model.py` extracts a clean inference-ready PyTorch model from a training checkpoint by stripping optimizer/training-only state.
    - `scripts/export_onnx.py` converts the model to ONNX.
    - `scripts/quantize_onnx.py` produces the INT8 quantized ONNX version.
    - This creates a clear lifecycle: checkpoint → cleaned weights → ONNX → quantized deployment artifact.

11. **Legacy experimentation path**
    - `src/mobilenetv4/` contains an older MobileNetV4-based training stack with its own data, labels, sampling, checkpointing, and fine-tuning utilities.
    - It is explicitly described as reference/experimental code retained for future work, while current production training uses MiniFAS.
    - For an architecture diagram, this should be shown as a side branch rather than the main line.

12. **Results, metrics, and documentation**
    - `assets/results/` and `models/best/` capture historical best runs and metric snapshots.
    - `docs/` contains architecture notes, data preparation rationale, metric definitions, previous results, and limitations.
    - These are supporting artifacts rather than runtime components, but they document the system’s performance characteristics and constraints.

13. **Tests and verification**
    - `tests/` covers detection, preprocessing, and inference.
    - The test boundary aligns with the runtime path: verify face detection behavior, preprocessing correctness, and inference integration.
    - This suggests the project treats preprocessing and model-loading as critical correctness points, not just the model itself.

14. **Operational constraint**
    - The deployed model is intentionally small and CPU-friendly, with quantized ONNX emphasized for production use.
    - The system is optimized for low-spec hardware and well-lit frontal faces, which is a practical limitation that should be reflected in any architecture diagram or deployment note.

Main entry points: [`src/minifasv2/main.py`](https://github.com/facenox/face-antispoof-onnx/blob/main/src/minifasv2/main.py) (MiniFAS main), [`src/detection/face.py`](https://github.com/facenox/face-antispoof-onnx/blob/main/src/detection/face.py) (Face detect), [`tests/test_inference.py`](https://github.com/facenox/face-antispoof-onnx/blob/main/tests/test_inference.py) (Tests), [`src/inference/system.py`](https://github.com/facenox/face-antispoof-onnx/blob/main/src/inference/system.py) (System)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Runtime"]
  node_demo["Demo<br/>entrypoint<br/>[demo.py]"]
  node_face_detect["Face detect<br/>detector<br/>[face.py]"]
  node_preprocess["Preprocess<br/>runtime stage<br/>[preprocess.py]"]
  node_loader["Loader<br/>model loading<br/>[loader.py]"]
  node_predict["Predict<br/>inference core<br/>[inference.py]"]
  node_system["System<br/>runtime orchestrator<br/>[system.py]"]
end

subgraph group_training["Training"]
  node_prep_data["Prep data<br/>dataset prep<br/>[prepare_data.py]"]
  node_train_entry["Train<br/>[train.py]"]
  node_cleanup["Clean ckpt<br/>checkpoint cleanup"]
  node_export_onnx["Export ONNX<br/>deployment export<br/>[export_onnx.py]"]
  node_quantize["Quantize<br/>deployment optimize<br/>[quantize_onnx.py]"]
  node_mf_config["MiniFAS cfg<br/>model config<br/>[config.py]"]
  node_mf_data["MiniFAS data<br/>training data<br/>[data.py]"]
  node_mf_model["MiniFASNet<br/>classifier model<br/>[model.py]"]
  node_mf_main["MiniFAS main<br/>training core<br/>[main.py]"]
end

subgraph group_legacy["Legacy"]
  node_legacy_train["Legacy train<br/>reference training<br/>[training.py]"]
  node_legacy_model["Legacy net<br/>reference model<br/>[ft_net.py]"]
end

subgraph group_support["Support"]
  node_deploy_model[("Deploy model<br/>artifact")]
  node_detector_model[("Detector model<br/>artifact")]
  node_tests["Tests<br/>verification<br/>[test_inference.py]"]
  node_docs["Docs<br/>documentation<br/>[ARCHITECTURE.md]"]
end

node_demo -->|"run"| node_system
node_system -->|"detect"| node_face_detect
node_face_detect -->|"crop"| node_preprocess
node_preprocess -->|"shape input"| node_loader
node_loader -->|"execute"| node_predict
node_predict -->|"render result"| node_demo
node_train_entry -->|"start"| node_mf_main
node_mf_main -->|"build"| node_mf_model
node_mf_main -->|"load"| node_mf_data
node_mf_main -->|"configure"| node_mf_config
node_prep_data -->|"prepare"| node_mf_data
node_cleanup -->|"clean"| node_export_onnx
node_export_onnx -->|"convert"| node_quantize
node_quantize -->|"publish"| node_deploy_model
node_face_detect -->|"uses"| node_detector_model
node_system -->|"uses"| node_deploy_model
node_legacy_train -->|"build"| node_legacy_model
node_tests -.->|"verify"| node_face_detect
node_tests -.->|"verify"| node_preprocess
node_tests -.->|"verify"| node_predict
node_docs -.->|"describe"| node_prep_data

click node_demo "https://github.com/facenox/face-antispoof-onnx/blob/main/demo.py"
click node_face_detect "https://github.com/facenox/face-antispoof-onnx/blob/main/src/detection/face.py"
click node_preprocess "https://github.com/facenox/face-antispoof-onnx/blob/main/src/inference/preprocess.py"
click node_loader "https://github.com/facenox/face-antispoof-onnx/blob/main/src/inference/loader.py"
click node_predict "https://github.com/facenox/face-antispoof-onnx/blob/main/src/inference/inference.py"
click node_system "https://github.com/facenox/face-antispoof-onnx/blob/main/src/inference/system.py"
click node_prep_data "https://github.com/facenox/face-antispoof-onnx/blob/main/scripts/prepare_data.py"
click node_train_entry "https://github.com/facenox/face-antispoof-onnx/blob/main/scripts/train.py"
click node_cleanup "https://github.com/facenox/face-antispoof-onnx/blob/main/scripts/prepare_best_model.py"
click node_export_onnx "https://github.com/facenox/face-antispoof-onnx/blob/main/scripts/export_onnx.py"
click node_quantize "https://github.com/facenox/face-antispoof-onnx/blob/main/scripts/quantize_onnx.py"
click node_mf_config "https://github.com/facenox/face-antispoof-onnx/blob/main/src/minifasv2/config.py"
click node_mf_data "https://github.com/facenox/face-antispoof-onnx/blob/main/src/minifasv2/data.py"
click node_mf_model "https://github.com/facenox/face-antispoof-onnx/blob/main/src/minifasv2/model.py"
click node_mf_main "https://github.com/facenox/face-antispoof-onnx/blob/main/src/minifasv2/main.py"
click node_deploy_model "https://github.com/facenox/face-antispoof-onnx/blob/main/models/best/98.20/best_model_quantized.onnx"
click node_detector_model "https://github.com/facenox/face-antispoof-onnx/blob/main/models/detector_quantized.onnx"
click node_legacy_train "https://github.com/facenox/face-antispoof-onnx/blob/main/src/mobilenetv4/training.py"
click node_legacy_model "https://github.com/facenox/face-antispoof-onnx/blob/main/src/mobilenetv4/ft_net.py"
click node_tests "https://github.com/facenox/face-antispoof-onnx/blob/main/tests/test_inference.py"
click node_docs "https://github.com/facenox/face-antispoof-onnx/blob/main/docs/ARCHITECTURE.md"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_demo,node_face_detect,node_preprocess,node_loader,node_predict,node_system toneBlue
class node_prep_data,node_train_entry,node_cleanup,node_export_onnx,node_quantize,node_mf_config,node_mf_data,node_mf_model,node_mf_main toneAmber
class node_legacy_train,node_legacy_model toneMint
class node_deploy_model,node_detector_model,node_tests,node_docs toneRose
```

## Components

### Runtime

Demo and low-latency inference path

- [Demo](https://github.com/facenox/face-antispoof-onnx/blob/main/demo.py): entrypoint · User-facing image/webcam entrypoint · `demo.py`
- [Face detect](https://github.com/facenox/face-antispoof-onnx/blob/main/src/detection/face.py): detector · Locates and isolates the face region · `src/detection/face.py`
- [Preprocess](https://github.com/facenox/face-antispoof-onnx/blob/main/src/inference/preprocess.py): runtime stage · Resizes, normalizes, and shapes face crops · `src/inference/preprocess.py`
- [Loader](https://github.com/facenox/face-antispoof-onnx/blob/main/src/inference/loader.py): model loading · Loads deployment artifacts for execution · `src/inference/loader.py`
- [Predict](https://github.com/facenox/face-antispoof-onnx/blob/main/src/inference/inference.py): inference core · Runs classification and returns real/spoof output · `src/inference/inference.py`
- [System](https://github.com/facenox/face-antispoof-onnx/blob/main/src/inference/system.py): runtime orchestrator · Coordinates detection and inference flow · `src/inference/system.py`

### Training

Active MiniFASV2 training and export pipeline

- [Prep data](https://github.com/facenox/face-antispoof-onnx/blob/main/scripts/prepare_data.py): dataset prep · Builds cropped train/test face datasets · `scripts/prepare_data.py`
- [Train](https://github.com/facenox/face-antispoof-onnx/blob/main/scripts/train.py): training entry · Starts active MiniFASV2 training · `scripts/train.py`
- [Clean ckpt](https://github.com/facenox/face-antispoof-onnx/blob/main/scripts/prepare_best_model.py): checkpoint cleanup · Strips training-only state from checkpoints · `scripts/prepare_best_model.py`
- [Export ONNX](https://github.com/facenox/face-antispoof-onnx/blob/main/scripts/export_onnx.py): deployment export · Converts cleaned weights to ONNX · `scripts/export_onnx.py`
- [Quantize](https://github.com/facenox/face-antispoof-onnx/blob/main/scripts/quantize_onnx.py): deployment optimize · Produces INT8 ONNX for production · `scripts/quantize_onnx.py`
- [MiniFAS cfg](https://github.com/facenox/face-antispoof-onnx/blob/main/src/minifasv2/config.py): model config · Active model and training configuration · `src/minifasv2/config.py`
- [MiniFAS data](https://github.com/facenox/face-antispoof-onnx/blob/main/src/minifasv2/data.py): training data · MiniFASV2 data definitions and loaders · `src/minifasv2/data.py`
- [MiniFASNet](https://github.com/facenox/face-antispoof-onnx/blob/main/src/minifasv2/model.py): classifier model · MiniFASNet V2 SE model definition · `src/minifasv2/model.py`
- [MiniFAS main](https://github.com/facenox/face-antispoof-onnx/blob/main/src/minifasv2/main.py): training core · Active training loop and optimization · `src/minifasv2/main.py`

### Legacy

Older MobileNetV4 training branch kept for reference

- [Legacy train](https://github.com/facenox/face-antispoof-onnx/blob/main/src/mobilenetv4/training.py): reference training · Older MobileNetV4 training stack · `src/mobilenetv4/training.py`
- [Legacy net](https://github.com/facenox/face-antispoof-onnx/blob/main/src/mobilenetv4/ft_net.py): reference model · MobileNetV4 fine-tuning and model code · `src/mobilenetv4/ft_net.py`

### Support

Artifacts, docs, and verification

- [Deploy model](https://github.com/facenox/face-antispoof-onnx/blob/main/models/best/98.20/best_model_quantized.onnx): artifact · Production ONNX classifier artifact · `models/best/98.20/best_model_quantized.onnx`
- [Detector model](https://github.com/facenox/face-antispoof-onnx/blob/main/models/detector_quantized.onnx): artifact · Face detector ONNX artifact · `models/detector_quantized.onnx`
- [Tests](https://github.com/facenox/face-antispoof-onnx/blob/main/tests/test_inference.py): verification · Covers detection, preprocessing, and inference · `tests/test_inference.py`
- [Docs](https://github.com/facenox/face-antispoof-onnx/blob/main/docs/ARCHITECTURE.md): documentation · Architecture, data prep, metrics, and limitations · `docs/ARCHITECTURE.md`

## Connections

- Demo → System: run · User input enters the runtime coordinator
- System → Face detect: detect · Find the face before classification
- Face detect → Preprocess: crop · Face region is resized and normalized
- Preprocess → Loader: shape input · Prepared tensors are passed to the model loader/runtime
- Loader → Predict: execute · Loaded ONNX/PyTorch artifact is used for prediction
- Predict → Demo: render result · Real/spoof decision is shown in the UI
- Train → MiniFAS main: start · Script launches the active training loop
- MiniFAS main → MiniFASNet: build · Training uses the MiniFASNet V2 SE model
- MiniFAS main → MiniFAS data: load · Training consumes MiniFAS-specific data pipeline
- MiniFAS main → MiniFAS cfg: configure · Model and run settings come from config
- Prep data → MiniFAS data: prepare · Raw face assets are converted into model-ready datasets
- Clean ckpt → Export ONNX: clean · Training checkpoint is converted to inference-ready weights
- Export ONNX → Quantize: convert · ONNX export feeds quantization
- Quantize → Deploy model: publish · Quantized ONNX becomes the deployment artifact
- Face detect → Detector model: uses · Detection subsystem relies on detector ONNX
- System → Deploy model: uses · Runtime classifier loads the deployed model
- Legacy train → Legacy net: build · Legacy branch trains its own MobileNetV4 stack
- Tests → Face detect: verify · Checks detection behavior
- Tests → Preprocess: verify · Checks preprocessing correctness
- Tests → Predict: verify · Checks inference integration
- Docs → Prep data: describe · Documents data preparation assumptions and limits
