# caoyunkang/adaclip architecture

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

- Interactive diagram: https://gitdiagram.com/caoyunkang/adaclip
- Repository: https://github.com/caoyunkang/adaclip

## Overview

AdaCLIP is a zero-shot image-anomaly detection system that adapts CLIP with learnable static and image-conditioned prompts, trained on annotated auxiliary datasets and applied to novel categories. Its main workflow is training or interactive inference: select datasets or upload an image, run the adapted model, and produce anomaly scores and localization maps. The graph also includes documented dataset preparation and evaluation capabilities. Relationships reflect the excerpts, SOURCE INDEX references, and README; unsampled implementation details are not assumed.

Main entry points: [`method/trainer.py`](https://github.com/caoyunkang/adaclip/blob/main/method/trainer.py) (Training and inference), [`train.py`](https://github.com/caoyunkang/adaclip/blob/main/train.py) (Training command), [`method/adaclip.py`](https://github.com/caoyunkang/adaclip/blob/main/method/adaclip.py) (Prompted anomaly model), [`dataset/base_dataset.py`](https://github.com/caoyunkang/adaclip/blob/main/dataset/base_dataset.py) (Image and mask loading)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_entry["Entry points"]
  node_train_cli["Training command<br/>[train.py]"]
  node_demo["Image demo<br/>[app.py]"]
end

subgraph group_data["Data pipeline"]
  node_preprocess["Dataset preparation"]
  node_dataset_factory["Dataset selection<br/>[__init__.py]"]
  node_dataset_adapters["Dataset adapters"]
  node_base_dataset["Image and mask loading<br/>[base_dataset.py]"]
end

subgraph group_model["AdaCLIP model"]
  node_adaclip["Prompted anomaly model<br/>[adaclip.py]"]
  node_clip["CLIP backbone<br/>[clip_model.py]"]
  node_transformer["Transformer layers<br/>[transformer.py]"]
  node_simple_tokenizer["Prompt tokenizer"]
  node_custom_clip["CLIP construction<br/>[custom_clip.py]"]
end

subgraph group_evaluation["Training and evaluation"]
  node_trainer["Training and inference<br/>[trainer.py]"]
  node_loss["Training losses<br/>[loss.py]"]
  node_metrics["Anomaly metrics<br/>[metrics.py]"]
  node_visualization["Result visualization<br/>[visualization.py]"]
  node_logger["Run logging<br/>[logger.py]"]
  node_training_tools["Training utilities<br/>[training_tools.py]"]
end

node_user(("User"))

node_user -->|"starts training"| node_train_cli
node_user -->|"submits image"| node_demo
node_train_cli -->|"selects datasets"| node_dataset_factory
node_train_cli -->|"runs training"| node_trainer
node_preprocess -->|"creates metadata"| node_base_dataset
node_dataset_factory -->|"selects adapter"| node_dataset_adapters
node_dataset_adapters -->|"inherits loading"| node_base_dataset
node_base_dataset -.->|"supplies batches"| node_trainer
node_demo -->|"loads and predicts"| node_trainer
node_trainer -->|"creates backbone"| node_custom_clip
node_trainer -->|"constructs and trains"| node_adaclip
node_trainer -->|"computes losses"| node_loss
node_trainer -->|"calculates scores"| node_metrics
node_trainer -->|"renders results"| node_visualization
node_trainer -.->|"records runs"| node_logger
node_trainer -.->|"uses utilities"| node_training_tools
node_adaclip -->|"adapts CLIP"| node_clip
node_adaclip -->|"tokenizes prompts"| node_simple_tokenizer
node_clip -->|"uses layers"| node_transformer
node_custom_clip -->|"builds model"| node_clip

click node_train_cli "https://github.com/caoyunkang/adaclip/blob/main/train.py"
click node_demo "https://github.com/caoyunkang/adaclip/blob/main/app.py"
click node_preprocess "https://github.com/caoyunkang/adaclip/tree/main/data_preprocess"
click node_dataset_factory "https://github.com/caoyunkang/adaclip/blob/main/dataset/__init__.py"
click node_dataset_adapters "https://github.com/caoyunkang/adaclip/tree/main/dataset"
click node_base_dataset "https://github.com/caoyunkang/adaclip/blob/main/dataset/base_dataset.py"
click node_trainer "https://github.com/caoyunkang/adaclip/blob/main/method/trainer.py"
click node_adaclip "https://github.com/caoyunkang/adaclip/blob/main/method/adaclip.py"
click node_clip "https://github.com/caoyunkang/adaclip/blob/main/method/clip_model.py"
click node_transformer "https://github.com/caoyunkang/adaclip/blob/main/method/transformer.py"
click node_simple_tokenizer "https://github.com/caoyunkang/adaclip/blob/main/method/simple_tokenizer.py"
click node_custom_clip "https://github.com/caoyunkang/adaclip/blob/main/method/custom_clip.py"
click node_loss "https://github.com/caoyunkang/adaclip/blob/main/loss.py"
click node_metrics "https://github.com/caoyunkang/adaclip/blob/main/tools/metrics.py"
click node_visualization "https://github.com/caoyunkang/adaclip/blob/main/tools/visualization.py"
click node_logger "https://github.com/caoyunkang/adaclip/blob/main/tools/logger.py"
click node_training_tools "https://github.com/caoyunkang/adaclip/blob/main/tools/training_tools.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_train_cli,node_demo,node_user toneBlue
class node_preprocess,node_dataset_factory,node_dataset_adapters,node_base_dataset toneAmber
class node_adaclip,node_clip,node_transformer,node_simple_tokenizer,node_custom_clip toneMint
class node_trainer,node_loss,node_metrics,node_visualization,node_logger,node_training_tools toneRose
```

## Components

### Entry points

- [Training command](https://github.com/caoyunkang/adaclip/blob/main/train.py): `train.py`
- [Image demo](https://github.com/caoyunkang/adaclip/blob/main/app.py): `app.py`

### Data pipeline

- [Dataset preparation](https://github.com/caoyunkang/adaclip/tree/main/data_preprocess): `data_preprocess`
- [Dataset selection](https://github.com/caoyunkang/adaclip/blob/main/dataset/__init__.py): `dataset/__init__.py`
- [Dataset adapters](https://github.com/caoyunkang/adaclip/tree/main/dataset): `dataset`
- [Image and mask loading](https://github.com/caoyunkang/adaclip/blob/main/dataset/base_dataset.py): `dataset/base_dataset.py`

### AdaCLIP model

- [Prompted anomaly model](https://github.com/caoyunkang/adaclip/blob/main/method/adaclip.py): `method/adaclip.py`
- [CLIP backbone](https://github.com/caoyunkang/adaclip/blob/main/method/clip_model.py): `method/clip_model.py`
- [Transformer layers](https://github.com/caoyunkang/adaclip/blob/main/method/transformer.py): `method/transformer.py`
- [Prompt tokenizer](https://github.com/caoyunkang/adaclip/blob/main/method/simple_tokenizer.py): `method/simple_tokenizer.py`
- [CLIP construction](https://github.com/caoyunkang/adaclip/blob/main/method/custom_clip.py): `method/custom_clip.py`

### Training and evaluation

- [Training and inference](https://github.com/caoyunkang/adaclip/blob/main/method/trainer.py): `method/trainer.py`
- [Training losses](https://github.com/caoyunkang/adaclip/blob/main/loss.py): `loss.py`
- [Anomaly metrics](https://github.com/caoyunkang/adaclip/blob/main/tools/metrics.py): `tools/metrics.py`
- [Result visualization](https://github.com/caoyunkang/adaclip/blob/main/tools/visualization.py): `tools/visualization.py`
- [Run logging](https://github.com/caoyunkang/adaclip/blob/main/tools/logger.py): `tools/logger.py`
- [Training utilities](https://github.com/caoyunkang/adaclip/blob/main/tools/training_tools.py): `tools/training_tools.py`

### Other components

- **User**

## Connections

- User → Training command: starts training
- User → Image demo: submits image (evidence: `app.py`)
- Training command → Dataset selection: selects datasets (evidence: `train.py`)
- Training command → Training and inference: runs training (evidence: `train.py`)
- Dataset preparation → Image and mask loading: creates metadata
- Dataset selection → Dataset adapters: selects adapter (evidence: `dataset/__init__.py`)
- Dataset adapters → Image and mask loading: inherits loading (evidence: `dataset/dtd.py`)
- Image and mask loading → Training and inference: supplies batches
- Image demo → Training and inference: loads and predicts (evidence: `app.py`)
- Training and inference → CLIP construction: creates backbone (evidence: `method/trainer.py`)
- Training and inference → Prompted anomaly model: constructs and trains (evidence: `method/trainer.py`)
- Training and inference → Training losses: computes losses (evidence: `method/trainer.py`)
- Training and inference → Anomaly metrics: calculates scores (evidence: `method/trainer.py`)
- Training and inference → Result visualization: renders results (evidence: `method/trainer.py`)
- Training and inference → Run logging: records runs
- Training and inference → Training utilities: uses utilities
- Prompted anomaly model → CLIP backbone: adapts CLIP (evidence: `method/adaclip.py`)
- Prompted anomaly model → Prompt tokenizer: tokenizes prompts (evidence: `method/adaclip.py`)
- CLIP backbone → Transformer layers: uses layers (evidence: `method/clip_model.py`)
- CLIP construction → CLIP backbone: builds model (evidence: `method/custom_clip.py`)
