DetectViT: Test-time Backdoor Detection for Vision Transformers via Inter-Head Attention Discrepancy

Siquan Huang, Yijiang Li, Xingfu Yan, Ningzhi Gao, Leyu Shi, Ying GaoarXiv 2026 · arXiv

TL;DR

DetectViT is a lightweight, test-time defense that identifies backdoored samples in Vision Transformers (ViTs) by analyzing internal "attention hijacking" patterns. Unlike previous methods, it requires zero training data, no knowledge of the trigger, and works across diverse tasks like image classification and captioning with negligible computational overhead.

What Problem Does This Paper Address?

Vision Transformers (ViTs) are the backbone of modern AI, from image classifiers to Large Vision-Language Models (LVLMs) like LLaVA. However, because training these models from scratch is prohibitively expensive, most developers rely on third-party pre-trained weights. This creates a massive security hole: an adversary can inject a "backdoor" into these weights that remains dormant until a specific trigger (like a small patch or a faint overlay) appears in the input.

Existing defenses have major drawbacks. Some require access to the original training data (which users often don't have), while others only work for simple classification tasks. Many also rely on "input augmentation"—creating dozens of copies of an image to see if the model's output changes—which is slow and computationally expensive. DetectViT aims to solve these issues by looking "under the hood" at the model's internal attention mechanism during a single forward pass.

Methods

The researchers discovered that backdoor triggers fundamentally disrupt how ViTs process information. In a clean image, different "attention heads" naturally focus on different parts of the scene to capture diverse features. Backdoor triggers, however, cause attention hijacking, which manifests in two ways:

  1. Complete Attention Hijacking: Prominent triggers (like visible patches) force almost all attention heads to converge on the trigger region.
  2. Partial Attention Hijacking: Stealthier triggers (like blended overlays) affect heads unevenly, causing some to focus intensely while others remain diffuse.
Figure 1: Visualization of the head attention distribution over the CLS token of clean/backdoor images.

DetectViT quantifies these anomalies using two scores:

  • Inter-Head Consistency ($S_{cons}$): Measures how much the spatial attention maps of different heads overlap. High overlap suggests complete hijacking.
  • Inter-Head Entropy Variance ($S_{var}$): Measures the disparity in how "focused" or "spread out" the attention is across different heads. High variance suggests partial hijacking.

The method uses a tiny "calibration set" (as few as 64–128 images) from an arbitrary, unrelated dataset to establish a baseline for "normal" attention. If a test image produces scores significantly higher than this baseline, it is flagged as a backdoor sample.

Figure 2: Overview of our proposed DetectViT.

Datasets

The authors evaluated DetectViT against four representative attacks (BadViT, TrojViT, BadCLIP, and BadVision) across several architectures (DeiT, CLIP, and LLaVA).

To prove the method's flexibility, they used seven different Out-of-Distribution (OOD) datasets for calibration, including iNaturalist, Food101, Oxford-IIIT Pet, Places365, STL-10, Flowers102, and PASCAL VOC. They found that the choice of calibration data barely affected performance—you can use almost any set of clean images to set your thresholds.

Key Insights and Ideas

  • Attention Diversity is a Security Feature: The inherent "disagreement" between attention heads in a healthy ViT is what makes backdoors stand out. When a trigger forces heads to agree (or disagree in a specific, high-variance way), it leaves a distinct fingerprint.
  • Zero-In-Distribution Requirement: One of the most surprising findings is that you don't need "clean" versions of your specific task data to calibrate the detector. General image data is enough to define what "normal" transformer behavior looks like.
  • Negligible Overhead: Because DetectViT uses attention weights that the model already calculates during inference, the additional math (cosine similarity and entropy) adds almost zero latency. This makes it ideal for real-time applications.

Key Results

DetectViT consistently outperformed state-of-the-art baselines across all tested attacks and tasks:

  • Near-Perfect Detection: On the BadCLIP attack, it achieved a 100% True Positive Rate (TPR) with a False Positive Rate (FPR) of only 0.17%.
  • Multimodal Success: Unlike label-based defenses, DetectViT successfully detected backdoors in LLaVA (an image-to-text model) with a 96% TPR.
  • Speed: It is significantly faster than augmentation-based methods. For example, on the TrojViT attack, DetectViT was over 300x faster than the TeCo baseline.
Table 1: Comparison of backdoor sample detection methods. TPR and FPR are reported as percent- ages (%). AUC is reported on a [0, 1] scale. We bold the best value.

Limitations and Future Work

  • OOD-as-Trigger Attacks: The authors note that if a backdoor is designed to trigger on natural features of OOD data (rather than a crafted pattern), it might be harder to distinguish from legitimate OOD samples.
  • Architecture Specificity: The current implementation relies on the CLS token (a special token used by many ViTs to summarize image info). Future work could extend this to "gap-based" ViTs that use global average pooling instead of a CLS token.
  • Adaptive Attacks: While the paper shows DetectViT is robust to some adaptive adversaries, highly sophisticated attackers might eventually find ways to "smooth out" attention distributions while still maintaining attack success.

Summary updated

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: $\textit{complete attention hijacking}$, causing the attention to exhibit abnormally high inter-head spatial overlap, and $\textit{partial attention hijacking}$, causing an abnormally large variance in the concentration of the attention distribution. Motivated by these findings, we propose $\textbf{DetectViT}$, 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 $100\%$ and a false positive rate of $0.17\%$ 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.
BibTeX
@misc{huang2026detectvit,
  author = {Siquan Huang and Yijiang Li and Xingfu Yan and Ningzhi Gao and Leyu Shi and Ying Gao},
  title = {{DetectViT: Test-time Backdoor Detection for Vision Transformers via Inter-Head Attention Discrepancy}},
  year = {2026},
  eprint = {2611.23770},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2611.23770},
}
Figure