TL;DR
Test-Time Padding (TTP) is a lightweight, training-free defense mechanism that protects Vision-Language Models (like CLIP) from adversarial attacks during inference. By exploiting how spatial padding shifts the feature embeddings of adversarial versus clean images, TTP achieves near-perfect attack detection and uses single-step trainable padding to restore model attention, boosting robustness without degrading clean accuracy.
What Problem Does This Paper Address?
Large-scale Vision-Language Models (VLMs) like CLIP are powerful zero-shot classifiers, but they are notoriously fragile against adversarial perturbations—tiny, imperceptible pixel changes designed to fool the model. Traditional defenses rely on adversarial training, which is computationally expensive and requires retraining the model on labeled adversarial data.
To avoid this, recent research has pivoted to Test-Time Adaptation (TTA), which adapts the model on the fly during inference. However, existing test-time defenses typically apply the same adaptation strategy to all inputs uniformly. This creates a strict trade-off: modifications that neutralize adversarial attacks often degrade the model's accuracy on clean images. While some methods try to detect attacks first, their detection mechanisms are inaccurate and fail to generalize across different datasets and model architectures.
Methods
The authors introduce Test-Time Padding (TTP), a two-stage "detect-then-adapt" framework that operates entirely in the input pixel space without modifying the underlying model weights:
- Adversarial Detection via Similarity Shift: The authors discovered that adding spatial padding to an image causes a massive shift in the CLIP embedding space for adversarial images, but very little shift for clean images. By computing the cosine similarity between the embeddings of an image before and after fixed padding, TTP reliably flags adversarial inputs using a single, universal threshold ($\tau = 0.8$). Clean images bypass the defense entirely, preserving their original accuracy.
- Trainable Test-Time Padding: For images flagged as adversarial, TTP generates multiple augmented views and selects the most confident (lowest-entropy) ones. It then optimizes instance-specific padding parameters for a single gradient step using entropy minimization, which helps counteract the adversarial noise.
- Similarity-Aware Ensemble: Finally, TTP aggregates predictions across the augmented views using adaptive weights. These weights prioritize views that are closer to the padded adversarial embedding and farther from the unpadded adversarial embedding.
Datasets
The method was evaluated on eight fine-grained classification datasets spanning diverse domains:
- General Objects: Caltech101
- Animals: OxfordPets
- Plants: Flower102
- Vehicles: Cars, Aircraft
- Textures: DTD
- Satellite Imagery: EuroSAT
- Video Actions: UCF101
Evaluation was performed across three standard CLIP backbones: ViT-B/32, ViT-B/16, and ViT-L/14.
Key Insights and Ideas
- Padding as an Attention Restorer: Adversarial attacks typically succeed by malicious hijacking of the model's attention mechanisms, forcing it to focus on irrelevant background noise. Spatial padding breaks the rigid structure of these pixel-level perturbations, naturally guiding the model's attention back to the actual object.
- The Embedding Shift Phenomenon: Clean images are semantically robust to padding, meaning their high-level CLIP embeddings remain stable. Adversarial images, however, are highly fragile; padding disrupts the carefully crafted attack pattern, causing a dramatic drop in cosine similarity. This asymmetry provides a clean, dataset-agnostic detection signal.
- Input-Space Flexibility: Because TTP operates purely in the input pixel space rather than modifying prompt tokens or internal network layers, it is completely model-agnostic and can be seamlessly paired with other clean-focused TTA methods.
Key Results
- Near-Perfect Detection: TTP achieves nearly 100% detection accuracy across all datasets and backbones using a single, unified threshold, vastly outperforming previous methods like Test-Time Counterattack (TTC), which fluctuate wildly depending on the dataset.
- Superior Robustness: On the ViT-B/32 backbone under a 100-step PGD attack, TTP achieved an average adversarial accuracy of 39.7%, outperforming the state-of-the-art defense R-TPT by 4.4% and TTC by 32.9%.
- No Clean Accuracy Trade-off: Because clean images bypass adaptation, TTP preserves vanilla CLIP's zero-shot performance. Furthermore, when combined with test-time prompt tuning (TPT), TTP actually improves average clean accuracy from 62.6% to 63.2%.
Limitations and Future Work
- Padding Size Sensitivity: The performance of TTP is sensitive to the size of the padding. As shown in the paper's ablation studies, if the padding is too small, it fails to disrupt the attack; if it is too large, it destroys the original image layout and context.
- Computational Overhead: Although TTP only requires a single optimization step at test time, generating multiple augmented views and passing them through the image encoder introduces latency compared to a standard forward pass.
- Future Directions: The authors suggest extending this input-space padding defense to other multimodal architectures beyond CLIP, such as generative vision-language models (e.g., LLaVA).
Summary updated