-
Notifications
You must be signed in to change notification settings - Fork 0
/
loss_fct.py
46 lines (40 loc) · 1.49 KB
/
loss_fct.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
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
# 支持多分类和二分类
class FocalLoss(nn.Module):
def __init__(self, num_class, alpha=None, gamma=0,
size_average=True):
super(FocalLoss, self).__init__()
self.num_class = num_class
self.alpha = alpha
self.gamma = gamma
self.size_average = size_average
if self.alpha is None:
self.alpha = torch.ones(1, self.num_class)
elif isinstance(self.alpha, (list, np.ndarray)):
assert len(self.alpha) == self.num_class
self.alpha = torch.FloatTensor(alpha).view(self.num_class, 1)
self.alpha = self.alpha / self.alpha.sum()
else:
raise TypeError('Not support alpha type')
def forward(self, logit, target):
batch_size = logit.shape[0]
logit = logit.view(batch_size, -1)
target = target.view(batch_size, -1)
epsilon = 1e-10
alpha = self.alpha
if alpha.device != logit.device:
alpha = alpha.to(logit.device)
pos_pt = logit
neg_pt = 1 - logit
gamma = self.gamma
pos_loss = torch.pow((1 - pos_pt), gamma) * torch.log(pos_pt) * target
neg_loss = torch.pow((1 - neg_pt), gamma) * torch.log(neg_pt) * (1 - target)
loss = -1 * (pos_loss + neg_loss)
if self.size_average:
loss = loss.sum() / batch_size
else:
loss = loss.sum()
return loss