REF-CFA is a novel framework for label-free anomaly detection under domain shift. It addresses the challenge where conventional residuals are dominated by stochastic noise. By explicitly modeling the Residual-Evolution Field (REF), this method isolates the persistent, anomaly-aligned signal from the non-anomalous dynamics. A Cross-domain Field Alignment (CFA) module then aligns these field representations across domains, enabling a detector trained on a labeled Source Domain to be robustly reused on an unlabeled Target Domain.
-
Residual-Evolution Field (REF):
- Constructs a spatio-temporal vector field from diffusion residuals using three primitives:
-
Gradient Residual (
$R_t$ ): Captures amplitude discrepancies. -
Directional Offset (
$M_t$ ): Measures deviations orthogonal to the normal manifold flow. -
Path-Integrated Drift (
$Q_t$ ): Accumulates persistent drift over the reverse trajectory.
-
Gradient Residual (
-
Field Transformer: A lightweight sequence model that aggregates these primitives to produce Adaptive Statistics (Energy
$E$ , Non-Stationarity$NS$ , Directional Variability$DV$ ).
- Constructs a spatio-temporal vector field from diffusion residuals using three primitives:
-
Cross-domain Field Alignment (CFA):
- Enables domain reuse via Temporal Alignment, Second-order Feature Alignment, and Directional Subspace Alignment.
-
Supervised Field Detector:
- A discriminative head (
$f_\psi$ ) trained on the Source Domain using the extracted REF features.
- A discriminative head (
It is recommended to use Anaconda to create a virtual environment:
conda create -n refcfa python=3.8
conda activate refcfa
# Install PyTorch (Adjust CUDA version as needed)
pip install torch torchvision --index-url https://download.pytorch.org/whl/cu118
# Install dependencies
pip install numpy opencv-python pyyaml matplotlib scipy scikit-learn tqdm omegaconfPlease ensure your data directory structure matches the following layouts and update the root path in your config files.
Standard MVTec structure:
/data/mvtec/
├── bottle
│ ├── train
│ │ └── good
│ └── test
│ ├── good
│ ├── broken_large
│ └── ...
Structure required for VisA:
/data/visa/
├── candle
│ ├── Data
│ │ ├── Images
│ │ │ ├── Normal
│ │ │ └── Anomaly
│ │ └── Masks
│ │ └── Anomaly
DAGM structure with _def suffix for defective samples:
/data/dagm/
├── Class1
│ ├── 0001.png
│ └── ... (Normal samples)
├── Class1_def
│ ├── 0001.png
│ └── 0001_mask.png (Defective samples and masks)
Train the diffusion score networks (
# Train Source Score Network (e.g., MVTec Bottle)
python tools/train_net.py --config-file configs/experiments/mvtec/source_bottle.yaml
# Train Target Score Network (e.g., MVTec Cable)
# Note: Target training is unsupervised (on unlabeled mixture)
python tools/train_net.py --config-file configs/experiments/mvtec/target_cable.yamlConstruct the Residual-Evolution Field (REF) on the source domain and train the Supervised Field Detector (
# Train the Field Transformer and Detector on Source
python tools/analyze_net.py \
--config configs/experiments/mvtec/source_bottle.yaml \
--checkpoint output/MVTec_Source_Bottle_Train/checkpoint_epoch_2000.pth \
--train_localization- This step learns to isolate anomaly-aligned components (
$R, M, Q$ ) and optimizes the supervised loss$\mathcal{L}_S$ .
Apply Cross-domain Field Alignment (CFA) to align the target REF to the source space, then reuse the detector for inference.
# Run Inference on Target Domain using Source Detector + CFA
python tools/analyze_net.py \
--config configs/experiments/mvtec/target_cable.yaml \
--checkpoint output/MVTec_Source_Bottle_Train/checkpoint_epoch_2000.pth \
--run_supervised_localization- This applies Temporal, Second-order, and Directional alignment before prediction.
Visualize the evolution of the residual primitives (
python tools/analyze_net.py \
--config configs/experiments/visa/target_pcb2.yaml \
--checkpoint output/VisA_Source_Candle_Train/checkpoint_epoch_2000.pth \
--analyze_residuals- Results saved in
output/vis_ref_dynamics/.
Evaluate the Adaptive Statistics (
python tools/analyze_net.py \
--config configs/experiments/mvtec/target_cable.yaml \
--checkpoint output/MVTec_Source_Bottle_Train/checkpoint_epoch_2000.pth \
--analyze_transformerConfigurations are located in configs/ and use an inheritance mechanism.
base/runtime.yaml: Global training parameters.base/models.yaml: Model architecture (UNet, Diffusion Steps).experiments/<dataset>/<config>.yaml: Experiment-specific configurations.
| Component | Description | Output Path |
|---|---|---|
| Score Network |
|
output/<ExpName>/checkpoint_epoch_X.pth |
| REF Detector | Field Transformer & Head weights | output/.../supervised_segmenter.pth |
| Inference | Anomaly Maps |
output/vis_supervised/ |
| Analysis | REF Dynamics ( |
output/vis_ref_dynamics/ |
If you use this code, please cite our paper:
@article{refcfa2026,
title={Anomaly-Related Residual Fields for Cross-domain Anomaly Detection},
author={Anonymous},
journal={CVPR Submission},
year={2026}
}