-
Notifications
You must be signed in to change notification settings - Fork 32
/
arcface.py
90 lines (69 loc) · 2.67 KB
/
arcface.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
import torch
import torch.nn as nn
import math
class FocalLoss(nn.Module):
def __init__(self, gamma=0, eps=1e-7):
super(FocalLoss, self).__init__()
self.gamma = gamma
#print(self.gamma)
self.eps = eps
self.ce = torch.nn.CrossEntropyLoss(reduction="none")
def forward(self, input, target):
logp = self.ce(input, target)
p = torch.exp(-logp)
loss = (1 - p) ** self.gamma * logp
return loss.mean()
class ArcFaceLoss(nn.modules.Module):
def __init__(self, s=45.0, m=0.1, crit="bce", weight=None, reduction="mean", class_weights_norm='batch'):
super().__init__()
self.weight = weight
self.reduction = reduction
self.class_weights_norm = class_weights_norm
if crit == "focal":
self.crit = FocalLoss(gamma=2)
elif crit == "bce":
self.crit = nn.CrossEntropyLoss(reduction="none")
if s is None:
self.s = torch.nn.Parameter(torch.tensor([45.], requires_grad=True, device='cuda'))
else:
self.s = s
self.cos_m = math.cos(m)
self.sin_m = math.sin(m)
self.th = math.cos(math.pi - m)
self.mm = math.sin(math.pi - m) * m
def forward(self, logits, labels):
logits = logits.float()
cosine = logits
sine = torch.sqrt(1.0 - torch.pow(cosine, 2))
phi = cosine * self.cos_m - sine * self.sin_m
phi = torch.where(cosine > self.th, phi, cosine - self.mm)
labels2 = torch.zeros_like(cosine)
labels2.scatter_(1, labels.view(-1, 1).long(), 1)
output = (labels2 * phi) + ((1.0 - labels2) * cosine)
s = self.s
output = output * s
loss = self.crit(output, labels)
if self.weight is not None:
w = self.weight[labels].to(logits.device)
loss = loss * w
if self.class_weights_norm == "batch":
loss = loss.sum() / w.sum()
if self.class_weights_norm == "global":
loss = loss.mean()
else:
loss = loss.mean()
return loss
if self.reduction == "mean":
loss = loss.mean()
elif self.reduction == "sum":
loss = loss.sum()
return loss
def loss_fn(metric_crit, target_dict, output_dict, num_classes, device):
y_true = target_dict['target'].to(device)
y_pred = output_dict['logits'].to(device)
#ignore invalid classes for val loss
mask = y_true < num_classes
if mask.sum() == 0:
return torch.zeros(1, device = y_pred.device)
loss = metric_crit(y_pred[mask], y_true[mask])
return loss