# gathierry/fastflow architecture

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

- Interactive diagram: https://gitdiagram.com/gathierry/fastflow
- Repository: https://github.com/gathierry/fastflow

## Overview

##### Project Type & Purpose
- This is a research-oriented machine-learning pipeline (not a web app or microservice).
- Purpose: train and evaluate the FastFlow 2D normalizing-flow model on MVTec-AD to detect anomalies.

##### Top-Level Components
a. External Data
- MVTec-AD dataset (local filesystem directory for train/test/ground_truth)
b. Configuration Manager
- `configs/*.yaml` + CLI flags in `main.py`
c. Data Ingestion & Preprocessing
- `dataset.py` defines PyTorch Dataset/Transforms
d. Model Definition
- `fastflow.py` implements the FastFlow architecture
e. Training & Evaluation Engine
- `main.py` orchestrates train vs. eval, loads config, instantiates model & data loader, loops epochs, computes losses, AUROC
f. Utilities & Constants
- `utils.py`: helper functions (logging, metrics, checkpoint naming)
- `constants.py`: shared constants (e.g., normalization stats)
g. Checkpoint & Logging Storage
- `_fastflow_experiment_checkpoints/…` for model `.pt` files and logs

##### Interactions & Data Flow
1. **Startup**: CLI (`main.py`) reads a YAML config from `configs/` → passes hyperparameters.
2. **Data Loader**: Configuration → `dataset.py` builds PyTorch DataLoader → yields batches.
3. **Model Instantiation**: `fastflow.py` reads model-specific params from config → constructs flow layers.
4. **Training Loop**:
– Input batch → forward through FastFlow → compute anomaly score & loss → backward pass → optimizer step.
– Every N epochs: save checkpoint to filesystem.
5. **Evaluation Mode** (––eval flag): load checkpoint → run inference on test set → call `utils.py` to compute AUROC and localization maps.
6. **Output**: metrics printed to console and optionally saved.

##### Architectural Patterns & Principles
- **Modular separation**: config, data, model, training, utils
- **Single entry point**: `main.py` handles both train & eval modes
- **Config-driven**: YAML files drive hyperparameters, model selection (ResNet18, WideResNet, DeiT, CaiT)
- **Reproducibility**: checkpointing, seed control (via constants or utils)

## Diagram (Mermaid)

```mermaid
flowchart TD
    CLI["CLI: main.py"]:::cli
    Config["Config Manager\n(configs/*.yaml)"]:::core
    DataLoader["Data Loader\n(dataset.py)"]:::core
    Model["Model Definition\n(fastflow.py)"]:::core
    Trainer["Trainer/Evaluator\n(main.py loop)"]:::core
    Storage["Checkpoints/Logs\n(_fastflow_experiment_checkpoints/)"]:::storage
    Metrics["Metrics/AUROC"]:::external
    Utils["Utilities\n(utils.py)"]:::util
    Constants["Constants\n(constants.py)"]:::util
    ExtData["MVTec-AD Dataset\n(local FS)"]:::data
    PyTorch((PyTorch)):::external
    DocsReadme["README.md"]:::external
    DocsLicense["LICENSE"]:::external

    CLI -->|"reads config"| Config
    Config -->|"passes params"| DataLoader
    Config -->|"model params"| Model
    ExtData -->|"images"| DataLoader
    DataLoader -->|"batch"| Model
    DataLoader -->|"uses PyTorch DataLoader"| PyTorch
    Model -->|"forward/backward"| Trainer
    Model -->|"built on"| PyTorch
    Trainer -->|"saves checkpoint.pt"| Storage
    Trainer -->|"computes AUROC"| Metrics
    Trainer -->|"calls utils"| Utils
    Trainer -->|"reads constants"| Constants
    Storage -->|"uses utils/constants"| Utils
    DocsReadme --- CLI
    DocsLicense --- CLI

    click Config "https://github.com/gathierry/fastflow/tree/master/configs/"
    click DataLoader "https://github.com/gathierry/fastflow/blob/master/dataset.py"
    click Model "https://github.com/gathierry/fastflow/blob/master/fastflow.py"
    click Trainer "https://github.com/gathierry/fastflow/blob/master/main.py"
    click Utils "https://github.com/gathierry/fastflow/blob/master/utils.py"
    click Constants "https://github.com/gathierry/fastflow/blob/master/constants.py"
    click DocsReadme "https://github.com/gathierry/fastflow/blob/master/README.md"
    click DocsLicense "https://github.com/gathierry/fastflow/tree/master/LICENSE"

    classDef data fill:#CCE5FF,stroke:#3399FF
    classDef core fill:#CCFFCC,stroke:#33AA33
    classDef util fill:#F2F2F2,stroke:#AAAAAA
    classDef storage fill:#FFE5CC,stroke:#FF9933
    classDef cli fill:#FFF2CC,stroke:#CCCC33
    classDef external fill:#E8E8E8,stroke:#999999
```
