-
Notifications
You must be signed in to change notification settings - Fork 5
/
evaluation_mnist.py
202 lines (162 loc) · 8.34 KB
/
evaluation_mnist.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Mar 21 17:19:50 2019
@author: eric
"""
# EXTERNAL LIBRARY IMPORTS
import prebuilt_loss_functions as plf
import adversarial_training as advtrain
import adversarial_evaluation as adveval
import adversarial_perturbations as ap
import adversarial_attacks as aa
import loss_functions as lf
import argparse
import re
import torch
import os
import torch.nn as nn
from adv_defence.models import Classifier
import torchvision.datasets as datasets
import torch.utils.data as data
import torchvision.transforms as transforms
import utils.pytorch_utils as utils
transform_list = [transforms.ToTensor()]
transform_chain = transforms.Compose(transform_list)
image_dataset = datasets.MNIST(root='./datasets/mnist', train=False, transform=transform_chain, download=True)
test_dataloader = data.DataLoader(image_dataset, batch_size=100, shuffle=False, num_workers=2)
def main(config):
model = Classifier(10, classifier_name="lenet", dataset="mnist", pretrained=False)
# format matching
data_classifier_state = torch.load(os.path.join(config.path, 'Classifier.pth'), map_location=None)
if 'state_dict' in data_classifier_state:
data_classifier_state = data_classifier_state['state_dict']
bad_classifier_state = {}
for k, v in data_classifier_state.items():
if k.startswith('1.'):
bad_classifier_state[k[2:]] = v
else:
bad_classifier_state[k] = v
starts_with_module = False
for key in bad_classifier_state.keys():
if key.startswith('module.'):
starts_with_module = True
break
if starts_with_module:
correct_classifier_state = {k[7:]: v for k, v in
bad_classifier_state.items()}
else:
correct_classifier_state = bad_classifier_state
starts_with_feature_extractor = False
for k in correct_classifier_state.keys():
if k.startswith('feature_extractor.'):
starts_with_feature_extractor = True
break
if not starts_with_feature_extractor:
correct_classifier_state = {'feature_extractor.'+k: v for k, v in
correct_classifier_state.items()}
model.load_state_dict( correct_classifier_state )
normalizer = utils.IdentityNormalize()
# Put this into the AdversarialEvaluation object
adv_eval_object = adveval.AdversarialEvaluation(model, normalizer)
surrogate = model
normalizer_surr = normalizer
# First let's build the attack parameters for each.
# we'll reuse the loss function:
attack_loss = plf.VanillaXentropy(surrogate, normalizer_surr)
linf_3_threat = ap.ThreatModel(ap.DeltaAddition, {'lp_style': 'inf',
'lp_bound': 0.3})
#------ FGSM Block
fgsm_attack = aa.FGSM(surrogate, normalizer_surr, linf_3_threat, attack_loss)
fgsm_attack_kwargs = {'step_size': 0.3,
'verbose': False}
fgsm_attack_params = advtrain.AdversarialAttackParameters(fgsm_attack,
attack_specific_params=
{'attack_kwargs': fgsm_attack_kwargs})
# ------ pgd10 Block
pgd10_attack = aa.PGD(surrogate, normalizer_surr, linf_3_threat, attack_loss)
pgd10_attack_kwargs = {'step_size': 0.3/4.0,
'num_iterations': 10,
'keep_best': True,
'random_init': True,
'verbose': False}
pgd10_attack_params = advtrain.AdversarialAttackParameters(pgd10_attack,
attack_specific_params=
{'attack_kwargs': pgd10_attack_kwargs})
# ------ pgd100 Block
pgd100_attack = aa.PGD(surrogate, normalizer_surr, linf_3_threat, attack_loss)
pgd100_attack_kwargs = {'step_size': 0.3/12.0,
'num_iterations': 100,
'keep_best': True,
'random_init': True,
'verbose': False}
pgd100_attack_params = advtrain.AdversarialAttackParameters(pgd100_attack,
attack_specific_params=
{'attack_kwargs': pgd100_attack_kwargs})
# ------ CarliniWagner100 Block
cwloss6 = lf.CWLossF6
distance_fxn = lf.SoftLInfRegularization
cw100_attack = aa.CarliniWagner(surrogate, normalizer_surr, linf_3_threat, distance_fxn, cwloss6)
cw100_attack_kwargs = {'num_optim_steps': 100,
'verbose': False}
cw100_attack_params = advtrain.AdversarialAttackParameters(cw100_attack,
attack_specific_params=
{'attack_kwargs': cw100_attack_kwargs})
# ------ CarliniWagner1000 Block
cwloss6 = lf.CWLossF6
distance_fxn = lf.SoftLInfRegularization
cw1000_attack = aa.CarliniWagner(surrogate, normalizer_surr, linf_3_threat, distance_fxn, cwloss6)
cw1000_attack_kwargs = {'num_optim_steps': 1000,
'verbose': False}
cw1000_attack_params = advtrain.AdversarialAttackParameters(cw1000_attack,
attack_specific_params=
{'attack_kwargs': cw1000_attack_kwargs})
to_eval_dict = {'top1': 'top1',
'avg_loss_value': 'avg_loss_value',
'avg_successful_ssim': 'avg_successful_ssim'}
fgsm_eval = adveval.EvaluationResult(fgsm_attack_params,
to_eval=to_eval_dict)
pgd10_eval = adveval.EvaluationResult(pgd10_attack_params,
to_eval=to_eval_dict)
pgd100_eval = adveval.EvaluationResult(pgd100_attack_params,
to_eval=to_eval_dict)
cw100_eval = adveval.EvaluationResult(cw100_attack_params,
to_eval=to_eval_dict)
cw1000_eval = adveval.EvaluationResult(cw1000_attack_params,
to_eval=to_eval_dict)
attack_ensemble = {'fgsm': fgsm_eval,
'pgd10' : pgd10_eval,
'pgd100' : pgd100_eval,
'cw100' : cw100_eval,
'cw1000' : cw1000_eval}
ensemble_out = adv_eval_object.evaluate_ensemble(test_dataloader, attack_ensemble,
verbose=True,
num_minibatches=None)
# Now let's build a little helper to print things out cleanly:
sort_order = {'ground': 1, 'fgsm': 2, 'pgd10': 3, 'pgd100': 4, 'cw100': 5, 'cw1000': 6}
def pretty_printer(fd, eval_ensemble, result_type):
print('~' * 10, result_type, '~' * 10)
fd.write('~' * 10+ result_type+ '~' * 10+"\n")
for key in sorted(list(eval_ensemble.keys()), key=lambda k: sort_order[k]):
eval_result = eval_ensemble[key]
pad = 6 - len(key)
if result_type not in eval_result.results:
continue
avg_result = eval_result.results[result_type].avg
print(key, pad* ' ', ': ', avg_result)
fd.write(key + pad* ' '+ ': '+ str(avg_result)+"\n")
with open(os.path.join(config.path, 'base_eval_result.txt'), "w") as fd:
fd.write('Result for {}'.format(config.path)+"\n")
fd.write("\n")
pretty_printer(fd, ensemble_out, 'top1')
# We can examine the loss (noting that we seek to 'maximize' loss in the adversarial example domain)
pretty_printer(fd, ensemble_out, 'avg_loss_value')
# This is actually 1-SSIM, which can serve as a makeshift 'similarity index',
# which essentially gives a meterstick for how similar the perturbed images are to the originals
pretty_printer(fd, ensemble_out, 'avg_successful_ssim')
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--path', type=str, default='mnist_lenet_ours',
help='classifier path')
config, _ = parser.parse_known_args()
main(config)