Official PyTorch implementation of the CVPR 2026 paper: "When CLIP Sees More, It Fights Back Harder: Multi-View Guided Adaptive Counterattacks for Test-Time Adversarial Robustness".
Vision-language models like CLIP exhibit remarkable zero-shot capabilities but remain highly vulnerable to adversarial perturbations. We propose MAC (Multi-view guided Adaptive Counterattack), a tuning-free test-time defense framework.
MAC dynamically improves CLIP's robustness by:
- Constructing multiple augmented views of an input image.
- Performing multi-view guided counterattacks to steer corrupted embeddings toward reliable states.
- Adaptively scaling the counterattack intensity using a corruption degree via corruption-aware soft weighting.
- Aggregating the predictions from each counterattacked view to yield a robust final prediction.
MAC achieves state-of-the-art adversarial robustness while remaining extremely fast and memory-efficient.
We recommend setting up a virtual environment. Below are the core dependencies required to run the code:
torch(>= 2.0.0)torchvisionnumpy==1.26.4scipy==1.14.1Pillow==11.1.0ftfy==6.3.1h5py==3.13.0tqdm==4.67.1
Please follow CoOp and manually download the required datasets.
Place the downloaded datasets into the ../data directory and make sure to check/update the path of the json file in data/fewshot_datasets.py.
We provide a bash script (mac_test.sh) to reproduce our test-time adversarial defense evaluations.
To execute the tests, simply run the bash script from the root of your project:
bash mac_test.shInside mac_test.sh, you can modify several parameters to test different setups:
ARCH: The CLIP backbone architecture (e.g.,"ViT-B/32","RN50").TEST_ATTACK_TYPE: The adversarial attack method evaluated against (e.g.,"pgd").TEST_EPS,TEST_NUMSTEPS,TEST_STEPSIZE: Parameters defining the strength of the incoming adversarial attack.MAC_EPS,MAC_NUMSTEPS: Hyperparameters defining the budget and iterations of the MAC defense counterattack.NUM_VIEWS: The number of augmented views generated by MAC.TEST_SET: A string listing the datasets to evaluate on (e.g.,"Caltech101 DTD Flower102").
The script will automatically create a timestamped output directory under MAC_results/ and save your evaluation results there.
If you find our work helpful for your research, please consider citing our paper:
@inproceedings{kim2026mac,
title={When CLIP Sees More, It Fights Back Harder: Multi-View Guided Adaptive Counterattacks for Test-Time Adversarial Robustness},
author={Kim, Sunoh and Um, Daeho},
booktitle={Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition (CVPR)},
year={2026}
}This repository is inspired by and built upon the codebase of TTC (Test-Time Counterattacks). We express our sincere gratitude to the authors for their great contribution.
