PatchGuard: Adversarially Robust Anomaly Detection and Localization through Vision Transformers and Pseudo Anomalies

Mojtaba Nafez, Amirhossein Koochakian, Arad Maleki, Jafar Habibi, Mohammad Hossein Rohban

Abstract

Anomaly Detection (AD) and Localization (AL) are critical for safety-sensitive applications like industrial inspection and medical diagnostics. However, these systems are highly vulnerable to adversarial attacks—imperceptible perturbations that can cause models to miss defects or generate false positives. We present PatchGuard, a novel framework that significantly enhances adversarial robustness. By combining a Vision Transformer (ViT) backbone with foreground-aware pseudo-anomaly generation and a unique attention-based regularization loss, PatchGuard achieves performance gains of over 50% in adversarial settings while remaining competitive on clean data.

Introduction

Most state-of-the-art AD and AL models are trained exclusively on "normal" data, learning to flag deviations as anomalies. This approach leaves a significant vulnerability: adversarial attacks. While robustness is well-studied in standard image classification, it remains a major gap in AD/AL research. Existing methods often ignore these attacks or fail to maintain localization accuracy under adversarial pressure. PatchGuard addresses this by focusing on robust feature representation and global context.

Methodology

PatchGuard is built upon three primary pillars designed to foster resilience against local perturbations:

  1. Foreground-Aware Pseudo-Anomaly Generation: To provide the model with realistic training examples, PatchGuard uses Grad-CAM to identify the "foreground" (the object of interest) in normal images. It then applies selective "hard" transformations (e.g., distortions) specifically to these regions, generating corresponding pixel-level masks. This ensures the model learns from near-distribution anomalies rather than simple noise.
  2. Vision Transformer (ViT) Backbone: The architecture utilizes a ViT to leverage global context. An integrated "Attention Discriminator" assigns anomaly scores to individual patches, facilitating precise localization.
  3. Attention Regularization Loss ($R$): The authors demonstrate that models with sparse attention (focusing on few tokens) are more susceptible to attack. PatchGuard introduces a loss function that encourages a higher Attention Degree, forcing the model to distribute its attention across more tokens. This makes it significantly harder for an attacker to flip a decision using small, local perturbations.
  4. Adversarial Training: The framework employs a min-max optimization strategy, defending against the strongest possible Projected Gradient Descent (PGD) attacks generated during training.
Overview of the PatchGuard framework, illustrating the foreground-aware pseudo-anomaly generation (1, 2) and the ViT-based training pipeline with attention regularization (3).

Experimental Setup

PatchGuard was evaluated across diverse industrial and medical benchmarks, including MVTec AD, VisA, BTAD, MPDD, and medical datasets such as Head-CT (hemorrhage detection) and BraTS2021 (brain tumor segmentation). Testing was conducted under both clean conditions and adversarial conditions (PGD-1000 attacks with a budget of $\epsilon = 8/255$).

Results and Discussion

PatchGuard demonstrates a massive leap in performance within adversarial environments compared to previous state-of-the-art (SOTA) methods.

  • Robustness Gains: In adversarial settings, PatchGuard outperformed existing methods by an average of 53.2% in Anomaly Detection and 68.5% in Anomaly Localization (AUROC).
  • MVTec AD Performance: While previous SOTA methods like ReContrast collapsed under attack, PatchGuard maintained a robust 71.1% AD AUROC and 73.8% AL AUROC.
  • Medical Reliability: On the BraTS2021 dataset, PatchGuard achieved an 81.0% robust AD AUROC, whereas competing methods typically fell below 10% under attack.
Visual comparison of anomaly localization before and after adversarial attacks. PatchGuard (Ours) maintains accurate localization while other SOTA methods fail to identify the defect after the attack.

The Attention Degree Connection

A key insight is the mathematical link between the number of tokens a ViT attends to and its robustness. By forcing the model to look at the "bigger picture" through attention regularization, PatchGuard becomes inherently more resilient to localized adversarial noise.

Conclusion and Limitations

PatchGuard establishes a new benchmark for robust anomaly detection. While there is a slight "robustness tax"—a minor decrease in clean data performance (~89% vs ~94% for non-robust models)—the gains in reliability are essential for high-stakes environments. Future work will focus on closing the clean-accuracy gap and reducing the computational overhead associated with adversarial training.

Summary updated

BibTeX
@inproceedings{nafez2025patchguard,
  author = {Mojtaba Nafez and Amirhossein Koochakian and Arad Maleki and Jafar Habibi and Mohammad Hossein Rohban},
  title = {{PatchGuard: Adversarially Robust Anomaly Detection and Localization through Vision Transformers and Pseudo Anomalies}},
  booktitle = {IEEE/CVF Conference on Computer Vision and Pattern Recognition},
  year = {2025},
  doi = {10.1109/cvpr52734.2025.01898},
}
Figure