# minivision-ai/silent-face-anti-spoofing architecture

> Architecture diagram of the GitHub repository minivision-ai/silent-face-anti-spoofing, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-02). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/minivision-ai/silent-face-anti-spoofing
- Repository: https://github.com/minivision-ai/silent-face-anti-spoofing

## Overview

##### Project Type & Purpose
- The project is an open-source computer vision solution aimed at “silent” (non-interactive) face anti-spoofing. It covers both model training and inference (testing) as well as data pre-processing to generate patch images and Fourier spectrum representations.
- The README indicates that the system includes multiple models (e.g., MiniFASNet variants) and leverages detector models (e.g., RetinaFace via Caffe) plus data preprocessing routines. This makes it a specialized deep learning system for live face verification.

##### File Structure Analysis
- The top-level directories and files (e.g., README files, LICENSE, requirements.txt) indicate that this project is self-contained with all necessary resources.
- The resources directory holds pretrained detection models and anti-spoofing models (Caffe and PyTorch models), which suggests the project integrates both face detection and spoofing verification.
- The datasets directory and its accompanying README imply a specific structure for image datasets and differentiated directories for various types of images (full images and patches).
- The src directory organizes the main components:
- The anti_spoof_predict.py handles live inference.
- The data_io subdirectory (with dataset_folder.py, dataset_loader.py, transform.py, etc.) manages data reading, preprocessing, and augmentation.
- The model_lib subdirectory contains model architectures (MiniFASNet, MultiFTNet), and the default_config and utility files support parameter configuration and common functions.
- train_main.py and train.py are used to control the training process.

##### Components
a. Core System Components:
- Data Input & Preprocessing: Represent the datasets (raw images, patches, and Fourier spectrum generation) and the data IO modules (located in src/data_io), which prepare the training and testing data.
- Model Training Pipeline: Include the training scripts (train.py and train_main.py), configuration and utility files (src/default_config.py, src/utility.py), and the model definitions in src/model_lib. Emphasize the branch that uses Fourier spectrum-based auxiliary supervision.
- Inference/Predictive Pipeline: Capture the anti_spoof_predict.py file and test.py, which coordinate with the detection models (from resources/detection_model) and the anti-spoof models (from resources/anti_spoof_models).
- External Components: Show the integration of the detection model (RetinaFace in Caffe), and note that an APK exists for Android deployment although its source is managed in a separate repository.
- Logging & Metrics: Indicate saved_logs as a component capturing training events and job logs.

b. Relationships and Data Flow:
- Training pipeline to the saved_logs component (for monitoring and record keeping).
- For inference, show that input images are first analyzed by a face detection module (the RetinaFace detector) and then passed to the anti-spoofing predictor.
- Configuration and utility components as supporting blocks that interact with both training and testing stages.

c. Architectural Patterns & Design Principles:
- Separation of concerns: data pre-processing, model architecture, training, and inference are modularized.
- Use layered architecture patterns: data layer (datasets, IO), model layer (model_lib), and application layer (training/inference scripts).
- Modularity and extensibility where components such as model definitions and data transforms can be updated independently.
- Fusion strategy described in the README where different patch scales and Fourier spectrum branches are combined.

d. Technologies, Frameworks, and Libraries:
- Deep Learning frameworks (e.g., PyTorch for the anti-spoof models as indicated by ".pth" model files).
- Caffe for the detection model (with files such as deploy.prototxt and .caffemodel).
- Python is the primary language, with dependencies managed via requirements.txt.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Data & Preprocessing Layer
    subgraph "Data & Preprocessing Layer"
        A["Datasets (datasets)"]:::data
        B["Data IO Modules (src/data_io)"]:::data
        C["Patch & Fourier Generation (src/generate_patches.py)"]:::data
        A -->|"feeds"| B
        B -->|"processes data"| C
    end

    %% Model Training Pipeline
    subgraph "Model Training Pipeline"
        D["Training Controllers (train.py, src/train_main.py)"]:::training
        E["Configuration & Utility (src/default_config.py, src/utility.py)"]:::training
        F["Model Architectures (src/model_lib)"]:::training
        G["Logging & Metrics (saved_logs)"]:::db
        C -->|"training input"| D
        D -->|"uses config"| E
        D -->|"trains using"| F
        D -->|"logs progress"| G
    end

    %% Inference / Deployment Pipeline
    subgraph "Inference & Deployment Pipeline"
        H["Input Images"]:::input
        I["Face Detection (RetinaFace Caffe, resources/detection_model)"]:::external
        J["Anti-Spoof Predictor (src/anti_spoof_predict.py)"]:::inference
        K["Testing/Inference (test.py)"]:::inference
        L["Pretrained Anti-Spoof Models (resources/anti_spoof_models)"]:::inference
        M["Android APK Deployment"]:::external

        H -->|"detects face"| I
        I -->|"sends cropped face to"| J
        J -->|"runs tests via"| K
        J -->|"loads models from"| L
        K -->|"outputs result to"| M
        %% Support from configuration used by inference predictor
        E -->|"supports"| J
    end

    %% Click Events for Data & Preprocessing
    click B "https://github.com/minivision-ai/silent-face-anti-spoofing/tree/master/src/data_io"
    click C "https://github.com/minivision-ai/silent-face-anti-spoofing/blob/master/src/generate_patches.py"
    click A "https://github.com/minivision-ai/silent-face-anti-spoofing/tree/master/datasets"

    %% Click Events for Model Training Pipeline
    click D "https://github.com/minivision-ai/silent-face-anti-spoofing/blob/master/train.py"
    click D "https://github.com/minivision-ai/silent-face-anti-spoofing/blob/master/src/train_main.py"
    click E "https://github.com/minivision-ai/silent-face-anti-spoofing/blob/master/src/default_config.py"
    click E "https://github.com/minivision-ai/silent-face-anti-spoofing/blob/master/src/utility.py"
    click F "https://github.com/minivision-ai/silent-face-anti-spoofing/tree/master/src/model_lib"

    %% Click Events for Inference Pipeline
    click J "https://github.com/minivision-ai/silent-face-anti-spoofing/blob/master/src/anti_spoof_predict.py"
    click K "https://github.com/minivision-ai/silent-face-anti-spoofing/blob/master/test.py"
    
    %% Click Events for Detection & Pre-trained Models
    click I "https://github.com/minivision-ai/silent-face-anti-spoofing/tree/master/resources/detection_model"
    click L "https://github.com/minivision-ai/silent-face-anti-spoofing/tree/master/resources/anti_spoof_models"
    
    %% Click Event for Logging & Metrics
    click G "https://github.com/minivision-ai/silent-face-anti-spoofing/tree/master/saved_logs"
    
    %% Click Event for Datasets
    click A "https://github.com/minivision-ai/silent-face-anti-spoofing/tree/master/datasets"

%% Styling for components
classDef data fill:#AED6F1,stroke:#1F618D,stroke-width:2px;
classDef training fill:#ABEBC6,stroke:#27AE60,stroke-width:2px;
classDef inference fill:#F9E79F,stroke:#F1C40F,stroke-width:2px;
classDef external fill:#F5CBA7,stroke:#E67E22,stroke-width:2px;
classDef db fill:#D2B4DE,stroke:#8E44AD,stroke-width:2px;
classDef input fill:#FAD7A0,stroke:#E67E22,stroke-width:2px;
```
