-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloss.py
224 lines (178 loc) · 8.28 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
from typing import Optional
def label_to_one_hot_label(
labels: torch.Tensor,
num_classes: int,
device: Optional[torch.device] = None,
dtype: Optional[torch.dtype] = None,
eps: float = 1e-6,
ignore_index=255,
) -> torch.Tensor:
r"""Convert an integer label x-D tensor to a one-hot (x+1)-D tensor.
Args:
labels: tensor with labels of shape :math:`(N, *)`, where N is batch size.
Each value is an integer representing correct classification.
num_classes: number of classes in labels.
device: the desired device of returned tensor.
dtype: the desired data type of returned tensor.
Returns:
the labels in one hot tensor of shape :math:`(N, C, *)`,
Examples:
>>> labels = torch.LongTensor([
[[0, 1],
[2, 0]]
])
>>> one_hot(labels, num_classes=3)
tensor([[[[1.0000e+00, 1.0000e-06],
[1.0000e-06, 1.0000e+00]],
[[1.0000e-06, 1.0000e+00],
[1.0000e-06, 1.0000e-06]],
[[1.0000e-06, 1.0000e-06],
[1.0000e+00, 1.0000e-06]]]])
"""
shape = labels.shape
# one hot : (B, C=ignore_index+1, H, W)
one_hot = torch.zeros((shape[0], ignore_index+1) + shape[1:], device=device, dtype=dtype)
# labels : (B, H, W)
# labels.unsqueeze(1) : (B, C=1, H, W)
# one_hot : (B, C=ignore_index+1, H, W)
one_hot = one_hot.scatter_(1, labels.unsqueeze(1), 1.0) + eps
# ret : (B, C=num_classes, H, W)
ret = torch.split(one_hot, [num_classes, ignore_index+1-num_classes], dim=1)[0]
return ret
# https://github.com/zhezh/focalloss/blob/master/focalloss.py
def focal_loss(input, target, alpha, gamma, reduction, eps, ignore_index):
r"""Criterion that computes Focal loss.
According to :cite:`lin2018focal`, the Focal loss is computed as follows:
.. math::
\text{FL}(p_t) = -\alpha_t (1 - p_t)^{\gamma} \, \text{log}(p_t)
Where:
- :math:`p_t` is the model's estimated probability for each class.
Args:
input: logits tensor with shape :math:`(N, C, *)` where C = number of classes.
target: labels tensor with shape :math:`(N, *)` where each value is :math:`0 ≤ targets[i] ≤ C−1`.
alpha: Weighting factor :math:`\alpha \in [0, 1]`.
gamma: Focusing parameter :math:`\gamma >= 0`.
reduction: Specifies the reduction to apply to the
output: ``'none'`` | ``'mean'`` | ``'sum'``. ``'none'``: no reduction
will be applied, ``'mean'``: the sum of the output will be divided by
the number of elements in the output, ``'sum'``: the output will be
summed.
eps: Scalar to enforce numerical stabiliy.
Return:
the computed loss.
Example:
>>> N = 5 # num_classes
>>> input = torch.randn(1, N, 3, 5, requires_grad=True)
>>> target = torch.empty(1, 3, 5, dtype=torch.long).random_(N)
>>> output = focal_loss(input, target, alpha=0.5, gamma=2.0, reduction='mean')
>>> output.backward()
"""
if not isinstance(input, torch.Tensor):
raise TypeError(f"Input type is not a torch.Tensor. Got {type(input)}")
if not len(input.shape) >= 2:
raise ValueError(f"Invalid input shape, we expect BxCx*. Got: {input.shape}")
if input.size(0) != target.size(0):
raise ValueError(f'Expected input batch_size ({input.size(0)}) to match target batch_size ({target.size(0)}).')
# input : (B, C, H, W)
n = input.size(0) # B
# out_sie : (B, H, W)
out_size = (n,) + input.size()[2:]
# input : (B, C, H, W)
# target : (B, H, W)
if target.size()[1:] != input.size()[2:]:
raise ValueError(f'Expected target size {out_size}, got {target.size()}')
if not input.device == target.device:
raise ValueError(f"input and target must be in the same device. Got: {input.device} and {target.device}")
if isinstance(alpha, float):
pass
elif isinstance(alpha, np.ndarray):
alpha = torch.from_numpy(alpha)
# alpha : (B, C, H, W)
alpha = alpha.view(-1, len(alpha), 1, 1).expand_as(input)
elif isinstance(alpha, torch.Tensor):
# alpha : (B, C, H, W)
alpha = alpha.view(-1, len(alpha), 1, 1).expand_as(input)
# compute softmax over the classes axis
# input_soft : (B, C, H, W)
input_soft = F.softmax(input, dim=1) + eps
# create the labels one hot tensor
# target_one_hot : (B, C, H, W)
target_one_hot = label_to_one_hot_label(target.long(), num_classes=input.shape[1], device=input.device, dtype=input.dtype, ignore_index=ignore_index)
# compute the actual focal loss
weight = torch.pow(1.0 - input_soft, gamma)
#print(alpha)
# alpha, weight, input_soft : (B, C, H, W)
# focal : (B, C, H, W)
focal = -alpha * weight * torch.log(input_soft)
# loss_tmp : (B, H, W)
loss_tmp = torch.sum(target_one_hot * focal, dim=1)
if reduction == 'none':
# loss : (B, H, W)
loss = loss_tmp
elif reduction == 'mean':
# loss : scalar
loss = torch.mean(loss_tmp)
elif reduction == 'sum':
# loss : scalar
loss = torch.sum(loss_tmp)
else:
raise NotImplementedError(f"Invalid reduction mode: {reduction}")
return loss
class FocalLoss(nn.Module):
r"""Criterion that computes Focal loss.
According to :cite:`lin2018focal`, the Focal loss is computed as follows:
.. math:
FL(p_t) = -alpha_t(1 - p_t)^{gamma}, log(p_t)
Where:
- :math:`p_t` is the model's estimated probability for each class.
Args:
alpha: Weighting factor :math:`\alpha \in [0, 1]`.
gamma: Focusing parameter :math:`\gamma >= 0`.
reduction: Specifies the reduction to apply to the
output: ``'none'`` | ``'mean'`` | ``'sum'``. ``'none'``: no reduction
will be applied, ``'mean'``: the sum of the output will be divided by
the number of elements in the output, ``'sum'``: the output will be
summed.
eps: Scalar to enforce numerical stabiliy.
Shape:
- Input: :math:`(N, C, *)` where C = number of classes.
- Target: :math:`(N, *)` where each value is
:math:`0 ≤ targets[i] ≤ C−1`.
Example:
>>> N = 5 # num_classes
>>> kwargs = {"alpha": 0.5, "gamma": 2.0, "reduction": 'mean'}
>>> criterion = FocalLoss(**kwargs)
>>> input = torch.randn(1, N, 3, 5, requires_grad=True)
>>> target = torch.empty(1, 3, 5, dtype=torch.long).random_(N)
>>> output = criterion(input, target)
>>> output.backward()
"""
def __init__(self, alpha, gamma = 2.0, reduction = 'mean', eps = 1e-8, ignore_index=30):
super().__init__()
self.alpha = alpha
self.gamma = gamma
self.reduction = reduction
self.eps = eps
self.ignore_index = ignore_index
def forward(self, input, target):
return focal_loss(input, target, self.alpha, self.gamma, self.reduction, self.eps, self.ignore_index)
class CBFocalLoss(nn.Module):
def __init__(self, beta=0.9999, gamma = 2.0, reduction = 'mean', eps = 1e-8, ignore_index=30, samples_per_cls=[260603092, 684334, 343438, 1331800, 242235, 205003, 538888, 195197, 121579, 1652270, 142654, 3507510], device='cuda:0'):
super().__init__()
self.gamma = gamma
self.reduction = reduction
self.eps = eps
self.ignore_index = ignore_index
self.beta = beta
self.samples_per_cls = np.array(samples_per_cls)
self.device = device
def forward(self, input, target):
effective_num = 1.0 - np.power(self.beta, self.samples_per_cls)
weights = (1.0 - self.beta) / np.array(effective_num)
weights = weights / np.sum(weights) * len(self.samples_per_cls)
weights = torch.Tensor(weights).to(self.device)
return focal_loss(input, target, weights, self.gamma, self.reduction, self.eps, self.ignore_index)