-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathadversarial.py
196 lines (165 loc) · 4.59 KB
/
adversarial.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
"""Adversarial attacks"""
from torch import nn
import torch
# %%
def fgsm(model, inp, label, epsilon=0.3):
"""Performs FGSM attack on model assuming cross entropy loss.
Parameters
----------
model : nn.Module
Model to attack
input : tensor
Input to perturb adversarially.
label : tensor
Target label to minimize score of.
epsilon : float
Magnitude of perturbation (the default is 0.3).
Returns
-------
tensor
Adversarially perturbed input.
"""
if epsilon == 0:
return inp.clone().detach()
inp = inp.clone().detach().requires_grad_(True)
output = model(inp)
loss = nn.CrossEntropyLoss()(output, label)
loss.backward()
perturbation = inp.grad.sign()*epsilon
return (inp+perturbation).detach()
# # %%
# import torch
# class Model(nn.Module):
# def __init__(self):
# super().__init__()
# self.layer = nn.Linear(784, 10)
# def forward(self, input):
# return self.layer(input.view(input.shape[0],-1))
#
# device='cuda'
# model = Model().to(device)
# input = torch.randn(5,28,28).to(device)
# inp = input
# label = torch.LongTensor([5,3,2,1,0]).to(device)
#
# # %%
# adv_inp = fgsm(model, input, label)
# with torch.no_grad():
# print(model(input)[range(len(input)),label])
# print(model(adv_inp)[range(len(input)),label])
# %%
class AdversarialLoader():
"""Wrap a DataLoader with an adversarial attack.
Parameters
----------
model : nn.Module
Model used to generate adversarial examples.
dataloader : DataLoader
Data loader to wrap. Assumes it yields batches of inputs and labels.
attack : function
Attack function with signature (model, input, label) -> adversarial_input.
Attributes
----------
model
dataloader
attack
"""
def __init__(self, model, dataloader, attack):
self.model = model
self.dataloader = dataloader
self.attack = attack
def __iter__(self):
for input, label in self.dataloader:
yield self.attack(self.model, input, label), label
def __len__(self):
return len(self.dataloader)
# # %%
# import logging
# from data_ingredient import make_dataloaders
# from training_functions import validate
# from tqdm import tqdm
#
# dset, train, val, test = make_dataloaders(32, 0, 0.1, 'cpu', logging.getLogger("dataset"))
#
# adv_test = AdversarialLoader(model, train, fgsm)
# len(adv_test)
#
#
# # %%
# validate(model, test)
#
# # %%
# validate(model, adv_test)
# %%
# inp = input
# epsilon = 0.3
# step_size = 0.01
# num_steps = 40
# random_start = True
# %%
def pgd(model, inp, label,
epsilon=0.3,
step_size=0.01,
num_steps=40,
random_start=True,
pixel_range=(-0.5, 0.5)):
"""Short summary.
Parameters
----------
model : nn.Module
Model to attack
inp : tensor
Input to perturb adversarially.
label : tensor
Target label to minimize score of.
epsilon : float
Magnitude of perturbation (the default is 0.3).
step_size : float
Size of PGD step (the default is 0.01).
num_steps : float
Number of PGD steps for one attack. Note that the model is called this
many times for each attack. (the default is 40).
random_start : float
Whether or not to add a uniform random (-epsilon to epsilon) perturbation
before performing PGD. (the default is True).
pixel_range : float
Range to clip the output. (the default is (-0.5, 0.5)).
Returns
-------
tensor
Adversarially perturbed input.
"""
adv_inp = inp.clone().detach()
if epsilon == 0:
return adv_inp
if random_start:
adv_inp += torch.rand(*adv_inp.shape, device=inp.device)*2*epsilon - epsilon
for i in range(num_steps):
inp_var = adv_inp.clone().requires_grad_(True)
output = model(inp_var)
loss = nn.CrossEntropyLoss()(output, label)
loss.backward()
adv_inp += inp_var.grad.sign()*step_size
adv_inp = torch.max(torch.min(adv_inp, inp+epsilon), inp-epsilon)
adv_inp = torch.clamp(adv_inp, *pixel_range)
return adv_inp.clone().detach()
# # %%
# import logging
# from data_ingredient import make_dataloaders
# from training_functions import validate
# from tqdm import tqdm
#
# device = 'cuda'
# model = model.to(device)
#
# dset, train, val, test = make_dataloaders(32, 0, 0.1, device, logging.getLogger("dataset"))
#
# adv_test = AdversarialLoader(model, test, pgd)
# len(adv_test)
#
#
# # %%
# validate(model, test)
#
# # %%
# validate(model, adv_test)