-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathae_grad_reg.py
171 lines (136 loc) · 6.52 KB
/
ae_grad_reg.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
import torch
import torch.nn.functional as func
import numpy as np
import utils
def train(model, device, train_loader, optimizer, epoch, print_freq, grad_loss_weight, ref_grad, nlayer):
"""
Args:
model (DataParallel(module)): VAE
device (Device): CPU or GPU
train_loader (DataLoader): data loader for training data
optimizer
epoch (int)
print_freq(int): Print frequency
grad_loss_weight (float): Weight for the gradient loss
ref_grad (tensor): Average of gradients generated while training
nlayer (int): Number of decoder layers
"""
model.train()
losses = utils.AverageMeter()
recon_losses = utils.AverageMeter()
grad_losses = utils.AverageMeter()
for batch_idx, (data, target_data, label) in enumerate(train_loader):
data = data.to(device)
target_data = target_data.to(device)
optimizer.zero_grad()
model.zero_grad()
recon_batch = model(data)
recon_loss = func.mse_loss(recon_batch, target_data)
# Calculate the gradient loss for each layer
grad_loss = 0
for i in range(nlayer):
wrt = model.module.up[int(2*i)].weight
target_grad = torch.autograd.grad(recon_loss, wrt, create_graph=True, retain_graph=True)[0]
grad_loss += -1 * func.cosine_similarity(target_grad.view(-1, 1),
ref_grad[i].avg.view(-1, 1), dim=0)
# In the first iteration, since there is no history of training gradients, gradient loss is not utilized
if ref_grad[0].count == 0:
grad_loss = torch.FloatTensor([0.0]).to(device)
else:
grad_loss = grad_loss / nlayer
loss = recon_loss + grad_loss_weight * grad_loss
losses.update(loss.item(), data.size(0)) # data.size(0): Batch size
recon_losses.update(recon_loss.item(), data.size(0))
grad_losses.update(grad_loss.item(), data.size(0))
loss.backward()
# Update the reference gradient
for i in range(nlayer):
ref_grad[i].update(model.module.up[2*i].weight.grad, 1)
optimizer.step()
if batch_idx % print_freq == 0:
print('Epoch: [{0}][{1}/{2}]\t'
'Loss {loss.val:.4f} ({loss.avg:.4f}) Recon Loss {recon_loss.val:.4f} ({recon_loss.avg:.4f}) '
'Grad Loss {grad_loss.val:.4f} ({grad_loss.avg:.4f})'
.format(epoch, batch_idx, len(train_loader), loss=losses, recon_loss=recon_losses,
grad_loss=grad_losses))
def test(model, device, test_loader, epoch, print_freq, grad_loss_weight, ref_grad, nlayer):
"""
Args:
model (DataParallel(module)): VAE
device (Device): CPU or GPU
test_loader (DataLoader): data loader for test data
epoch (int)
print_freq(int): Print frequency
grad_loss_weight (float): Weight for the gradient loss
ref_grad (tensor): Average of gradients generated while training
nlayer (int): Number of decoder layers
"""
model.eval()
losses = utils.AverageMeter()
recon_losses = utils.AverageMeter()
grad_losses = utils.AverageMeter()
for batch_idx, (data, target_data, label) in enumerate(test_loader):
data = data.to(device)
target_data = target_data.to(device)
model.zero_grad()
recon_batch = model(data)
recon_loss = func.mse_loss(recon_batch, target_data)
# Calculate the gradient loss for each layer
grad_loss = 0
for i in range(nlayer):
wrt = model.module.up[int(2*i)].weight
target_grad = torch.autograd.grad(recon_loss, wrt, create_graph=True, retain_graph=True)[0]
grad_loss += -1 * func.cosine_similarity(target_grad.view(-1, 1),
ref_grad[i].avg.view(-1, 1), dim=0)
grad_loss = grad_loss / nlayer
loss = recon_loss + grad_loss_weight * grad_loss
losses.update(loss.item(), data.size(0))
recon_losses.update(recon_loss.item(), data.size(0))
grad_losses.update(grad_loss.item(), data.size(0))
if batch_idx == 0:
nimg = 3 # Visualize three sample images on Tensorboard
input_img = data[:nimg]
recon_img = recon_batch[:nimg, :].view(nimg, data.shape[1], data.shape[2], data.shape[3])
target_img = target_data[:nimg]
if batch_idx % print_freq == 0:
print('Epoch: [{0}][{1}/{2}]\t'
'Loss {loss.val:.4f} ({loss.avg:.4f}) Recon Loss {recon_loss.val:.4f} ({recon_loss.avg:.4f}) '
'Grad Loss {grad_loss.val:.4f} ({grad_loss.avg:.4f})'
.format(epoch, batch_idx, len(test_loader), loss=losses, recon_loss=recon_losses,
grad_loss=grad_losses))
print(' * Loss {loss.avg:.3f}'.format(loss=losses))
return losses.avg, recon_losses.avg, grad_losses.avg, input_img, recon_img, target_img
def gradcon_score(model, in_cls, grad_loss_weight, ref_grad, nlayer, device, test_loader):
"""
Args:
model (DataParallel(module)): AE
in_cls (int): Inlier class
grad_loss_weight (float): Weight for the gradient loss
ref_grad (list): Extracted gradients from training data
nlayer (int): Number of decoder layers
device (Device): CPU or GPU
test_loader (DataLoader): Data loader for test data
Return:
result (ndarray): (number of samples) x 2 (label, estimated score)
"""
model.eval()
results = np.zeros([len(test_loader.dataset), 2])
for batch_idx, (data, target_data, class_label) in enumerate(test_loader):
if batch_idx % 10 == 0:
print('Evaluation inlier {0}: [{1} / {2}]...'.format(in_cls, batch_idx, len(test_loader)))
data = data.to(device)
target_data = target_data.to(device)
model.zero_grad()
recon_batch = model(data)
recon_loss = func.mse_loss(recon_batch, target_data)
recon_loss.backward()
grad_loss = 0
for i in range(nlayer):
target_grad = model.module.up[int(2*i)].weight.grad
grad_loss += 1 * func.cosine_similarity(target_grad.view(-1, 1), ref_grad[i].avg.view(-1, 1), dim=0)
grad_loss = grad_loss / nlayer
score = -1 * recon_loss + grad_loss_weight * grad_loss
inout_label = 1 if class_label == in_cls else 0
results[batch_idx, 0] = inout_label
results[batch_idx, 1] = score.cpu().detach().numpy()
return results