This repository provides the official implementation of the paper "AnyAttack: Towards Large-scale Self-supervised Generation of Targeted Adversarial Examples for Vision-Language Models." Our method demonstrates high effectiveness across a wide range of commercial Vision-Language Models (VLMs).
Figure: AnyAttack results on various commercial VLMs
Create Conda environment for LAVIS:
Set up the LAVIS environment for BLIP, BLIP2, and InstructBLIP. Follow the instructions here.
Optional: Mini-GPT4 environment: If you plan to evaluate on Mini-GPT4 series models, set up an additional environment according to Mini-GPT4's installation guide.
Data Preparation:
You can either use the pretrained weights from Step 2 or train the models from scratch.
Optional: Pretraining on LAION-400M: If you choose to pretrain on LAION-400M:
./scripts/main.sh
Replace "YOUR_LAION_DATASET"
with your LAION-400M dataset path.
Fine-tuning on downstream datasets:
./scripts/finetune_ddp.sh
Adjust the dataset
, criterion
, and data_dir
parameters as needed.
Use the pretrained decoder to generate adversarial images:
./scripts/generate_adv_img.sh
Ensure that datasets from Step 1 are stored under the DATASET_BASE_PATH
directory, and set PROJECT_PATH
to the current project directory.
Evaluate the trained models on different tasks:
./scripts/retrieval.sh
python ./scripts/classification_shell.py
python ./scripts/caption_shell.py
We've added a demo.py
script for easy demonstration of AnyAttack. This script allows users to generate adversarial examples using a single target image and a clean image.
To run the demo:
python demo.py --decoder_path path/to/decoder.pth --clean_image_path path/to/clean_image.jpg --target_image_path path/to/target_image.jpg --output_path output.png
For more options and details, please refer to the demo.py
file.
If you find this work useful for your research, please consider citing:
@article{zhang2024anyattack,
title={AnyAttack: Towards Large-scale Self-supervised Generation of Targeted Adversarial Examples for Vision-Language Models},
author={Jiaming Zhang and Junhong Ye and Xingjun Ma and Yige Li and Yunfan Yang and Jitao Sang and Dit-Yan Yeung},
year={2024},
journal={arXiv preprint arXiv:2410.05346},
}
For any questions or concerns, please open an issue in this repository or contact the authors directly.