-
Notifications
You must be signed in to change notification settings - Fork 0
/
attack_main.py
227 lines (195 loc) · 10.1 KB
/
attack_main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import argparse
import datetime
import os
from pathlib import Path
from typing import Dict
import logging
import numpy as np
import torch
import yaml
from torch import nn
from tqdm import tqdm
from attacks import ATTACK_MAPPINGS
from attacks.art_attack import execute_attack, get_models, get_xyz, hybridize
from attacks.plot_attack import plot_lips, plot_robust_accuracy
from attacks.evaluate_attack import calculate_lpips
from dataloader import DATALOADER_MAPPINGS
from utils import set_logger, save, load
from attacks.torch_attacks import execute_torchattack
from attacks.plot_attack import plot_block
import warnings
warnings.filterwarnings('ignore')
def run_attack(train_dataloader, autoencoder_model, args, config, attack_name, dataset_name, classifier_model, kwargs_orig, kwargs_modf):
result = {attack_name.__name__: {}}
xs, ys = [], []
cas, ras = [], []
x_adv, x_adv_acc, delta_x = [], [], []
modf_x_adv, modf_x_adv_acc = [], []
z_adv, x_hat_adv, x_hat_adv_acc, delta_x_hat = [], [], [], []
orig_time, modf_time = [], []
for images, labels in tqdm(train_dataloader):
x_test, y_test = images.to(args.device), labels.to(args.device)
x_test_np, y_test_np = x_test.cpu().numpy(), y_test.cpu().numpy()
with torch.no_grad():
z_test = autoencoder_model.get_z(x_test)
z_test_np = z_test.detach().cpu().numpy()
x, y, z = (x_test, x_test_np), (y_test, y_test_np), (z_test, z_test_np)
if dataset_name == "imagenet":
config["latent_shape"] = (512, 7, 7)
if dataset_name == "celaba":
config["latent_shape"] = (128, 16, 16)
else:
config["latent_shape"] = args.ae_name.split('_')[-1]
classifier, hybrid_classifier, ca, ra = hybridize(x, y, z,
config, classifier_model, autoencoder_model)
xs.append(images)
for ele in y[1]:
ys.append(ele)
cas.append(ca)
ras.append(ra)
# Perform attack
conditionals = {
"calculate_original": not args.skip_orig,
"is_class_constrained": args.cc
}
results: Dict = execute_attack(config,
attack_name,
x, y, z,
classifier,
hybrid_classifier,
autoencoder_model,
kwargs_orig, kwargs_modf,
conditionals)[attack_name.__name__]
# results = result[attack_name.__name__]
if not args.skip_orig:
x_adv.append(results["x_adv"])
x_adv_acc.append(results["x_adv_acc"])
delta_x.append(results["delta_x"])
orig_time.append(results["orig_time"])
modf_x_adv.append(results["modf_x_adv"])
modf_x_adv_acc.append(results["modf_x_adv_acc"])
z_adv.append(results["z_adv"])
x_hat_adv.append(results["x_hat_adv"])
x_hat_adv_acc.append(results["x_hat_adv_acc"])
delta_x_hat.append(results["delta_x_hat"])
modf_time.append(results["modf_time"])
print("Accuracy on benign test examples: {}%".format((sum(cas)/len(cas)) * 100))
print("Accuracy on benign test examples(from reconstructed): {}%".format((sum(ras)/len(ras)) * 100))
if not args.skip_orig:
result[attack_name.__name__]["x_adv"] = np.vstack(x_adv)
result[attack_name.__name__]["x_adv_acc"] = sum(x_adv_acc) / len(x_adv_acc)
result[attack_name.__name__]["delta_x"] = np.vstack(delta_x)
result[attack_name.__name__]["modf_x_adv"] = np.vstack(modf_x_adv)
result[attack_name.__name__]["modf_x_adv_acc"] = sum(modf_x_adv_acc) / len(modf_x_adv_acc)
result[attack_name.__name__]["z_adv"] = np.vstack(z_adv)
result[attack_name.__name__]["x_hat_adv"] = np.vstack(x_hat_adv)
result[attack_name.__name__]["x_hat_adv_acc"] = sum(x_hat_adv_acc) / len(x_hat_adv_acc)
result[attack_name.__name__]["delta_x_hat"] = np.vstack(delta_x_hat)
result[attack_name.__name__]["x"] = torch.vstack(xs)
ys = np.array(ys)
if not args.skip_orig:
print(f"Time taken for original attack: {sum(orig_time)} seconds")
print(f"Time taken for modified attack: {sum(modf_time)} seconds")
return result, ys
def get_args_parser():
parser = argparse.ArgumentParser('X and Semantic-X adversarial attack', add_help=False)
parser.add_argument('--root-dir', default="/scratch/itee/uqsswain/",
help="folder where all the artifacts lie; differs from system to system")
parser.add_argument('--batch-size', default=64, type=int)
parser.add_argument('--dataset-len', default=1000, type=int)
parser.add_argument('--attack-name', default="fgsm", type=str,
help="choose one of: fgsm, bim, pgd, cnw, deepfool, elastic")
parser.add_argument('--device', default='cuda',
help='device to use for training / testing')
parser.add_argument('--model-name', default='mnist_ann_1', type=str,
help='name of the model to attack(find the list)')
parser.add_argument('--ae-name', default='ann_128', type=str,
help='name of the autoencoder to use for recon(find from configs)')
parser.add_argument('--plot', action='store_true', default=False,
help='generate plot')
parser.add_argument('--eval', action='store_true', default=False,
help='perform attack inference')
parser.add_argument('--skip-orig', action='store_true', default=False,
help='calculate original')
parser.add_argument('--cc', action='store_true', default=False,
help='class-constrained')
parser.add_argument('--torch-attacks', action='store_true', default=False,
help='Use torchattacks library')
return parser
def main(args):
logger = set_logger(args)
dataset_name = args.model_name.split("_")[0]
logger.info(f"Working on the dataset: {dataset_name}!!!!!")
object_path = args.root_dir + f"artifacts/spaa/objects/{dataset_name}/{args.attack_name}.pkl"
attack_name = ATTACK_MAPPINGS.get(args.attack_name)
logger.info(f"----------------------------- {attack_name.__name__} ----------------------------------")
if not args.eval:
# Load Models
classifier_model, autoencoder_model, config = get_models(args)
logger.info(f"Loaded classifier and autoencoder models in eval mode!!!!!")
train_dataloader = DATALOADER_MAPPINGS[dataset_name + "_x"](
batch_size=args.batch_size, root=args.root_dir, dataset_len=args.dataset_len
)
logger.info(f"Loaded dataloader!!!!!")
if args.torch_attacks:
kwargs = config["torch_attack_kwargs"]
kwargs_orig = kwargs[f"{args.attack_name}_orig"]
kwargs_modf = kwargs[f"{args.attack_name}_modf"]
print(kwargs_orig, kwargs_modf)
# Run Attack torchattacks
result, ys = execute_torchattack(args, config,
train_dataloader,
autoencoder_model,
classifier_model,
attack_name, dataset_name,
kwargs_orig, kwargs_modf)
else:
kwargs = config["art_attack_kwargs"]
kwargs_orig = kwargs[f"{args.attack_name}_orig"]
kwargs_modf = kwargs[f"{args.attack_name}_modf"]
kwargs_orig["batch_size"], kwargs_modf["batch_size"] = args.batch_size, args.batch_size
print(kwargs_orig, kwargs_modf)
# Run Attack ART
result, ys = run_attack(train_dataloader,
autoencoder_model,
args, config,
attack_name, dataset_name,
classifier_model,
kwargs_orig, kwargs_modf)
# Save Adversarial Samples
save(object_path, result)
else:
result = load(object_path)
if not args.skip_orig:
print("Robust accuracy of original adversarial attack: {}%".format(result[attack_name.__name__]["x_adv_acc"] * 100))
print("Robust accuracy of modified adversarial attack: {}%".format(result[attack_name.__name__]["modf_x_adv_acc"] * 100))
print("Robust accuracy of reconstructed adversarial attack: {}%".format(result[attack_name.__name__]["x_hat_adv_acc"] * 100))
# # LPIPS
x = result[attack_name.__name__]["x"]
orig_lpips, modf_lpips, orig_linf, modf_linf = calculate_lpips(args, result, attack_name, x)
print("Average LPIPS score of original adversarial attack: ", orig_lpips.flatten().mean())
print("Average LPIPS score of modifed adversarial attack: ", modf_lpips.flatten().mean())
# Linf
print("Average Linf distance between original and original adversarial images: ", orig_linf.mean())
print("Average Linf distance between original and modified adversarial images: ", modf_linf.mean())
if args.plot:
# Plot
if not args.skip_orig:
x_adv = result[attack_name.__name__]["x_adv"]
delta_x = result[attack_name.__name__]["delta_x"]
x_hat_adv = result[attack_name.__name__]["x_hat_adv"]
modf_x_adv = result[attack_name.__name__]["modf_x_adv"]
# noises
delta_x_hat = result[attack_name.__name__]["delta_x_hat"]
start = 22
end = 32
images1 = np.vstack([x[start: end], x_adv[start: end], delta_x[start: end]])
images2 = np.vstack([modf_x_adv[start: end], delta_x_hat[start: end]])
plot_block(images1, "./plots/cifar_df_block1.png")
plot_block(images2, "./plots/cifar_df_block2.png")
if __name__ == '__main__':
parser = argparse.ArgumentParser('Attack and evaluation script', parents=[get_args_parser()])
args = parser.parse_args()
# if args.output_dir:
# Path(args.output_dir).mkdir(parents=True, exist_ok=True)
main(args)