-
Notifications
You must be signed in to change notification settings - Fork 9
/
losses.py
executable file
·97 lines (82 loc) · 3.21 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
import math
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
from opts import parser
args = parser.parse_args()
if args.dataset == 'cifar100':
num_classes = 100
elif args.dataset == 'cifar10':
num_classes = 10
elif args.dataset == 'tinyimagenet':
num_classes = 200
else: #iNat18
num_classes = 8142
def ib_loss(input_values, ib):
"""Computes the focal loss"""
loss = input_values * ib
return loss.mean()
class IBLoss(nn.Module):
def __init__(self, weight=None, alpha=10000.):
super(IBLoss, self).__init__()
assert alpha > 0
self.alpha = alpha
self.epsilon = 0.001
self.weight = weight
def forward(self, input, target, features):
grads = torch.sum(torch.abs(F.softmax(input, dim=1) - F.one_hot(target, num_classes)),1) # N * 1
ib = grads*features.reshape(-1)
ib = self.alpha / (ib + self.epsilon)
return ib_loss(F.cross_entropy(input, target, reduction='none', weight=self.weight), ib)
def ib_focal_loss(input_values, ib, gamma):
"""Computes the ib focal loss"""
p = torch.exp(-input_values)
loss = (1 - p) ** gamma * input_values * ib
return loss.mean()
class IB_FocalLoss(nn.Module):
def __init__(self, weight=None, alpha=10000., gamma=0.):
super(IB_FocalLoss, self).__init__()
assert alpha > 0
self.alpha = alpha
self.epsilon = 0.001
self.weight = weight
self.gamma = gamma
def forward(self, input, target, features):
grads = torch.sum(torch.abs(F.softmax(input, dim=1) - F.one_hot(target, num_classes)),1) # N * 1
ib = grads*(features.reshape(-1))
ib = self.alpha / (ib + self.epsilon)
return ib_focal_loss(F.cross_entropy(input, target, reduction='none', weight=self.weight), ib, self.gamma)
def focal_loss(input_values, gamma):
"""Computes the focal loss"""
p = torch.exp(-input_values)
#loss = (1 - p) ** gamma * input_values
loss = (1- p) ** gamma * input_values * 10
return loss.mean()
class FocalLoss(nn.Module):
def __init__(self, weight=None, gamma=0.):
super(FocalLoss, self).__init__()
assert gamma >= 0
self.gamma = gamma
self.weight = weight
def forward(self, input, target):
return focal_loss(F.cross_entropy(input, target, reduction='none', weight=self.weight), self.gamma)
class LDAMLoss(nn.Module):
def __init__(self, cls_num_list, max_m=0.5, weight=None, s=30):
super(LDAMLoss, self).__init__()
m_list = 1.0 / np.sqrt(np.sqrt(cls_num_list))
m_list = m_list * (max_m / np.max(m_list))
m_list = torch.cuda.FloatTensor(m_list)
self.m_list = m_list
assert s > 0
self.s = s
self.weight = weight
def forward(self, x, target):
index = torch.zeros_like(x, dtype=torch.uint8)
index.scatter_(1, target.data.view(-1, 1), 1)
index_float = index.type(torch.cuda.FloatTensor)
batch_m = torch.matmul(self.m_list[None, :], index_float.transpose(0,1))
batch_m = batch_m.view((-1, 1))
x_m = x - batch_m
output = torch.where(index, x_m, x)
return F.cross_entropy(self.s*output, target, weight=self.weight)