This is a Python implementation of `AICAttack: Adversarial Image Captioning Attack with Attention-Based Optimization''. A GPU environment is required for running the code.
![github_exmaple](https://private-user-images.githubusercontent.com/49722565/330729660-df6d5611-541c-4af9-9e40-74e1069303dc.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Mzk2Mjc1OTAsIm5iZiI6MTczOTYyNzI5MCwicGF0aCI6Ii80OTcyMjU2NS8zMzA3Mjk2NjAtZGY2ZDU2MTEtNTQxYy00YWY5LTllNDAtNzRlMTA2OTMwM2RjLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNTAyMTUlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjUwMjE1VDEzNDgxMFomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTI2NGQ5YmUxNDZiYWUyY2EyOTkxZjVjNzYwYTNjMDczYjc0ODI1NjQzZDFiZGE5M2E4MTA4OGE5NjBiZTNkNjImWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0._QUYo1XaeIgtD0_oY1Pkz273O6-jJQJEuUg3kMS5imI)
The code is written in Python 3.8, with:
rouge-score==0.1.2
tensorflow==2.7.4
torch==2.0.1
Other Python packages can be installed by running the following command from the command line.
pip install -r requirements.txt
The checkpoints you need to download: (https://drive.google.com/file/d/1HkhMC_SBRz57puNTN9MqCz-Vut5OzvSA/view?usp=sharing)
First, you need to download victim models from the following links:
SAT: https://github.com/sgrvinod/a-PyTorch-Tutorial-to-Image-Captioning
BLIP: https://github.com/salesforce/BLIP
Secondly, the datasets are available at:
COCO -- https://cocodataset.org/#home
Flickr8k -- https://www.kaggle.com/datasets/adityajn105/flickr8k
Move data and model to the root directory.
To attack an image, run the attack_sat.py
or attack_blip_test.py
according to the victim model.
For example, to attack SAT:
python3 attack_sat.py
To test AICAttack performance, you should run the "seperate.py".
python3 seperate.py
The results are saved in a .json
file in outputs/jsons/........json
.
You can customize your test run by changing the following arguments:
-
--name
, dataset name; -
--num_input
, number of input data; -
--pixels
, number of pixels to attack; -
--max_iter
, max iteration of DE algorithm; -
--pop_size
, population size; -
--keywords
, number of keywords; -
--F
, mutation scale factor ($\lambda$ in paper); -
--image_size
, input image size; -
--metric
, metrics used for comparing attack performance in DE algorithm; -
--separate
, word-attack or sentence-attack; -
--attention
, Whether use attention; -
--save_img
, Save the perturbed image or not.