-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloss.py
58 lines (46 loc) · 1.55 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
import torch
import torch.nn as nn
import torch.nn.functional as F
class SoftmaxFocalLoss(nn.Module):
def __init__(self,gamma=2.0,weight=None,reduction="mean"):
super().__init__()
self.weight=weight
self.gamma=gamma
assert reduction in ["sum","mean","none"]
self.reduction=reduction
def forward(self,input,target):
assert self.weight is None or isinstance(self.weight, torch.Tensor)
ce = F.cross_entropy(input, target,reduction="none").view(-1)
pt=torch.exp(-ce)
if self.weight!=None:
target=target.view(-1)
weights=self.weight[target]
else:
weights=torch.ones_like(target).view(-1)
focal=weights*((1-pt)**self.gamma)
if self.reduction=="mean":
return (focal*ce).sum()/weights.sum()
elif self.reduction=="sum":
return (focal*ce).sum()
else:
return focal*ce
if __name__ == "__main__":
torch.manual_seed(123)
weights=torch.Tensor([0.75,0.25])
fl=SoftmaxFocalLoss(gamma=2,weight=weights,reduction="mean")
#multiclass classfication
pred=torch.rand(3,2)
trg=torch.randint(0,2,(3,))
a=fl.forward(pred,trg)
print("1.multiclass classfication:\n")
print(pred,"\n")
print(trg,"\n")
print("loss:",a,"\n")
#multiclass segmentation
pred=torch.rand(1,2,3,3)
trg=torch.randint(0,2,(1,3,3))
b=fl.forward(pred,trg)
print("2.multiclass segmentation:\n")
print(pred,"\n")
print(trg,"\n")
print("loss:",b,"\n")