-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutils.py
More file actions
116 lines (106 loc) · 4.29 KB
/
utils.py
File metadata and controls
116 lines (106 loc) · 4.29 KB
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
from cv2 import mean
import torch
import torch.nn as nn
import torch.nn.parallel
import torch.optim
from tqdm import tqdm
import torch.nn.functional as F
import numpy as np
from models.layers import *
import random
import time
def arsnn_reg(net, beta):
l = 0
for m in net.modules():
if isinstance(m, (nn.Linear, nn.Conv2d)):
weight = m.weight
if isinstance(m, nn.Conv2d):
weight = weight.view(weight.shape[0], -1)
sum_1 = torch.sum(F.relu(0 - weight), dim=1)
sum_2 = torch.sum(F.relu(weight), dim=1)
l += (torch.max(sum_1) + torch.max(sum_2)) * beta
return l
def train(model, device, train_loader, criterion, optimizer, T, atk, beta, parseval=False):
running_loss = 0
model.train()
M = len(train_loader)
total = 0
correct = 0
for i, (images, labels) in enumerate((train_loader)):
optimizer.zero_grad()
labels = labels.to(device)
images = images.to(device)
if atk is not None:
atk.set_training_mode(model_training=False, batchnorm_training=False, dropout_training=False)
images = atk(images, labels)
if T > 0:
outputs = model(images).mean(0)
else:
outputs = model(images)
loss = criterion(outputs, labels)
running_loss += loss.item()
loss.mean().backward()
optimizer.step()
if parseval:
orthogonal_retraction(model, beta)
convex_constraint(model)
total += float(labels.size(0))
_, predicted = outputs.cpu().max(1)
correct += float(predicted.eq(labels.cpu()).sum().item())
return running_loss, 100 * correct / total
def val(model, test_loader, device, T, atk=None):
correct = 0
total = 0
model.eval()
for batch_idx, (inputs, targets) in enumerate((test_loader)):
inputs = inputs.to(device)
if atk is not None:
atk.set_training_mode(model_training=False, batchnorm_training=False, dropout_training=False)
inputs = atk(inputs, targets.to(device))
model.set_simulation_time(T)
with torch.no_grad():
if T > 0:
outputs = model(inputs).mean(0)
else:
outputs = model(inputs)
_, predicted = outputs.cpu().max(1)
total += float(targets.size(0))
correct += float(predicted.eq(targets).sum().item())
final_acc = 100 * correct / total
return final_acc
def orthogonal_retraction(model, beta=0.002):
with torch.no_grad():
for module in model.modules():
if isinstance(module, (nn.Conv2d, nn.Linear)):
if isinstance(module, nn.Conv2d):
weight_ = module.weight.data
sz = weight_.shape
weight_ = weight_.reshape(sz[0],-1)
rows = list(range(module.weight.data.shape[0]))
elif isinstance(module, nn.Linear):
if module.weight.data.shape[0] < 200: # set a sample threshold for row number
weight_ = module.weight.data
sz = weight_.shape
weight_ = weight_.reshape(sz[0], -1)
rows = list(range(module.weight.data.shape[0]))
else:
rand_rows = np.random.permutation(module.weight.data.shape[0])
rows = rand_rows[: int(module.weight.data.shape[0] * 0.3)]
weight_ = module.weight.data[rows,:]
sz = weight_.shape
module.weight.data[rows,:] = ((1 + beta) * weight_ - beta * weight_.matmul(weight_.t()).matmul(weight_)).reshape(sz)
def convex_constraint(model):
with torch.no_grad():
for module in model.modules():
if isinstance(module, ConvexCombination):
comb = module.comb.data
alpha = torch.sort(comb, descending=True)[0]
k = 1
for j in range(1,module.n+1):
if (1 + j * alpha[j-1]) > torch.sum(alpha[:j]):
k = j
else:
break
gamma = (torch.sum(alpha[:k]) - 1)/k
module.comb.data -= gamma
torch.relu_(module.comb.data)