R-TPT: Improving Adversarial Robustness of Vision-Language Models through Test-Time Prompt Tuning

Lijun Sheng, Jian Liang, Zilei Wang, Ran He · DOI

Abstract

Vision-Language Models (VLMs) like CLIP have become foundational in computer vision, yet they remain highly vulnerable to adversarial attacks. Traditional adversarial defense mechanisms, such as adversarial training, are computationally expensive and require large-scale labeled datasets, limiting their practical utility for zero-shot applications. This paper introduces Robust Test-Time Prompt Tuning (R-TPT), an inference-stage defense that requires no labeled data or retraining. By refining the optimization objective to focus on pointwise entropy and implementing a reliability-weighted ensemble of augmented views, R-TPT significantly enhances adversarial robustness on the fly. Experimental results show that R-TPT can boost adversarial accuracy from near-zero to over 45% across various benchmarks while maintaining or improving clean-image performance.

Introduction

The widespread adoption of open-source Vision-Language Models (VLMs) has created a unique security challenge. Because many applications rely on the same pre-trained checkpoints (e.g., CLIP), attackers can easily craft white-box adversarial examples—images containing imperceptible noise designed to trigger high-confidence misclassifications.

Existing defenses primarily rely on adversarial training (e.g., TeCoA), which involves retraining the model on adversarial examples. This approach is often impractical due to the massive computational requirements and the loss of the model's original zero-shot flexibility. R-TPT addresses this by shifting the defense to the inference stage, utilizing test-time adaptation to neutralize attacks without prior knowledge of the specific task or access to training data.

Methodology: Robust Test-Time Prompt Tuning

R-TPT builds upon the framework of Test-Time Prompt Tuning (TPT). In standard TPT, a single test image is augmented multiple times, and the model's text prompt (e.g., "a photo of a [CLASS]") is optimized to minimize the entropy of the predictions. R-TPT introduces two pivotal modifications to ensure this process is robust against adversarial perturbations.

1. Refining Marginal Entropy Minimization

Standard TPT minimizes marginal entropy, which can be decomposed into two components: the average of pointwise entropies and the Kullback–Leibler (KL) divergence among augmented views. The KL divergence term essentially forces all augmented views to reach a consensus with the mean prediction.

The authors identify a "KL Divergence Trap": if the input is adversarial, the mean prediction is likely already poisoned. Forcing agreement via KL divergence pulls relatively "clean" augmented views toward the misleading adversarial prediction, effectively amplifying the attack. R-TPT resolves this by completely removing the KL divergence term and optimizing solely for pointwise entropy, allowing the model to find stable prompts without being forced into a poisoned consensus.

2. Reliability-based Weighted Ensembling

Rather than treating all augmented views equally, R-TPT introduces a weighting mechanism based on feature reliability. The method calculates the cosine similarity of each augmented view's feature representation to its nearest neighbors within the batch.

Adversarial noise typically pushes features into isolated, low-density regions of the latent space. By identifying these outliers, R-TPT assigns lower weights to unreliable views (likely dominated by noise or poor cropping) and higher weights to views that cluster naturally. This ensures the final prediction is driven by the most visually coherent representations.

Experimental Evaluation

The effectiveness of R-TPT was evaluated across a diverse set of benchmarks, including fine-grained classification (Caltech101, Flowers102, etc.) and ImageNet variants (ImageNet-A, -V2, -R, -S). The model was tested against several attack algorithms, including PGD, CW, DeepFool, and FGSM.

Key Performance Metrics

  • Fine-Grained Classification: On a ResNet50 backbone, vanilla CLIP's performance collapses to 0.4% accuracy under PGD attacks. R-TPT recovers this to 45.2%. Notably, it also slightly improves clean accuracy from 53.2% to 53.7%.
  • ImageNet Robustness: Under attack, CLIP's accuracy drops to 0.1%. R-TPT restores this to 47.7%, outperforming standard ensemble methods by a margin of 7.6%.
  • Cross-Architecture Versatility: The defense remains effective across various backbones, including ViT-B/16 and ViT-L/14, demonstrating that the method is not architecture-specific.

Discussion and Insights

The success of R-TPT highlights a critical realization in test-time adaptation: standard consensus-based objectives can be counterproductive in the presence of adversarial noise. By treating outliers as a signal rather than just noise, and by avoiding the optimization conflicts inherent in marginal entropy minimization, R-TPT achieves a level of robustness that competes with computationally intensive train-time methods.

Limitations

The primary trade-off for this robustness is inference latency. R-TPT requires multiple augmentations (typically 64) and an optimization step per image, resulting in a processing time of approximately 0.58 seconds per sample. While reducing the number of views to 16 can lower this to 0.20 seconds with minimal robustness loss, it remains slower than standard zero-shot inference. Additionally, while R-TPT maintains clean accuracy, it may occasionally underperform standard TPT on unperturbed data due to its aggressive outlier rejection.

Conclusion

R-TPT represents a significant step forward in securing Vision-Language Models. It demonstrates that adversarial robustness can be achieved at the prompt level during inference, providing a flexible and data-efficient alternative to traditional adversarial training. Future work will likely focus on reducing the computational overhead to make test-time prompt tuning viable for real-time applications.

Summary updated

BibTeX
@inproceedings{sheng2025r,
  author = {Lijun Sheng and Jian Liang and Zilei Wang and Ran He},
  title = {{R-TPT: Improving Adversarial Robustness of Vision-Language Models through Test-Time Prompt Tuning}},
  booktitle = {IEEE/CVF Conference on Computer Vision and Pattern Recognition},
  year = {2025},
  doi = {10.1109/cvpr52734.2025.02788},
}
Figure