# sunyiyou/safas architecture

> Architecture diagram of the GitHub repository sunyiyou/safas, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-09-29). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/sunyiyou/safas
- Repository: https://github.com/sunyiyou/safas

## Overview

This repository implements a research workflow for cross-domain face anti-spoofing: videos from several face-spoofing datasets are preprocessed into face crops and metadata, assembled according to a train/test protocol, and used to train and evaluate a ResNet-based model. The README documents the supported datasets and preprocessing/training entry points. Sampled training, dataset, and model code supports the main wiring; unsampled portions limit detail about evaluation outputs and some preprocessing steps.

Main entry points: [`train.py`](https://github.com/sunyiyou/safas/blob/master/train.py) (Training and testing), [`preposess.py`](https://github.com/sunyiyou/safas/blob/master/preposess.py) (Video preprocessing), [`datasets/__init__.py`](https://github.com/sunyiyou/safas/blob/master/datasets/__init__.py) (Dataset assembly), [`datasets/supcon_dataset.py`](https://github.com/sunyiyou/safas/blob/master/datasets/supcon_dataset.py) (Face samples)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_prep["Video preprocessing"]
  node_preprocess["Video preprocessing<br/>[preposess.py]"]
  node_crop["Face crop utilities<br/>[rotate_crop.py]"]
  node_prepared[("Prepared face data")]
end

subgraph group_data["Data and protocols"]
  node_protocol["Protocol mapping<br/>[__init__.py]"]
  node_dataset_build["Dataset assembly<br/>[__init__.py]"]
  node_face_dataset["Face samples<br/>[supcon_dataset.py]"]
  node_domain_meta["Domain metadata<br/>[meta.py]"]
end

subgraph group_learn["Model and training"]
  node_trainer["Training and testing<br/>[train.py]"]
  node_model_factory["Model factory<br/>[__init__.py]"]
  node_resnet["ResNet classifiers<br/>[resnet_lgt.py]"]
  node_losses["Training losses<br/>[__init__.py]"]
end

subgraph group_eval["Evaluation"]
  node_metrics["Performance metrics<br/>[performance.py]"]
  node_scores["Evaluation scores"]
end

node_researcher(("Researcher"))
node_rawdata[("Raw videos")]
node_pretrained[("Pretrained weights")]

node_researcher -->|"runs preprocessing"| node_preprocess
node_rawdata -->|"provides videos"| node_preprocess
node_preprocess -->|"uses crop helpers"| node_crop
node_preprocess -->|"writes crops and metadata"| node_prepared
node_researcher -->|"runs protocol"| node_trainer
node_trainer -->|"requests datasets"| node_dataset_build
node_dataset_build -->|"decodes protocol"| node_protocol
node_dataset_build -->|"constructs splits"| node_face_dataset
node_face_dataset -->|"reads samples"| node_prepared
node_face_dataset -->|"uses label patterns"| node_domain_meta
node_trainer -->|"creates model"| node_model_factory
node_model_factory -->|"selects ResNet"| node_resnet
node_trainer -->|"trains and scores"| node_resnet
node_trainer -->|"computes feature loss"| node_losses
node_pretrained -.->|"supplies optional weights"| node_trainer
node_trainer -->|"evaluates predictions"| node_metrics
node_trainer -->|"writes evaluation scores"| node_scores

click node_preprocess "https://github.com/sunyiyou/safas/blob/master/preposess.py"
click node_crop "https://github.com/sunyiyou/safas/blob/master/utils/rotate_crop.py"
click node_protocol "https://github.com/sunyiyou/safas/blob/master/utils/__init__.py"
click node_dataset_build "https://github.com/sunyiyou/safas/blob/master/datasets/__init__.py"
click node_face_dataset "https://github.com/sunyiyou/safas/blob/master/datasets/supcon_dataset.py"
click node_domain_meta "https://github.com/sunyiyou/safas/blob/master/datasets/meta.py"
click node_trainer "https://github.com/sunyiyou/safas/blob/master/train.py"
click node_model_factory "https://github.com/sunyiyou/safas/blob/master/networks/__init__.py"
click node_resnet "https://github.com/sunyiyou/safas/blob/master/networks/resnet_lgt.py"
click node_losses "https://github.com/sunyiyou/safas/blob/master/loss/__init__.py"
click node_metrics "https://github.com/sunyiyou/safas/blob/master/utils/performance.py"

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_preprocess,node_crop,node_prepared toneBlue
class node_protocol,node_dataset_build,node_face_dataset,node_domain_meta,node_rawdata,node_pretrained toneAmber
class node_trainer,node_model_factory,node_resnet,node_losses toneMint
class node_metrics,node_scores toneRose
class node_researcher toneIndigo
```

## Components

### Video preprocessing

- [Video preprocessing](https://github.com/sunyiyou/safas/blob/master/preposess.py): `preposess.py`
- [Face crop utilities](https://github.com/sunyiyou/safas/blob/master/utils/rotate_crop.py): `utils/rotate_crop.py`
- **Prepared face data**

### Data and protocols

- [Protocol mapping](https://github.com/sunyiyou/safas/blob/master/utils/__init__.py): `utils/__init__.py`
- [Dataset assembly](https://github.com/sunyiyou/safas/blob/master/datasets/__init__.py): `datasets/__init__.py`
- [Face samples](https://github.com/sunyiyou/safas/blob/master/datasets/supcon_dataset.py): `datasets/supcon_dataset.py`
- [Domain metadata](https://github.com/sunyiyou/safas/blob/master/datasets/meta.py): `datasets/meta.py`

### Model and training

- [Training and testing](https://github.com/sunyiyou/safas/blob/master/train.py): `train.py`
- [Model factory](https://github.com/sunyiyou/safas/blob/master/networks/__init__.py): `networks/__init__.py`
- [ResNet classifiers](https://github.com/sunyiyou/safas/blob/master/networks/resnet_lgt.py): `networks/resnet_lgt.py`
- [Training losses](https://github.com/sunyiyou/safas/blob/master/loss/__init__.py): `loss/__init__.py`

### Evaluation

- [Performance metrics](https://github.com/sunyiyou/safas/blob/master/utils/performance.py): `utils/performance.py`
- **Evaluation scores**

### Other components

- **Researcher**
- **Raw videos**
- **Pretrained weights**

## Connections

- Researcher → Video preprocessing: runs preprocessing
- Raw videos → Video preprocessing: provides videos
- Video preprocessing → Face crop utilities: uses crop helpers
- Video preprocessing → Prepared face data: writes crops and metadata
- Researcher → Training and testing: runs protocol
- Training and testing → Dataset assembly: requests datasets
- Dataset assembly → Protocol mapping: decodes protocol
- Dataset assembly → Face samples: constructs splits
- Face samples → Prepared face data: reads samples
- Face samples → Domain metadata: uses label patterns
- Training and testing → Model factory: creates model
- Model factory → ResNet classifiers: selects ResNet
- Training and testing → ResNet classifiers: trains and scores
- Training and testing → Training losses: computes feature loss
- Pretrained weights → Training and testing: supplies optional weights
- Training and testing → Performance metrics: evaluates predictions
- Training and testing → Evaluation scores: writes evaluation scores
