forked from MSU-NEU-ATL/DFTND
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbaselpnormattack.py
184 lines (147 loc) · 5.71 KB
/
baselpnormattack.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
import torch
class Attack(object):
"""
Base class for all attacks.
.. note::
It automatically set device to the device where given model is.
It temporarily changes the original model's training mode to `test`
by `.eval()` only during an attack process.
"""
def __init__(self, name, model):
"""
Initializes internal attack state.
Arguments:
name (str) : name of an attack.
model (torch.nn.Module): model to attack.
"""
self.attack = name
self.model = model
self.model_name = str(model).split("(")[0]
self.training = model.training
self.device = next(model.parameters()).device
self._targeted = 1
self._attack_mode = 'original'
self._return_type = 'float'
def forward(self, *input):
"""
It defines the computation performed at every call.
Should be overridden by all subclasses.
"""
raise NotImplementedError
def set_attack_mode(self, mode, target_map_function=None):
r"""
Set the attack mode.
Arguments:
mode (str) : 'original' (DEFAULT)
'targeted' - Use input labels as targeted labels.
'least_likely' - Use least likely labels as targeted labels.
"""
# if self._attack_mode is 'only_original':
# raise ValueError("Changing attack mode is not supported in this attack method.")
if mode == "original":
self._attack_mode = "original"
self._targeted = 1
self._transform_label = self._get_label
elif mode == "targeted":
self._attack_mode = "targeted"
self._targeted = -1
self._target_map_function = target_map_function
self._transform_label = self._get_target_label
else:
raise ValueError(mode + " is not a valid mode. [Options : original, targeted, least_likely]")
def set_return_type(self, type):
"""
Set the return type of adversarial images: `int` or `float`.
Arguments:
type (str) : 'float' or 'int'. (DEFAULT : 'float')
"""
if type == 'float':
self._return_type = 'float'
elif type == 'int':
self._return_type = 'int'
else:
raise ValueError(type + " is not a valid type. [Options : float, int]")
def save(self, save_path, data_loader, verbose=True):
r"""
Save adversarial images as torch.tensor from given torch.utils.data.DataLoader.
Arguments:
save_path (str) : save_path.
data_loader (torch.utils.data.DataLoader) : data loader.
verbose (bool) : True for displaying detailed information. (DEFAULT : True)
"""
self.model.eval()
image_list = []
label_list = []
correct = 0
total = 0
total_batch = len(data_loader)
for step, (images, labels) in enumerate(data_loader):
adv_images = self.__call__(images, labels)
image_list.append(adv_images.cpu())
label_list.append(labels.cpu())
if self._return_type == 'int':
adv_images = adv_images.float() / 255
if verbose:
outputs = self.model(adv_images)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels.to(self.device)).sum()
acc = 100 * float(correct) / total
print('- Save Progress : %2.2f %% / Accuracy : %2.2f %%' % ((step + 1) / total_batch * 100, acc),
end='\r')
x = torch.cat(image_list, 0)
y = torch.cat(label_list, 0)
torch.save((x, y), save_path)
print('\n- Save Complete!')
self._switch_model()
def _transform_label(self, images, labels):
"""
Function for changing the attack mode.
"""
return labels
def _get_label(self, images, labels):
"""
Function for changing the attack mode.
Return input labels.
"""
return labels
def _get_target_label(self, images, labels):
r"""
Function for changing the attack mode.
Return input labels.
"""
return self._target_map_function(images, labels)
def _to_uint(self, images):
"""
Function for changing the return type.
Return images as int.
"""
return (images * 255).type(torch.uint8)
def _switch_model(self):
"""
Function for changing the training mode of the model.
"""
if self.training:
self.model.train()
else:
self.model.eval()
def __str__(self):
info = self.__dict__.copy()
del_keys = ['model', 'attack']
for key in info.keys():
if key[0] == "_":
del_keys.append(key)
for key in del_keys:
del info[key]
info['attack_mode'] = self._attack_mode
if info['attack_mode'] == 'only_original':
info['attack_mode'] = 'original'
info['return_type'] = self._return_type
return self.attack + "(" + ', '.join('{}={}'.format(key, val) for key, val in info.items()) + ")"
def __call__(self, *input, **kwargs):
self.model.eval()
images = self.forward(*input, **kwargs)
self._switch_model()
if self._return_type == 'int':
images = self._to_uint(images)
return images