-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlosses.py
153 lines (89 loc) · 4.12 KB
/
losses.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
import torch
import torch.nn as nn
from torch.nn import functional as F
class InstProtoCLR(nn.Module):
def __init__(self, temperature,devices):
super(InstProtoCLR, self).__init__()
self.ce = nn.CrossEntropyLoss(reduction='none')
self.temperature = temperature
self.devices = devices
def forward(self, anchor, prototypes, peso_labels):
batch_size = anchor.size(0)
anchor = F.normalize(anchor)
prototypes = F.normalize(prototypes).to(self.devices)
pos_proto_id = peso_labels
pos_prototypes = prototypes[pos_proto_id]
proto_similarity_matrix = torch.matmul(anchor, pos_prototypes.T)
mask = torch.eye(batch_size, dtype=torch.bool)
assert proto_similarity_matrix.shape == mask.shape
proto_positives = proto_similarity_matrix[mask].view(batch_size, -1)
proto_negatives = proto_similarity_matrix[~mask].view(batch_size, -1)
proto_logits = torch.cat([proto_positives, proto_negatives], dim=1)
proto_labels = torch.zeros(batch_size, dtype=torch.long).to(self.devices)
proto_logits /= self.temperature
loss_proto = self.ce(proto_logits, proto_labels)
w = torch.ones(batch_size).to(self.devices)
loss_proto = torch.sum(w*(loss_proto))/ torch.sum(w)
return loss_proto
class ContrastiveLoss(nn.Module):
def __init__(self, margin=0, shift=2., measure=False, max_violation=False):
super(ContrastiveLoss, self).__init__()
self.margin = margin
self.shift = shift
if measure == 'order':
self.sim = order_sim
else:
self.sim = lambda x, y: x.mm(y.t())
self.max_violation = max_violation
self.count = 1
def set_margin(self, margin):
self.margin = margin
def loss_func(self, cost, tau):
cost = (cost - cost.diag().reshape([-1, 1])).exp()
I = (cost.diag().diag() == 0)
return cost[I].sum() / (cost.shape[0] * (cost.shape[0] - 1))
def forward(self, im, s=None, tau=1., lab=None):
if s is None:
scores = im
diagonal = im[:, 0].view(im.size(0), 1)
d1 = diagonal.expand_as(scores)
cost = (self.margin + scores - d1).clamp(min=0)
if self.max_violation:
cost = cost.max(1)[0]
return cost.sum()
else:
scores = self.sim(im, s)
self.count += 1
diagonal = scores.diag().view(im.size(0), 1)
d1 = diagonal.expand_as(scores)
d2 = diagonal.t().expand_as(scores)
mask_s = (scores >= (d1 - self.margin)).float().detach()
cost_s = scores * mask_s + (1. - mask_s) * (scores - self.shift)
mask_im = (scores >= (d2 - self.margin)).float().detach()
cost_im = scores * mask_im + (1. - mask_im) * (scores - self.shift)
loss = (-cost_s.diag() + tau * (cost_s / tau).exp().sum(1).log() + self.margin).mean() + (-cost_im.diag() + tau * (cost_im / tau).exp().sum(0).log() + self.margin).mean()
return loss
class InstProtoRC(nn.Module):
def __init__(self,devices):
super(InstProtoRC, self).__init__()
self.mse = nn.MSELoss()
self.devices = devices
def forward(self,args,F_I,F_T,code_I,code_T,prototypes):
batch_size = F_I.size(0)
F_I = F.normalize(F_I)
F_T = F.normalize(F_T)
prototypes = F.normalize(prototypes).to(self.devices)
q1 = torch.matmul(F_I,prototypes.T)
qi = torch.softmax(q1,dim=1)
S_ii = torch.mm(qi,qi.T)
q2 = torch.matmul(F_T,prototypes.T)
qt = torch.softmax(q2,dim=1)
S_tt = torch.mm(qt,qt.T)
B_I = F.normalize(code_I)
B_T = F.normalize(code_T)
BI_BI = B_I.mm(B_I.t())
BT_BT = B_T.mm(B_T.t())
BI_BT = B_I.mm(B_T.t())
S = S_ii + S_tt
loss2 = self.mse(BI_BT, S)
return loss2