DetectViT: Test-time Backdoor Detection for Vision Transformers via Inter-Head Attention Discrepancy
Organizations: South China University of Technology · University of California, San Diego · South China Normal University
Abstract
Vision Transformers (ViTs) now underpin the visual encoders of most multimodal models, but this success comes at a cost: relying on third-party pretrained checkpoints leaves these systems open to backdoor attacks. Existing test-time detection methods rely on either the predicted label or intermediate embeddings, limiting their applicability across diverse tasks and incurring substantial computational overhead. In this work, we study how backdoor triggers affect the multi-head attention mechanism of ViTs and identify two distinct anomalous patterns: , causing the attention to exhibit abnormally high inter-head spatial overlap, and , causing an abnormally large variance in the concentration of the attention distribution. Motivated by these findings, we propose , a test-time backdoor detection method that requires neither training data, model outputs, nor any prior knowledge of the trigger. DetectViT quantifies the two hijacking phenomena via an inter-head consistency score and an inter-head entropy variance score, with thresholds estimated from a small out-of-distribution (OOD) calibration set drawn from an arbitrary source. Across four representative attacks, diverse architectures (DeiT, CLIP, and LLaVA), and downstream tasks (image classification and captioning), DetectViT substantially outperforms all baselines with as few as 64--128 OOD calibration samples, achieving a true positive rate of and a false positive rate of on BadCLIP. Notably, it introduces negligible additional overhead by relying solely on attention weights already computed during inference. Code is available at: https://github.com/siquanhuang/DetectViT.
Figures & tables
| Defense | BadViT | TrojViT | BadCLIP | BadVision | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| TPR | FPR | AUC | TPR | FPR | AUC | TPR | FPR | AUC | TPR | FPR | AUC | |
| TeCo Liu et al. (2023) | 77.95 | 61.86 | 0.63 | 55.00 | 51.76 | 0.51 | 99.20 | 39.52 | 0.89 | - | - | - |
| Scale-up Guo et al. (2023) | 88.65 | 30.66 | 0.81 | 85.00 | 41.98 | 0.74 | 91.84 | 65.03 | 0.66 | - | - | - |
| PatchProc Doan et al. (2023) | 90.61 | 18.42 | 0.89 | 87.60 | 21.44 | 0.85 | 86.97 | 26.78 | 0.82 | - | - | - |
| DeDe Hou et al. (2025) | 63.97 | 23.29 | 0.78 | 50.20 | 25.80 | 0.72 | 55.12 | 27.97 | 0.69 | 7.00 | 29.68 | 0.50 |
| B-IDS Huang et al. (2026) | 80.79 | 9.73 | - | 5.00 | 10.84 | - | 93.24 | 16.88 | - | 96.00 | 15.96 | - |
| OOD Source | BadViT | TrojViT | BadCLIP | BadVision | |||||
|---|---|---|---|---|---|---|---|---|---|
| TPR | FPR | TPR | FPR | TPR | FPR | TPR | FPR | ||
| iNaturalist Horn and macaodha (2021) | 64 | 100.00 | 2.08 | 93.40 | 13.82 | 100.00 | 1.12 | 96.00 | 2.42 |
| 128 | 100.00 | 1.58 | 90.80 | 5.94 | 100.00 | 0.17 | 96.00 | 3.00 | |
| Food101 Bossard et al. (2014) | 64 | 100.00 | 10.37 | 94.60 | 20.33 | 100.00 | 7.52 | 96.00 | 3.42 |
| 128 | 100.00 | 9.39 | 92.60 | 11.81 | 100.00 | 8.52 | 96.00 | 2.95 | |
| Oxford-IIIT Pet Parkhi et al. (2012) | 64 | 100.00 | 5.48 | 88.60 | 9.04 | 100.00 | 8.90 | 97.00 | 5.16 |
| Defense | BadViT | TrojViT | BadCLIP | BadVision |
|---|---|---|---|---|
| TeCo Liu et al. (2023) | 2.5094 | 4.2920 | 1.8448 | - |
| Scale-Up Guo et al. (2023) | 0.0246 | 0.0261 | 0.0696 | - |
| PatchProc Doan et al. (2023) | 0.6686 | 0.6926 | 0.8257 | - |
| DeDe Hou et al. (2025) | 0.0206 | 0.0279 | 0.1034 | 0.6060 |
| B-IDS Huang et al. (2026) | 0.1734 | 0.1982 | 0.2560 | 3.3458 |
| DetectViT | 0.0124 | 0.0134 | 0.0163 | 0.0226 |
| Signal | BadViT | TrojViT | BadCLIP | BadVision | |||||
|---|---|---|---|---|---|---|---|---|---|
| TPR | FPR | TPR | FPR | TPR | FPR | TPR | FPR | ||
| ✓ | 100.00 | 0.54 | 90.40 | 4.22 | 100.00 | 0.02 | 0.00 | 0.00 | |
| ✓ | 80.35 | 1.06 | 44.20 | 1.76 | 89.52 | 0.16 | 96.00 | 3.00 | |
| ✓ | ✓ | 100.00 | 1.58 | 90.80 | 5.94 | 100.00 | 0.17 | 96.00 | 3.00 |
| Attack | Adaptive | TPR | FPR | ASR | CA |
|---|---|---|---|---|---|
| BadViT | w/o | 100.00 | 1.58 | 100.00 | 83.04 |
| 100.00 | 1.88 | 99.84 | 81.67 | ||
| + | 100.00 | 1.32 | 100.00 | 40.66 | |
| TrojViT | w/o | 90.80 | 5.94 | 100.00 | 85.17 |
| 90.80 | 5.94 | 100.00 | 85.17 | ||
| + | 90.80 | 5.94 | 100.00 | 85.17 |
Appendix figures & tables6 assets
Supplementary material from the paper’s appendix.
Appendix
| 1-sigma | 2-sigma | 3-sigma | Shapiro-Wilk | |
|---|---|---|---|---|
| Theoretical | 68.27 | 95.45 | 99.73 | 1.0000 |
| BadViT | 69.80 ( 1.53) | 94.65 ( 0.80) | 99.79 ( 0.06) | 0.9890 ( 0.0110) |
| TrojViT | 77.71 ( 9.44) | 94.66 ( 0.79) | 99.09 ( 0.64) | 0.9179 ( 0.0821) |
| BadCLIP | 69.06 ( 0.79) | 95.21 ( 0.24) | 99.61 ( 0.12) | 0.9973 ( 0.0027) |
| BadVision | 67.47 ( 0.80) | 95.32 ( 0.13) | 99.68 ( 0.05) | 0.9968 ( 0.0032) |
| Threshold | BadViT | TrojViT | BadCLIP | BadVision | ||||
|---|---|---|---|---|---|---|---|---|
| TPR | FPR | TPR | FPR | TPR | FPR | TPR | FPR | |
| Three-sigma | 100.00 | 1.58 | 90.80 | 5.94 | 100.00 | 0.17 | 96.00 | 3.00 |
| 99th percentile | 100.00 | 4.29 | 89.40 | 5.14 | 100.00 | 0.52 | 97.00 | 6.32 |
| Defense | BadEncoder | DRUPE | Low-Frequency | Label-Consistent | Blended-Optimized | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| TPR | FPR | AUC | TPR | FPR | AUC | TPR | FPR | AUC | TPR | FPR | AUC | TPR | FPR | AUC | |
| TeCo | 54.91 | 62.29 | 0.37 | 48.10 | 65.79 | 0.35 | 99.60 | 62.38 | 0.52 | 66.80 | 56.99 | 0.57 | 12.12 | 29.67 | 0.42 |
| Scale-up | 63.29 | 10.26 | 0.79 | 47.15 | 6.93 | 0.72 | 100.00 | 11.32 | 0.95 | 84.00 | 13.06 | 0.88 | 95.24 | 17.39 | 0.91 |
| PatchProc | 85.44 | 21.58 | 0.83 | 75.95 | 23.68 | 0.78 | 100.00 | 17.05 | 0.87 | 0.00 | 20.21 | 0.47 | 88.20 | 20.86 | 0.85 |
| DeDe | 99.21 | 23.13 | 0.97 | 97.94 | 27.37 | 0.91 | 0.00 | 30.33 | 0.00 | 78.60 | 25.52 | 0.83 | 73.44 | 23.98 | 0.81 |
| B-IDS | 96.04 | 17.12 | - | 95.09 | 22.11 | - | 0.00 | 26.00 | - | 14.60 | 25.47 | - | 44.96 | 12.78 | - |
Backdoor Detection
Backdoor detection finds hidden triggers or poisoned behavior in trained models before they can secretly redirect predictions or actions.
See all 60 papers