This repository contains the GDA-CLIP implementation for anomaly detection: an AnomalyCLIP branch is jointly trained with a Gaussian Discriminant Analysis (GDA) branch and layer-wise gated patch adapters.
GDA-CLIP/
├── train.py # Training entry point
├── test.py # Evaluation entry point
├── models/ # AnomalyCLIP, prompt learner, and GatePatch adapter
├── gda/ # GDA fitting, fusion, and patch-token transformation
├── data/ # Dataset loader, transforms, and metrics
├── utils/ # Losses, logging, and shared training helpers
├── scripts/ # Cross-dataset train/test scripts
└── requirements.txt
pip install -r requirements.txtPrepare each dataset root with its required meta.json file. Dataset images, masks, pretrained model weights, checkpoints, and experiment outputs are intentionally not included in this repository.
generate_dataset_json/ contains the original dataset-specific preprocessing and meta.json generation scripts for MVTec, VisA, BTAD, MPDD, SDD, DAGM, DTD, KSDD2, RSDD, and the supported medical datasets. Run the matching script after setting its dataset-root path. For example, mvtec.py and visa.py write <dataset_root>/meta.json; their __main__ runner paths are inherited from the source implementation and should be changed to your local dataset location before execution.
preprocess_ksdd_to_sdd.py is the exception: it already accepts explicit command-line paths, for example:
python generate_dataset_json/preprocess_ksdd_to_sdd.py \
--source_root /path/to/raw_ksdd \
--target_root /path/to/processed_sddpython train.py \
--train_data_path /path/to/dataset \
--save_path ./checkpoints/gda_clip \
--dataset visa \
--device cuda:0python test.py \
--data_path /path/to/dataset \
--checkpoint_path ./checkpoints/gda_clip/final_model.pth \
--save_path ./results/gda_clip \
--dataset visa \
--device cuda:0The scripts expose only dataset locations, dataset names, checkpoints, and output paths. All model and optimization settings use the defaults declared in train.py and test.py.
# Train on VisA (3 epochs) or MVTec (4 epochs), then evaluate.
bash scripts/train_transfer.sh visa /path/to/visa mvtec /path/to/mvtec
bash scripts/train_transfer.sh mvtec /path/to/mvtec visa /path/to/visa
# Evaluate an existing checkpoint.
bash scripts/evaluate.sh /path/to/dataset visa /path/to/final_model.pthThe training algorithm, defaults, loss construction, checkpoint keys, and evaluation logic are preserved from the original train_joint_gda_gatepatch_anomalyclip_final.py implementation. The files have only been relocated and their imports updated to make the released repository easier to navigate.