(args: argparse.Namespace, dataset)
| 55 | |
| 56 | @beartype |
| 57 | def run(args: argparse.Namespace, dataset) -> None: |
| 58 | attack_fn = get_attack_fn(args.attack) |
| 59 | captioning_model = get_model(args.captioning_model) |
| 60 | |
| 61 | idx = 0 |
| 62 | for example in dataset: |
| 63 | if args.index is not None and ( |
| 64 | idx not in list(range(args.index * args.batch_size, (args.index + 1) * args.batch_size)) |
| 65 | ): |
| 66 | idx += 1 |
| 67 | continue |
| 68 | idx += 1 |
| 69 | |
| 70 | victim_image = example["victim_image"] |
| 71 | prompt_fn = captioning_model.get_captioning_prompt_fn() |
| 72 | inputs = [prompt_fn()] |
| 73 | outputs = [example["target_caption"]] |
| 74 | |
| 75 | for size in [1536]: |
| 76 | attack_out_dict = attack_fn(captioning_model, victim_image, inputs, outputs, size=size) |
| 77 | adv_image = attack_out_dict["adv_image"] |
| 78 | adv_caption = attack_out_dict["adv_outputs"][0] |
| 79 | acc = attack_out_dict["acc"] |
| 80 | print("Adv caption:", adv_caption) |
| 81 | print("Target caption:", example["target_caption"]) |
| 82 | print("Accuracy:", acc) |
| 83 | if (acc - 1) < 1e-6: |
| 84 | break |
| 85 | |
| 86 | # Save the image |
| 87 | adv_image.save( |
| 88 | os.path.join( |
| 89 | "exp_data", |
| 90 | "agent_adv", |
| 91 | example["id"], |
| 92 | f"{args.attack}_caption_attack_image.png", |
| 93 | ) |
| 94 | ) |
| 95 | # Save the caption |
| 96 | with open( |
| 97 | os.path.join( |
| 98 | "exp_data", |
| 99 | "agent_adv", |
| 100 | example["id"], |
| 101 | f"{args.attack}_caption_attack_caption.txt", |
| 102 | ), |
| 103 | "w", |
| 104 | ) as f: |
| 105 | f.write(adv_caption) |
| 106 | # Save the accuracy |
| 107 | with open( |
| 108 | os.path.join( |
| 109 | "exp_data", |
| 110 | "agent_adv", |
| 111 | example["id"], |
| 112 | f"{args.attack}_caption_attack_acc.txt", |
| 113 | ), |
| 114 | "w", |
no test coverage detected