Official implementation for R-TPT

August 28, 2025 ยท View on GitHub

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

[2025.08] We have corrected the robustness results on the Aircraft dataset and uploaded an updated (arXiv) version of the paper.

Our implementation is based on TPT and adversarial-attacks-pytorch.

Framework:

Prerequisites:

  • python == 3.8.5
  • pytorch == 1.12.1
  • torchvision == 0.13.1

Dataset:

Please follow CoOp and manually download the require datasets. Replace /path/to/dataset with your dataset folder root in below commands and check the path of json file in fewshot_datasets.py.

Test-time adaptation:

  1. Adaptation on clean dataset (R-TPT)
    python rtpt.py /path/to/dataset --test_sets DTD -a RN50 -b 64 --gpu 0 --ctx_init a_photo_of_a -p 50 --eps 0.0 --output_dir 'output_results/rtpt'
    
  2. Adaptation on adversarial dataset (R-TPT)
    python rtpt.py /path/to/dataset --test_sets DTD -a RN50 -b 64 --gpu 0 --ctx_init a_photo_of_a -p 50 --eps 1.0 --step 7 --output_dir 'output_results/rtpt'
    
  3. Test-time adaptation based on TeCoA pretrained encoder:

    If you want to load TeCoA pretrained encoder, please download the TeCoA from this repo and copy it into pretrain/tecoa folder, and the command is:

    python rtpt.py /path/to/dataset --test_sets DTD -a RN50 -b 64 --gpu 0 --ctx_init a_photo_of_a -p 50 --eps 1.0 --step 7 --output_dir 'output_results/rtpt' --load_tecoa 'RN50-eps1'