-
Notifications
You must be signed in to change notification settings - Fork 173
/
loss_functions.py
149 lines (121 loc) · 4.76 KB
/
loss_functions.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
from __future__ import print_function, division
import torch
import torch.nn as nn
import embedding_utils
from torch.autograd import Variable
import random
import Options
opt = Options.Config()
class GANLoss(nn.Module):
def __init__(self, use_lsgan=False, target_real_label=1.0, target_fake_label=0.0,
tensor=torch.FloatTensor, softlabel=False):
super(GANLoss, self).__init__()
self.real_label = target_real_label
self.fake_label = target_fake_label
self.real_label_var = None
self.fake_label_var = None
self.Tensor = tensor
self.softlabel = softlabel
if use_lsgan:
self.loss = nn.MSELoss()
else:
self.loss = nn.BCELoss()
def get_target_tensor(self, input, target_is_real):
target_tensor = None
if self.softlabel:
soft = random.random() * 0.1
else:
soft = 0
if target_is_real:
create_label = ((self.real_label_var is None) or
(self.real_label_var.numel() != input.numel()))
if create_label:
real_tensor = self.Tensor(input.size()).fill_(self.real_label - soft)
self.real_label_var = Variable(real_tensor, requires_grad=False)
target_tensor = self.real_label_var
else:
create_label = ((self.fake_label_var is None) or
(self.fake_label_var.numel() != input.numel()))
if create_label:
fake_tensor = self.Tensor(input.size()).fill_(self.fake_label + soft)
self.fake_label_var = Variable(fake_tensor, requires_grad=False)
target_tensor = self.fake_label_var
return target_tensor
def __call__(self, input, target_is_real):
target_tensor = self.get_target_tensor(input, target_is_real)
return self.loss(input, target_tensor)
class ContrastiveLoss(nn.Module):
"""
Compute contrastive loss
"""
def __init__(self, margin=0, measure=False, max_violation=False):
super(ContrastiveLoss, self).__init__()
self.margin = margin
self.sim = embedding_utils.sim
self.max_violation = max_violation
def forward(self, im, s):
# compute image-sentence score matrix
scores = self.sim(im, s)
diagonal = scores.diag().view(im.size(0), 1)
d1 = diagonal.expand_as(scores)
# compare every diagonal score to scores in its column
# caption retrieval
cost_s = (self.margin + scores - d1).clamp(min=0)
# clear diagonals
mask = torch.eye(scores.size(0)) > .5
I = Variable(mask)
if torch.cuda.is_available():
I = I.cuda()
cost_s = cost_s.masked_fill_(I, 0)
# keep the maximum violating negative for each query
if self.max_violation:
cost_s = cost_s.max(1)[0]
return cost_s.sum()
class SumLogSoftmaxLoss(nn.Module):
def __init__(self, opt=opt):
super(SumLogSoftmaxLoss, self).__init__()
self.logsoftmax = nn.LogSoftmax()
def forward(self, x):
out = self.logsoftmax(x)
loss = - torch.mean(out)
return loss
class L2SoftmaxLoss(nn.Module):
def __init__(self, opt=opt):
super(L2SoftmaxLoss, self).__init__()
self.softmax = nn.Softmax()
self.L2loss = nn.MSELoss()
self.label = None
def forward(self, x):
out = self.softmax(x)
self.label = Variable(torch.ones(out.size()).float() * (1 / x.size(1)), requires_grad=False).cuda()
loss = self.L2loss(out, self.label)
return loss
class L2ContrastiveLoss(nn.Module):
"""
Compute L2 contrastive loss
"""
def __init__(self, margin=0, measure=False, max_violation=False):
super(L2ContrastiveLoss, self).__init__()
self.margin = margin
self.sim = embedding_utils.l2_sim
self.max_violation = max_violation
def forward(self, feature1, feature2):
# compute image-sentence score matrix
scores = self.sim(feature1, feature2)
# diagonal = scores.diag().view(feature1.size(0), 1)
diagonal_dist = scores.diag()
# d1 = diagonal.expand_as(scores)
# compare every diagonal score to scores in its column
# caption retrieval
cost_s = (self.margin - scores).clamp(min=0)
# clear diagonals
mask = torch.eye(scores.size(0)) > .5
I = Variable(mask)
if torch.cuda.is_available():
I = I.cuda()
cost_s = cost_s.masked_fill_(I, 0)
# keep the maximum violating negative for each query
if self.max_violation:
cost_s = cost_s.max(1)[0]
loss = (torch.sum(cost_s ** 2) + torch.sum(diagonal_dist ** 2)) / (2 * feature1.size(0))
return loss