# sun-9545sunoj/few-shot-anomaly-detection architecture

> Architecture diagram of the GitHub repository sun-9545sunoj/few-shot-anomaly-detection, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-08). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/sun-9545sunoj/few-shot-anomaly-detection
- Repository: https://github.com/sun-9545sunoj/few-shot-anomaly-detection

## Overview

This repository is a research and demonstration system for few-shot, unsupervised industrial defect detection. Its main workflow extracts features from normal support images, builds a reference model, scores inspection images, converts patch scores into heat maps and image scores, then applies a threshold fitted on held-out normals to produce PASS/REJECT decisions. Experiment sweeps measure accuracy and cost; reporting, stress analysis, failure analysis, and the single-image demo are additional product capabilities. The tree omits the documented data package, so dataset-loading internals are not mapped.

Main entry points: [`src/experiment.py`](https://github.com/sun-9545sunoj/few-shot-anomaly-detection/blob/main/src/experiment.py) (Experiment sweep), [`src/methods/base.py`](https://github.com/sun-9545sunoj/few-shot-anomaly-detection/blob/main/src/methods/base.py) (Method interface), [`src/demo.py`](https://github.com/sun-9545sunoj/few-shot-anomaly-detection/blob/main/src/demo.py) (Single-image demo), [`src/decision.py`](https://github.com/sun-9545sunoj/few-shot-anomaly-detection/blob/main/src/decision.py) (Threshold decision)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_inspection["Detection workflow"]
  node_postprocess["Map post-processing<br/>[postprocess.py]"]
  node_decision["Threshold decision<br/>[decision.py]"]
end

subgraph group_methods["Feature and methods"]
  node_backbone["WideResNet features<br/>[backbone.py]"]
  node_clip["CLIP features<br/>[clip_backbone.py]"]
  node_method_interface["Method interface<br/>[base.py]"]
  node_patchcore["PatchCore<br/>[patchcore.py]"]
  node_padim["PaDiM<br/>[padim.py]"]
  node_winclip["WinCLIP<br/>[winclip.py]"]
end

subgraph group_evaluation["Experiment analysis"]
  node_experiment["Experiment sweep<br/>[experiment.py]"]
  node_metrics["Detection metrics<br/>[metrics.py]"]
  node_profiling["Cost profiling<br/>[profiling.py]"]
  node_stress["Support-set stress<br/>[stress.py]"]
  node_failures["Failure analysis<br/>[failures.py]"]
  node_report["Result reporting<br/>[report.py]"]
  node_tables["Result tables<br/>[tables.py]"]
  node_inventory["Dataset integrity check<br/>[inventory.py]"]
  node_baseline["Smoke baseline<br/>[baseline.py]"]
end

subgraph group_presentation["Inspection outputs"]
  node_qualitative["Qualitative grids<br/>[qualitative.py]"]
  node_demo["Single-image demo<br/>[demo.py]"]
end

subgraph group_configuration["Run configuration"]
  node_config["Validated run config<br/>[schema.py]"]
end

node_operator(("Engineer or inspector"))
node_datasets[("MVTec AD / VisA")]

node_operator -->|"configures sweep"| node_experiment
node_datasets -->|"supplies images"| node_experiment
node_operator -->|"inspects image"| node_demo
node_datasets -->|"supplies images"| node_demo
node_config -->|"configures run"| node_experiment
node_experiment -->|"extracts features"| node_backbone
node_experiment -->|"extracts features"| node_clip
node_experiment -->|"builds method"| node_method_interface
node_method_interface -->|"dispatches to"| node_patchcore
node_method_interface -->|"dispatches to"| node_padim
node_method_interface -->|"dispatches to"| node_winclip
node_experiment -->|"processes maps"| node_postprocess
node_experiment -->|"fits threshold"| node_decision
node_experiment -->|"computes metrics"| node_metrics
node_experiment -->|"profiles cost"| node_profiling
node_experiment -->|"measures stress"| node_stress
node_failures -->|"reuses runs"| node_experiment
node_failures -->|"analyzes causes"| node_stress
node_report -->|"builds tables"| node_tables
node_tables -->|"summarizes metrics"| node_metrics
node_tables -->|"summarizes decisions"| node_decision
node_qualitative -->|"reads results"| node_experiment
node_qualitative -->|"renders maps"| node_postprocess
node_qualitative -->|"shows verdicts"| node_decision
node_demo -->|"applies threshold"| node_decision
node_demo -->|"scores maps"| node_postprocess
node_demo -->|"builds method"| node_method_interface
node_inventory -->|"checks integrity"| node_datasets
node_baseline -->|"measures baseline"| node_metrics

click node_config "https://github.com/sun-9545sunoj/few-shot-anomaly-detection/blob/main/src/config/schema.py"
click node_experiment "https://github.com/sun-9545sunoj/few-shot-anomaly-detection/blob/main/src/experiment.py"
click node_backbone "https://github.com/sun-9545sunoj/few-shot-anomaly-detection/blob/main/src/models/backbone.py"
click node_clip "https://github.com/sun-9545sunoj/few-shot-anomaly-detection/blob/main/src/models/clip_backbone.py"
click node_method_interface "https://github.com/sun-9545sunoj/few-shot-anomaly-detection/blob/main/src/methods/base.py"
click node_patchcore "https://github.com/sun-9545sunoj/few-shot-anomaly-detection/blob/main/src/methods/patchcore.py"
click node_padim "https://github.com/sun-9545sunoj/few-shot-anomaly-detection/blob/main/src/methods/padim.py"
click node_winclip "https://github.com/sun-9545sunoj/few-shot-anomaly-detection/blob/main/src/methods/winclip.py"
click node_postprocess "https://github.com/sun-9545sunoj/few-shot-anomaly-detection/blob/main/src/postprocess.py"
click node_decision "https://github.com/sun-9545sunoj/few-shot-anomaly-detection/blob/main/src/decision.py"
click node_metrics "https://github.com/sun-9545sunoj/few-shot-anomaly-detection/blob/main/src/metrics.py"
click node_profiling "https://github.com/sun-9545sunoj/few-shot-anomaly-detection/blob/main/src/profiling.py"
click node_stress "https://github.com/sun-9545sunoj/few-shot-anomaly-detection/blob/main/src/stress.py"
click node_failures "https://github.com/sun-9545sunoj/few-shot-anomaly-detection/blob/main/src/failures.py"
click node_report "https://github.com/sun-9545sunoj/few-shot-anomaly-detection/blob/main/src/report.py"
click node_tables "https://github.com/sun-9545sunoj/few-shot-anomaly-detection/blob/main/src/tables.py"
click node_qualitative "https://github.com/sun-9545sunoj/few-shot-anomaly-detection/blob/main/src/qualitative.py"
click node_demo "https://github.com/sun-9545sunoj/few-shot-anomaly-detection/blob/main/src/demo.py"
click node_inventory "https://github.com/sun-9545sunoj/few-shot-anomaly-detection/blob/main/src/inventory.py"
click node_baseline "https://github.com/sun-9545sunoj/few-shot-anomaly-detection/blob/main/src/baseline.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_postprocess,node_decision toneBlue
class node_backbone,node_clip,node_method_interface,node_patchcore,node_padim,node_winclip,node_datasets toneAmber
class node_experiment,node_metrics,node_profiling,node_stress,node_failures,node_report,node_tables,node_inventory,node_baseline toneMint
class node_qualitative,node_demo toneRose
class node_config,node_operator toneIndigo
```

## Components

### Detection workflow

- [Map post-processing](https://github.com/sun-9545sunoj/few-shot-anomaly-detection/blob/main/src/postprocess.py): `src/postprocess.py`
- [Threshold decision](https://github.com/sun-9545sunoj/few-shot-anomaly-detection/blob/main/src/decision.py): `src/decision.py`

### Feature and methods

- [WideResNet features](https://github.com/sun-9545sunoj/few-shot-anomaly-detection/blob/main/src/models/backbone.py): `src/models/backbone.py`
- [CLIP features](https://github.com/sun-9545sunoj/few-shot-anomaly-detection/blob/main/src/models/clip_backbone.py): `src/models/clip_backbone.py`
- [Method interface](https://github.com/sun-9545sunoj/few-shot-anomaly-detection/blob/main/src/methods/base.py): `src/methods/base.py`
- [PatchCore](https://github.com/sun-9545sunoj/few-shot-anomaly-detection/blob/main/src/methods/patchcore.py): `src/methods/patchcore.py`
- [PaDiM](https://github.com/sun-9545sunoj/few-shot-anomaly-detection/blob/main/src/methods/padim.py): `src/methods/padim.py`
- [WinCLIP](https://github.com/sun-9545sunoj/few-shot-anomaly-detection/blob/main/src/methods/winclip.py): `src/methods/winclip.py`

### Experiment analysis

- [Experiment sweep](https://github.com/sun-9545sunoj/few-shot-anomaly-detection/blob/main/src/experiment.py): `src/experiment.py`
- [Detection metrics](https://github.com/sun-9545sunoj/few-shot-anomaly-detection/blob/main/src/metrics.py): `src/metrics.py`
- [Cost profiling](https://github.com/sun-9545sunoj/few-shot-anomaly-detection/blob/main/src/profiling.py): `src/profiling.py`
- [Support-set stress](https://github.com/sun-9545sunoj/few-shot-anomaly-detection/blob/main/src/stress.py): `src/stress.py`
- [Failure analysis](https://github.com/sun-9545sunoj/few-shot-anomaly-detection/blob/main/src/failures.py): `src/failures.py`
- [Result reporting](https://github.com/sun-9545sunoj/few-shot-anomaly-detection/blob/main/src/report.py): `src/report.py`
- [Result tables](https://github.com/sun-9545sunoj/few-shot-anomaly-detection/blob/main/src/tables.py): `src/tables.py`
- [Dataset integrity check](https://github.com/sun-9545sunoj/few-shot-anomaly-detection/blob/main/src/inventory.py): `src/inventory.py`
- [Smoke baseline](https://github.com/sun-9545sunoj/few-shot-anomaly-detection/blob/main/src/baseline.py): `src/baseline.py`

### Inspection outputs

- [Qualitative grids](https://github.com/sun-9545sunoj/few-shot-anomaly-detection/blob/main/src/qualitative.py): `src/qualitative.py`
- [Single-image demo](https://github.com/sun-9545sunoj/few-shot-anomaly-detection/blob/main/src/demo.py): `src/demo.py`

### Run configuration

- [Validated run config](https://github.com/sun-9545sunoj/few-shot-anomaly-detection/blob/main/src/config/schema.py): `src/config/schema.py`

### Other components

- **Engineer or inspector**
- **MVTec AD / VisA**

## Connections

- Engineer or inspector → Experiment sweep: configures sweep (evidence: `README.md`)
- MVTec AD / VisA → Experiment sweep: supplies images (evidence: `README.md`)
- Engineer or inspector → Single-image demo: inspects image (evidence: `README.md`)
- MVTec AD / VisA → Single-image demo: supplies images (evidence: `README.md`)
- Validated run config → Experiment sweep: configures run (evidence: `src/experiment.py`)
- Experiment sweep → WideResNet features: extracts features (evidence: `src/experiment.py`)
- Experiment sweep → CLIP features: extracts features (evidence: `src/experiment.py`)
- Experiment sweep → Method interface: builds method (evidence: `src/experiment.py`)
- Method interface → PatchCore: dispatches to (evidence: `src/methods/base.py`)
- Method interface → PaDiM: dispatches to (evidence: `src/methods/base.py`)
- Method interface → WinCLIP: dispatches to (evidence: `src/methods/base.py`)
- Experiment sweep → Map post-processing: processes maps (evidence: `src/experiment.py`)
- Experiment sweep → Threshold decision: fits threshold (evidence: `src/experiment.py`)
- Experiment sweep → Detection metrics: computes metrics (evidence: `src/experiment.py`)
- Experiment sweep → Cost profiling: profiles cost (evidence: `src/experiment.py`)
- Experiment sweep → Support-set stress: measures stress (evidence: `src/experiment.py`)
- Failure analysis → Experiment sweep: reuses runs (evidence: `src/failures.py`)
- Failure analysis → Support-set stress: analyzes causes (evidence: `src/failures.py`)
- Result reporting → Result tables: builds tables (evidence: `src/report.py`)
- Result tables → Detection metrics: summarizes metrics (evidence: `src/tables.py`)
- Result tables → Threshold decision: summarizes decisions (evidence: `src/tables.py`)
- Qualitative grids → Experiment sweep: reads results (evidence: `src/qualitative.py`)
- Qualitative grids → Map post-processing: renders maps (evidence: `src/qualitative.py`)
- Qualitative grids → Threshold decision: shows verdicts (evidence: `src/qualitative.py`)
- Single-image demo → Threshold decision: applies threshold (evidence: `src/demo.py`)
- Single-image demo → Map post-processing: scores maps (evidence: `src/demo.py`)
- Single-image demo → Method interface: builds method (evidence: `src/demo.py`)
- Dataset integrity check → MVTec AD / VisA: checks integrity (evidence: `README.md`)
- Smoke baseline → Detection metrics: measures baseline (evidence: `src/baseline.py`)
