forked from UCSC-REAL/SelfSup_NoisyLabel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloss.py
156 lines (112 loc) · 4.63 KB
/
loss.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
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
import numpy as np
import random
import math
# Loss functions
def loss_peer(epoch, outputs, labels):
beta = f_beta(epoch)
# if epoch == 1:
# print(f'current beta is {beta}')
loss = F.cross_entropy(outputs, labels, reduce = False)
#num_batch = len(loss_numpy)
#loss_v = np.zeros(num_batch)
#oss_div_numpy = float(np.array(0))
loss_ = -torch.log(F.softmax(outputs) + 1e-8)
loss = loss - beta*torch.mean(loss_,1)
return torch.mean(loss)
def f_beta(epoch):
beta1 = np.linspace(0.0, 0.0, num=10)
beta2 = np.linspace(0.0, 2, num=30)
beta3 = np.linspace(2, 2, num=160)
beta = np.concatenate((beta1,beta2,beta3),axis=0)
return beta[epoch]
def DMI_loss(epoch,output, target):
outputs = F.softmax(output, dim=1)
targets = target.reshape(target.size(0), 1).cpu()
y_onehot = torch.FloatTensor(target.size(0), 10).zero_()
y_onehot.scatter_(1, targets, 1)
y_onehot = y_onehot.transpose(0, 1).cuda()
mat = y_onehot @ outputs
return -1.0 * torch.log(torch.abs(torch.det(mat.float())) + 0.001)
def lq_loss(epoch,outputs, target):
# loss = (1 - h_j(x)^q) / q
loss = 0
outputs = F.softmax(outputs, dim=1)
for i in range(outputs.size(0)):
loss += (1.0 - (outputs[i][target[i]]) ** 0.2) / 0.2
loss = loss / outputs.size(0)
return loss
def sce_loss(epoch, output, target):
# l_{forward}(y, h(x)) = l_{ce}(y, h(x) @ T)
if epoch <=30:
return F.cross_entropy(output, target)
else:
alpha = 0.1
beta = 1.0
num_classes = 10
ce = F.cross_entropy(output, target)
# RCE
pred = F.softmax(output, dim=1)
pred = torch.clamp(pred, min=1e-7, max=1.0)
label_one_hot = torch.nn.functional.one_hot(target, num_classes).float().to('cuda')
label_one_hot = torch.clamp(label_one_hot, min=1e-4, max=1.0)
rce = (-1*torch.sum(pred * torch.log(label_one_hot), dim=1))
# Loss
loss = alpha * ce + beta * rce.mean()
return loss
class RkdDistance(nn.Module):
def forward(self, student, teacher,self_correct = True):
student = F.softmax(student)
t_d = pdist(teacher, squared=True)
mean_td = t_d[t_d>0].mean()
t_d = t_d / mean_td
d = pdist(student, squared=False)
mean_d = d[d>0].mean()
d = d / mean_d
loss = F.smooth_l1_loss(d, t_d, reduction='elementwise_mean')
return loss
class RKdAngle(nn.Module):
def forward(self, student, teacher,self_correct = True):
# N x C
# N x N x C
td = (teacher.unsqueeze(0) - teacher.unsqueeze(1))
norm_td = F.normalize(td, p=2, dim=2)
t_angle = torch.bmm(norm_td, norm_td.transpose(1, 2)).view(-1)
sd = (student.unsqueeze(0) - student.unsqueeze(1))
norm_sd = F.normalize(sd, p=2, dim=2)
s_angle = torch.bmm(norm_sd, norm_sd.transpose(1, 2)).view(-1)
loss = F.smooth_l1_loss(s_angle, t_angle, reduction='elementwise_mean')
return loss
class Info_NCE(nn.Module):
def __init__(self, temperature=0.5):
super(Info_NCE, self).__init__()
self.temperature = temperature
self.cross_entropy = torch.nn.CrossEntropyLoss(reduction="mean")
def forward(self, x, y):
batchsize = x.size(0)
x = torch.nn.functional.normalize(x, dim=1)
y = torch.nn.functional.normalize(y, dim=1)
output = torch.cat((x, y),0)
logits = torch.einsum('nc,mc->nm', output, output) / self.temperature
zero_matrix = torch.zeros((batchsize,batchsize),dtype=torch.bool,device=x.device)
eye_matrix = torch.eye(batchsize, dtype=torch.bool, device=x.device)
pos_index = torch.cat((torch.cat((zero_matrix,eye_matrix)),torch.cat((eye_matrix,zero_matrix))),1 )
neg_index = ~torch.cat((torch.cat((eye_matrix,eye_matrix)),torch.cat((eye_matrix,eye_matrix))),1 )
pos_logits = logits[pos_index].view(2*batchsize, -1)
neg_index = logits[neg_index].view(2*batchsize, -1)
final_logits = torch.cat((pos_logits, neg_index), dim=1)
labels = torch.zeros(final_logits.shape[0], device=x.device, dtype=torch.long)
loss = self.cross_entropy(final_logits, labels)
return loss
def pdist(e, squared=False, eps=1e-12):
e_square = e.pow(2).sum(dim=1)
prod = e @ e.t()
res = (e_square.unsqueeze(1) + e_square.unsqueeze(0) - 2 * prod).clamp(min=eps)
if not squared:
res = res.sqrt()
res = res.clone()
res[range(len(e)), range(len(e))] = 0
return res