-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
297 lines (265 loc) · 10.6 KB
/
utils.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
import torch
import torch.nn as nn
import torch.nn.functional as F
from torchvision import datasets, transforms
from torch.utils.data.sampler import SubsetRandomSampler
import numpy as np
import math
cifar10_mean = (0.4914, 0.4822, 0.4465)
cifar10_std = (0.2507, 0.2507, 0.2507)
mu = torch.tensor(cifar10_mean).view(3, 1, 1).cuda()
std = torch.tensor(cifar10_std).view(3, 1, 1).cuda()
upper_limit = ((1 - mu)/ std)
lower_limit = ((0 - mu)/ std)
def clamp(X, lower_limit, upper_limit):
return torch.max(torch.min(X, upper_limit), lower_limit)
def get_loaders(dir_, batch_size, dataset_name='cifar10', normalize=True):
if dataset_name == 'cifar10':
dataset_func = datasets.CIFAR10
elif dataset_name == 'cifar100':
dataset_func = datasets.CIFAR100
if normalize:
train_transform = transforms.Compose([
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize(cifar10_mean, cifar10_std),
])
test_transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(cifar10_mean, cifar10_std),
])
else:
train_transform = transforms.Compose([
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
])
test_transform = transforms.Compose([
transforms.ToTensor(),
])
num_workers = 4
train_dataset = dataset_func(
dir_, train=True, transform=train_transform, download=True)
test_dataset = dataset_func(
dir_, train=False, transform=test_transform, download=True)
train_loader = torch.utils.data.DataLoader(
dataset=train_dataset,
batch_size=batch_size,
shuffle=True,
pin_memory=True,
num_workers=num_workers,
)
test_loader = torch.utils.data.DataLoader(
dataset=test_dataset,
batch_size=batch_size,
shuffle=True,
pin_memory=True,
num_workers=2,
)
return train_loader, test_loader
def attack_pgd(model, X, y, epsilon, alpha, attack_iters, restarts, opt=None):
max_loss = torch.zeros(y.shape[0]).cuda()
max_delta = torch.zeros_like(X).cuda()
for zz in range(restarts):
delta = torch.zeros_like(X).cuda()
for i in range(len(epsilon)):
delta[:, i, :, :].uniform_(-epsilon[i][0][0].item(), epsilon[i][0][0].item())
delta.data = clamp(delta, lower_limit - X, upper_limit - X)
delta.requires_grad = True
for _ in range(attack_iters):
output = model(X + delta)
index = torch.where(output.max(1)[1] == y)
if len(index[0]) == 0:
break
loss = F.cross_entropy(output, y)
if opt is not None:
with amp.scale_loss(loss, opt) as scaled_loss:
scaled_loss.backward()
else:
loss.backward()
grad = delta.grad.detach()
d = delta[index[0], :, :, :]
g = grad[index[0], :, :, :]
d = clamp(d + alpha * torch.sign(g), -epsilon, epsilon)
d = clamp(d, lower_limit - X[index[0], :, :, :], upper_limit - X[index[0], :, :, :])
delta.data[index[0], :, :, :] = d
delta.grad.zero_()
all_loss = F.cross_entropy(model(X + delta), y, reduction='none').detach()
max_delta[all_loss >= max_loss] = delta.detach()[all_loss >= max_loss]
max_loss = torch.max(max_loss, all_loss)
return max_delta
def evaluate_pgd(test_loader, model, attack_iters, restarts, limit_n=float("inf")):
epsilon = (8 / 255.) / std
alpha = (2 / 255.) / std
pgd_loss = 0
pgd_acc = 0
n = 0
model.eval()
for i, (X, y) in enumerate(test_loader):
X, y = X.cuda(), y.cuda()
pgd_delta = attack_pgd(model, X, y, epsilon, alpha, attack_iters, restarts)
with torch.no_grad():
output = model(X + pgd_delta)
loss = F.cross_entropy(output, y)
pgd_loss += loss.item() * y.size(0)
pgd_acc += (output.max(1)[1] == y).sum().item()
n += y.size(0)
if n >= limit_n:
break
return pgd_loss/n, pgd_acc/n
def attack_pgd_l2(model, X, y, epsilon, alpha, attack_iters, restarts, opt=None):
max_loss = torch.zeros(y.shape[0]).cuda()
max_delta = torch.zeros_like(X).cuda()
for zz in range(restarts):
delta = torch.zeros_like(X).cuda()
for i in range(len(epsilon)):
delta[:, i, :, :].uniform_(-epsilon[i][0][0].item(), epsilon[i][0][0].item())
delta.data = clamp(delta, lower_limit - X, upper_limit - X)
delta.requires_grad = True
for _ in range(attack_iters):
output = model(X + delta)
index = torch.where(output.max(1)[1] == y)
if len(index[0]) == 0:
break
loss = F.cross_entropy(output, y)
if opt is not None:
with amp.scale_loss(loss, opt) as scaled_loss:
scaled_loss.backward()
else:
loss.backward()
grad = delta.grad.detach()
d = delta[index[0], :, :, :]
g = grad[index[0], :, :, :]
d = clamp(d + alpha * torch.sign(g), -epsilon, epsilon)
d = clamp(d, lower_limit - X[index[0], :, :, :], upper_limit - X[index[0], :, :, :])
delta.data[index[0], :, :, :] = d
delta.grad.zero_()
all_loss = F.cross_entropy(model(X+delta), y, reduction='none').detach()
max_delta[all_loss >= max_loss] = delta.detach()[all_loss >= max_loss]
max_loss = torch.max(max_loss, all_loss)
return max_delta
def evaluate_pgd_l2(test_loader, model, attack_iters, restarts, limit_n=float("inf")):
epsilon = (36 / 255.) / std
alpha = epsilon/5.
pgd_loss = 0
pgd_acc = 0
n = 0
model.eval()
for i, (X, y) in enumerate(test_loader):
X, y = X.cuda(), y.cuda()
pgd_delta = attack_pgd_l2(model, X, y, epsilon, alpha, attack_iters, restarts)
with torch.no_grad():
output = model(X + pgd_delta)
loss = F.cross_entropy(output, y)
pgd_loss += loss.item() * y.size(0)
pgd_acc += (output.max(1)[1] == y).sum().item()
n += y.size(0)
if n >= limit_n:
break
return pgd_loss/n, pgd_acc/n
def evaluate_standard(test_loader, model):
test_loss = 0
test_acc = 0
n = 0
model.eval()
with torch.no_grad():
for i, (X, y) in enumerate(test_loader):
X, y = X.cuda(), y.cuda()
output = model(X)
loss = F.cross_entropy(output, y)
test_loss += loss.item() * y.size(0)
test_acc += (output.max(1)[1] == y).sum().item()
n += y.size(0)
return test_loss/n, test_acc/n
def ortho_certificates(output, class_indices, L):
batch_size = output.shape[0]
batch_indices = torch.arange(batch_size)
onehot = torch.zeros_like(output).cuda()
onehot[torch.arange(output.shape[0]), class_indices] = 1.
output_trunc = output - onehot*1e6
output_class_indices = output[batch_indices, class_indices]
output_nextmax = torch.max(output_trunc, dim=1)[0]
output_diff = output_class_indices - output_nextmax
return output_diff/(math.sqrt(2)*L)
def lln_certificates(output, class_indices, last_layer, L):
batch_size = output.shape[0]
batch_indices = torch.arange(batch_size)
onehot = torch.zeros_like(output).cuda()
onehot[batch_indices, class_indices] = 1.
output_trunc = output - onehot*1e6
lln_weight = last_layer.lln_weight
lln_weight_indices = lln_weight[class_indices, :]
lln_weight_diff = lln_weight_indices.unsqueeze(1) - lln_weight.unsqueeze(0)
lln_weight_diff_norm = torch.norm(lln_weight_diff, dim=2)
lln_weight_diff_norm = lln_weight_diff_norm + onehot
output_class_indices = output[batch_indices, class_indices]
output_diff = output_class_indices.unsqueeze(1) - output_trunc
all_certificates = output_diff/(lln_weight_diff_norm*L)
return torch.min(all_certificates, dim=1)[0]
def evaluate_certificates(test_loader, model, L, epsilon=36.):
losses_list = []
certificates_list = []
correct_list = []
model.eval()
with torch.no_grad():
for i, (X, y) in enumerate(test_loader):
X, y = X.cuda(), y.cuda()
output = model(X)
loss = F.cross_entropy(output, y, reduction='none')
losses_list.append(loss)
output_max, output_amax = torch.max(output, dim=1)
if model.lln:
certificates = lln_certificates(output, output_amax, model.last_layer, L)
else:
certificates = ortho_certificates(output, output_amax, L)
correct = (output_amax==y)
certificates_list.append(certificates)
correct_list.append(correct)
losses_array = torch.cat(losses_list, dim=0).cpu().numpy()
certificates_array = torch.cat(certificates_list, dim=0).cpu().numpy()
correct_array = torch.cat(correct_list, dim=0).cpu().numpy()
return losses_array, correct_array, certificates_array
from cayley_ortho_conv import Cayley, CayleyLinear
from block_ortho_conv import BCOP
from skew_ortho_conv import SOC
from lot_ortho_conv import LOT
conv_mapping = {
'standard': nn.Conv2d,
'soc': SOC,
'bcop': BCOP,
'cayley': Cayley,
'lot': LOT
}
from custom_activations import MaxMin, HouseHolder, HouseHolder_Order_2
activation_dict = {
'relu': F.relu,
'swish': F.silu,
'sigmoid': F.sigmoid,
'tanh': F.tanh,
'softplus': F.softplus,
'maxmin': MaxMin()
}
def activation_mapping(activation_name, channels=None):
if activation_name == 'hh1':
assert channels is not None, channels
activation_func = HouseHolder(channels=channels)
elif activation_name == 'hh2':
assert channels is not None, channels
activation_func = HouseHolder_Order_2(channels=channels)
else:
activation_func = activation_dict[activation_name]
return activation_func
def parameter_lists(model):
conv_params = []
activation_params = []
other_params = []
for name, param in model.named_parameters():
if param.requires_grad:
if 'activation' in name:
activation_params.append(param)
elif 'conv' in name:
conv_params.append(param)
else:
other_params.append(param)
return conv_params, activation_params, other_params