-
Notifications
You must be signed in to change notification settings - Fork 6
/
loss_utils.py
195 lines (165 loc) · 7.37 KB
/
loss_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
import glob
import os.path
from PIL import Image
import torch
import torch.nn.functional as F
from torchvision import transforms
def NSS_loss(input, saliency, target): # This NSS loss is UNISAL paper's implementation, note that the NSS loss weight in their paper is -0.1
pred, fixations=input, target
size = pred.size()
new_size = (-1, size[-1] * size[-2])
pred = pred.reshape(new_size)
fixations = fixations.reshape(new_size)
pred_normed = (pred - pred.mean(-1, True)) / pred.std(-1, keepdim=True)
results = []
for this_pred_normed, mask in zip(torch.unbind(pred_normed, 0),
torch.unbind(fixations, 0)):
if mask.sum() == 0:
print("No fixations.")
results.append(torch.ones([]).float().to(fixations.device))
continue
nss_ = torch.masked_select(this_pred_normed, mask)
nss_ = nss_.mean(-1)
results.append(nss_)
results = torch.stack(results)
results = results.reshape(size[:2])
return results
def NSS_loss_v1(input, saliency, target): # This NSS loss is EMLNet paper's implementation,, note that the NSS loss weight in their paper is 1
input=input.float()
saliency=saliency.float()
target=target.float()
ref = (target - target.mean()) / target.std()
input = (input - input.mean()) / input.std()
loss = (ref*target - input*target).sum(-1).sum(-1).sum(-1) / target.sum(-1).sum(-1).sum(-1)
return loss
def SFNE_loss_v1(input, saliency, target): # This SFNE loss (based on NSS loss) is our implementation v1
input=input.float()
saliency=saliency.float()
target=target.float()
ref = (saliency - saliency.mean()) / saliency.std()
input = (input - input.mean()) / input.std()
#loss = torch.abs(ref*target - input*target).sum(-1).sum(-1).sum(-1) / target.sum(-1).sum(-1).sum(-1)
loss = torch.pow((ref*target - input*target),2).sum(-1).sum(-1).sum(-1) / target.sum(-1).sum(-1).sum(-1)
return loss
def SFNE_loss(input, saliency, target, nontarget): # This SFNE loss (based on NSS loss) is our implementation
input=input.float()
saliency=saliency.float()
target=target.float()
nontarget=nontarget.float()
ref = (saliency - saliency.mean()) / saliency.std()
input = (input - input.mean()) / input.std()
#loss = torch.abs(ref*target - input*target).sum(-1).sum(-1).sum(-1) / target.sum(-1).sum(-1).sum(-1)
loss1 = torch.pow((ref*target - input*target),2).sum(-1).sum(-1).sum(-1) / target.sum(-1).sum(-1).sum(-1) # salient points
loss2 = torch.pow((ref*nontarget - input*nontarget),2).sum(-1).sum(-1).sum(-1) / nontarget.sum(-1).sum(-1).sum(-1) # nonsalient points
loss=loss1+loss2
return loss
def NSS_loss_v2(input, saliency, target): # This NSS loss is our SFNE_loss implementation plus unisal NSS_loss1 implementation
loss_nss1=NSS_loss(input, saliency, target)
loss_nss2=SFNE_loss(input, saliency, target)
loss=(-1)*loss_nss1+loss_nss2
return loss
def corr_coeff(pred, target):
size = pred.size()
new_size = (-1, size[-1] * size[-2])
pred = pred.reshape(new_size)
target = target.reshape(new_size)
cc = []
for x, y in zip(torch.unbind(pred, 0), torch.unbind(target, 0)):
xm, ym = x - x.mean(), y - y.mean()
r_num = torch.mean(xm * ym)
r_den = torch.sqrt(
torch.mean(torch.pow(xm, 2)) * torch.mean(torch.pow(ym, 2)))
r = r_num / r_den
cc.append(r)
cc = torch.stack(cc)
cc = cc.reshape(size[:2])
#return cc # 1 - torch.square(r), note that the Unisal paper use (-0.1)*cc as cc loss
return 1-cc # here we use 1-cc as cc loss
def corr_coeff_v2(pred, target):
size = pred.size()
new_size = (-1, size[-1] * size[-2])
pred = pred.reshape(new_size)
target = target.reshape(new_size)
cc = []
for x, y in zip(torch.unbind(pred, 0), torch.unbind(target, 0)):
xm, ym = x - x.mean(), y - y.mean()
r_num = torch.mean(xm * ym)
r_den = torch.sqrt(
torch.mean(torch.pow(xm, 2)) * torch.mean(torch.pow(ym, 2)))
r = r_num / r_den
cc.append(r)
cc = torch.stack(cc)
cc = cc.reshape(size[:2])
return cc # 1 - torch.square(r), note that the Unisal paper use (-0.1)*cc as cc loss
def kld_loss(pred, target):
loss = F.kl_div(pred, target, reduction='none')
loss = loss.sum(-1).sum(-1).sum(-1)
return loss
def loss_sequences(pred_seq, sal_seq, fix_seq, nonfix, metrics):
"""
Compute the training losses
"""
# sal_seq = softmax(sal_seq)
# pred_seq = log_softmax(pred_seq)
losses = []
for this_metric in metrics:
if this_metric == 'kld':
losses.append(kld_loss(log_softmax(pred_seq), softmax(sal_seq)))
if this_metric == 'nss':
losses.append(NSS_loss(pred_seq, sal_seq, fix_seq))
if this_metric == 'cc':
losses.append(corr_coeff(pred_seq, sal_seq))
if this_metric == 'sfne':
losses.append(SFNE_loss(pred_seq, sal_seq, fix_seq, nonfix))
return losses
def loss_sequences_val(pred_seq, sal_seq, fix_seq, nonfix, metrics):
"""
Compute the training losses
"""
# sal_seq = softmax(sal_seq)
# pred_seq = log_softmax(pred_seq)
losses = []
for this_metric in metrics:
if this_metric == 'kld':
losses.append(kld_loss(log_softmax(pred_seq), softmax(sal_seq))) # real KLD metric
if this_metric == 'nss':
losses.append(NSS_loss(pred_seq, sal_seq, fix_seq)) # real NSS metric
if this_metric == 'cc':
losses.append(corr_coeff_v2(pred_seq, sal_seq)) # real CC metric
return losses
def softmax(x):
x_size = x.size()
x = x.view(x.size(0), -1)
x = F.softmax(x, dim=1)
return x.view(x_size)
def log_softmax(x):
x_size = x.size()
x = x.view(x.size(0), -1)
x = F.log_softmax(x, dim=1)
return x.view(x_size)
def calc_loss_unisal(pred_seq, sal, fix, nonfix, loss_metrics=('kld', 'nss', 'cc', 'sfne'), loss_weights=(1.0, -1.0, 1.0, 1.0)):
assert len(pred_seq.shape) == 4 and len(sal.shape) == 4 and len(fix.shape) == 4
sal = torch.unsqueeze(sal, 1) # [b, nframe, c, w, h]
fix = torch.unsqueeze(fix, 1) # [b, nframe, c, w, h]
nonfix = torch.unsqueeze(nonfix, 1) # [b, nframe, c, w, h]
pred_seq = torch.unsqueeze(pred_seq, 1) #[b, nframe, c, w, h]
# Compute the total loss
loss_summands = loss_sequences(
pred_seq, sal, fix, nonfix, metrics=loss_metrics)
loss_summands = [l.mean(1).mean(0) for l in loss_summands]
loss = sum(weight * l for weight, l in
zip(loss_weights, loss_summands))
return loss
def calc_loss_unisal_val(pred_seq, sal, fix, nonfix, loss_metrics=('kld', 'nss', 'cc'), loss_weights=(-1.0, 1.0, 1.0)): # real KLD, NSS, CC metrics for validation
assert len(pred_seq.shape) == 4 and len(sal.shape) == 4 and len(fix.shape) == 4
sal = torch.unsqueeze(sal, 1) # [b, nframe, c, w, h]
fix = torch.unsqueeze(fix, 1) # [b, nframe, c, w, h]
nonfix = torch.unsqueeze(nonfix, 1) # [b, nframe, c, w, h]
pred_seq = torch.unsqueeze(pred_seq, 1) #[b, nframe, c, w, h]
# Compute the total loss
loss_summands = loss_sequences_val(
pred_seq, sal, fix, nonfix, metrics=loss_metrics)
loss_summands = [l.mean(1).mean(0) for l in loss_summands]
loss = sum(weight * l for weight, l in
zip(loss_weights, loss_summands))
return loss