-
Notifications
You must be signed in to change notification settings - Fork 3
/
generate_adversarial_examples.py
220 lines (161 loc) · 6.62 KB
/
generate_adversarial_examples.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
import logging
import os
import shutil
import yaml
import foolbox as fb
import torch
from torchvision import datasets, transforms
from torchvision.utils import save_image
from tqdm.auto import tqdm
from classifiers import Classifier1, Classifier2
with open('config.yaml', 'r') as file:
config = yaml.safe_load(file)['generate_adversarial_examples']
seed = config['seed']
save_path = config['save_path']
dataset = config['dataset']
attack_model = config['attack_model']
batch_size = 1024
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
print(f"Device: {device}")
save_path = os.path.join(save_path, dataset, attack_model)
if os.path.exists(save_path):
shutil.rmtree(save_path)
os.makedirs(save_path)
def get_dataset(dataset):
logging.info("Entering the function 'get_dataset' in 'generate_adversarial_examples.py'")
if dataset == 'mnist':
test_set = datasets.MNIST('../data', train=False, download=False,
transform=transforms.Compose([
transforms.ToTensor(),
]))
elif dataset == 'fashion-mnist':
test_set = datasets.FashionMNIST('../data', train=False, download=False,
transform=transforms.Compose([
transforms.ToTensor(),
]))
elif dataset == 'cifar-10':
test_set = datasets.CIFAR10('../data/', train=False, download=False,
transform=transforms.Compose([
transforms.ToTensor(),
]))
else:
raise ValueError("Undefined dataset")
logging.info("Exiting the function 'get_dataset' in 'generate_adversarial_examples.py'")
return test_set
def get_model(attack_model):
logging.info("Entering the function 'get_model' in 'generate_adversarial_examples.py'")
if attack_model == 'classifier-1':
model = Classifier1()
elif attack_model == 'classifier-2':
model = Classifier2()
else:
raise ValueError("Undefined classifier")
model_state = torch.load(os.path.join('models', f'{dataset}_{attack_model}_checkpoint.pth'))['state_dict']
model.load_state_dict(model_state)
model.eval()
logging.info("Exiting the function 'get_model' in 'generate_adversarial_examples.py'")
return model
def make_dirs(config, test_set):
logging.info("Entering the function 'make_dirs' in 'generate_adversarial_examples.py'")
global save_path
attack_save_path = os.path.join(save_path, f"Attack-{config['attack_id']}")
os.mkdir(attack_save_path)
with open(os.path.join(attack_save_path, 'config.yaml'), 'w') as file:
yaml.dump(config, file, default_flow_style=False)
if config['dataset'] in ['mnist', 'fashion-mnist']:
unique_targets = [unique_target.item() for unique_target in torch.unique(test_set.targets)]
elif config['dataset'] in ['cifar-10']:
unique_targets = list(set(test_set.targets))
for unique_target in unique_targets:
os.mkdir(os.path.join(attack_save_path, str(unique_target)))
logging.info("Exiting the function 'make_dirs' in 'generate_adversarial_examples.py'")
return True
def make_adversarial_examples(config, test_set):
logging.info("Entering the function 'make_adversarial_examples' in 'generate_adversarial_examples.py'")
global save_path
attack_save_path = os.path.join(save_path, f"Attack-{config['attack_id']}")
test_loader = torch.utils.data.DataLoader(test_set, batch_size=batch_size, shuffle=False)
for batch_idx, (X_mb, Y_mb) in tqdm(enumerate(test_loader), leave=False, total=len(test_loader),
desc=f"Attack: {config['attack_id']}"):
X_mb, Y_mb = X_mb.to(device), Y_mb.to(device)
attack_function = config['attack_function']
_, adversarials_mb, success_mb = attack_function(fmodel, X_mb, Y_mb, epsilons=config['epsilons'])
for i in tqdm(range(len(X_mb)), leave=False, desc=f"Attack: {config['attack_id']}; Batch: {batch_idx}"):
adversarial_image = adversarials_mb[i]
y = Y_mb[i].item()
image_id = batch_idx * batch_size + i
attack_image_save_path = os.path.join(attack_save_path, str(y), f'{image_id}.png')
save_image(adversarial_image, attack_image_save_path)
logging.info("Entering the function 'make_adversarial_examples' in 'generate_adversarial_examples.py'")
return True
test_set = get_dataset(dataset)
model = get_model(attack_model)
fmodel = fb.PyTorchModel(model, bounds=(0,1), preprocessing=dict())
# Attack: 0
# No attack
######################
config['attack_id'] = 0
config['attack_function'] = None
config['epsilons'] = 0
make_dirs(config, test_set)
attack_save_path = os.path.join(save_path, f"Attack-{config['attack_id']}")
for i in tqdm(range(len(test_set)), leave=False, desc=f"Attack: {config['attack_id']}"):
x, y = test_set[i]
attack_image_save_path = os.path.join(attack_save_path, str(y), f'{i}.png')
save_image(x, attack_image_save_path)
# Attack: 1
# FGSM
######################
config['attack_id'] = 1
config['attack_function'] = fb.attacks.FGSM()
config['epsilons'] = 0.01
make_dirs(config, test_set)
make_adversarial_examples(config, test_set)
# Attack: 2
# FGSM
######################
config['attack_id'] = 2
config['attack_function'] = fb.attacks.FGSM()
config['epsilons'] = 0.1
make_dirs(config, test_set)
make_adversarial_examples(config, test_set)
# Attack: 3
# L2-PGD
######################
config['attack_id'] = 3
config['attack_function'] = fb.attacks.L2PGD()
config['epsilons'] = 0.01
make_dirs(config, test_set)
make_adversarial_examples(config, test_set)
# Attack: 4
# L2-PGD
######################
config['attack_id'] = 4
config['attack_function'] = fb.attacks.L2PGD()
config['epsilons'] = 0.1
make_dirs(config, test_set)
make_adversarial_examples(config, test_set)
# Attack: 5
# L2-PGD
######################
config['attack_id'] = 5
config['attack_function'] = fb.attacks.L2PGD()
config['epsilons'] = 0.5
make_dirs(config, test_set)
make_adversarial_examples(config, test_set)
# Attack: 6
# DeepFool
######################
config['attack_id'] = 6
config['attack_function'] = fb.attacks.L2DeepFoolAttack()
config['epsilons'] = 0.1
make_dirs(config, test_set)
make_adversarial_examples(config, test_set)
# Attack: 7
# Carlini-Wagner
######################
config['attack_id'] = 7
config['attack_function'] = fb.attacks.L2CarliniWagnerAttack(steps=1000)
config['epsilons'] = 0.1
make_dirs(config, test_set)
make_adversarial_examples(config, test_set)