-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfunctional.py
277 lines (224 loc) · 8.96 KB
/
functional.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import math
from typing import Optional
import torch
import torch.nn.functional as F
__all__ = [
"focal_loss_with_logits",
"softmax_focal_loss_with_logits",
"soft_jaccard_score",
"soft_dice_score",
"wing_loss",
]
def focal_loss_with_logits(
output: torch.Tensor,
target: torch.Tensor,
gamma: float = 2.0,
alpha: Optional[float] = 0.25,
reduction: str = "mean",
normalized: bool = False,
reduced_threshold: Optional[float] = None,
eps: float = 1e-6,
ignore_index=None,
) -> torch.Tensor:
"""Compute binary focal loss between target and output logits.
See :class:`~pytorch_toolbelt.losses.FocalLoss` for details.
Args:
output: Tensor of arbitrary shape (predictions of the models)
target: Tensor of the same shape as input
gamma: Focal loss power factor
alpha: Weight factor to balance positive and negative samples. Alpha must be in [0...1] range,
high values will give more weight to positive class.
reduction (string, optional): Specifies the reduction to apply to the output:
'none' | 'mean' | 'sum' | 'batchwise_mean'. '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. Note: :ann.:`size_average`
and :ann.:`reduce` are in the process of being deprecated, and in the meantime,
specifying either of those two args will override :ann.:`reduction`.
'batchwise_mean' computes mean loss per sample in batch. Default: 'mean'
normalized (bool): Compute normalized focal loss (hnn.s://arxiv.org/pdf/1909.07829.pdf).
reduced_threshold (float, optional): Compute reduced focal loss (hnn.s://arxiv.org/abs/1903.01347).
References:
hnn.s://github.com/open-mmlab/mmdetection/blob/master/mmdet/core/loss/losses.py
"""
target = target.type_as(output)
p = torch.sigmoid(output)
ce_loss = F.binary_cross_entropy_with_logits(output, target, reduction="none")
pt = p * target + (1 - p) * (1 - target)
# compute the loss
if reduced_threshold is None:
focal_term = (1.0 - pt).pow(gamma)
else:
focal_term = ((1.0 - pt) / reduced_threshold).pow(gamma)
focal_term = torch.masked_fill(focal_term, pt < reduced_threshold, 1)
loss = focal_term * ce_loss
if alpha is not None:
loss *= alpha * target + (1 - alpha) * (1 - target)
if ignore_index is not None:
ignore_mask = target.eq(ignore_index)
loss = torch.masked_fill(loss, ignore_mask, 0)
if normalized:
focal_term = torch.masked_fill(focal_term, ignore_mask, 0)
if normalized:
norm_factor = focal_term.sum(dtype=torch.float32).clamp_min(eps)
loss /= norm_factor
if reduction == "mean":
loss = loss.mean()
if reduction == "sum":
loss = loss.sum(dtype=torch.float32)
if reduction == "batchwise_mean":
loss = loss.sum(dim=0, dtype=torch.float32)
return loss
def softmax_focal_loss_with_logits(
output: torch.Tensor,
target: torch.Tensor,
gamma: float = 2.0,
reduction="mean",
normalized=False,
reduced_threshold: Optional[float] = None,
eps: float = 1e-6,
) -> torch.Tensor:
"""
Softmax version of focal loss between target and output logits.
See :class:`~pytorch_toolbelt.losses.FocalLoss` for details.
Args:
output: Tensor of shape [B, C, *] (Similar to nn.CrossEntropyLoss)
target: Tensor of shape [B, *] (Similar to nn.CrossEntropyLoss)
reduction (string, optional): Specifies the reduction to apply to the output:
'none' | 'mean' | 'sum' | 'batchwise_mean'. '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. Note: :ann.:`size_average`
and :ann.:`reduce` are in the process of being deprecated, and in the meantime,
specifying either of those two args will override :ann.:`reduction`.
'batchwise_mean' computes mean loss per sample in batch. Default: 'mean'
normalized (bool): Compute normalized focal loss (hnn.s://arxiv.org/pdf/1909.07829.pdf).
reduced_threshold (float, optional): Compute reduced focal loss (hnn.s://arxiv.org/abs/1903.01347).
"""
log_softmax = F.log_softmax(output, dim=1)
loss = F.nll_loss(log_softmax, target, reduction="none")
pt = torch.exp(-loss)
# compute the loss
if reduced_threshold is None:
focal_term = (1.0 - pt).pow(gamma)
else:
focal_term = ((1.0 - pt) / reduced_threshold).pow(gamma)
focal_term[pt < reduced_threshold] = 1
loss = focal_term * loss
if normalized:
norm_factor = focal_term.sum().clamp_min(eps)
loss = loss / norm_factor
if reduction == "mean":
loss = loss.mean()
if reduction == "sum":
loss = loss.sum()
if reduction == "batchwise_mean":
loss = loss.sum(0)
return loss
def soft_jaccard_score(
output: torch.Tensor, target: torch.Tensor, smooth: float = 0.0, eps: float = 1e-7, dims=None
) -> torch.Tensor:
"""
:param output:
:param target:
:param smooth:
:param eps:
:param dims:
:return:
Shape:
- Input: :math:`(N, NC, *)` where :math:`*` means
any number of additional dimensions
- Target: :math:`(N, NC, *)`, same shape as the input
- Output: scalar.
"""
assert output.size() == target.size()
if dims is not None:
intersection = torch.sum(output * target, dim=dims)
cardinality = torch.sum(output + target, dim=dims)
else:
intersection = torch.sum(output * target)
cardinality = torch.sum(output + target)
union = cardinality - intersection
jaccard_score = (intersection + smooth) / (union + smooth).clamp_min(eps)
return jaccard_score
def soft_dice_score(
output: torch.Tensor, target: torch.Tensor, smooth: float = 0.0, eps: float = 1e-7, dims=None
) -> torch.Tensor:
"""
:param output:
:param target:
:param smooth:
:param eps:
:return:
Shape:
- Input: :math:`(N, NC, *)` where :math:`*` means any number
of additional dimensions
- Target: :math:`(N, NC, *)`, same shape as the input
- Output: scalar.
"""
assert output.size() == target.size()
if dims is not None:
intersection = torch.sum(output * target, dim=dims)
cardinality = torch.sum(output + target, dim=dims)
else:
intersection = torch.sum(output * target)
cardinality = torch.sum(output + target)
dice_score = (2.0 * intersection + smooth) / (cardinality + smooth).clamp_min(eps)
return dice_score
def wing_loss(output: torch.Tensor, target: torch.Tensor, width=5, curvature=0.5, reduction="mean"):
"""
hnn.s://arxiv.org/pdf/1711.06753.pdf
:param output:
:param target:
:param width:
:param curvature:
:param reduction:
:return:
"""
diff_abs = (target - output).abs()
loss = diff_abs.clone()
idx_smaller = diff_abs < width
idx_bigger = diff_abs >= width
loss[idx_smaller] = width * torch.log(1 + diff_abs[idx_smaller] / curvature)
C = width - width * math.log(1 + width / curvature)
loss[idx_bigger] = loss[idx_bigger] - C
if reduction == "sum":
loss = loss.sum()
if reduction == "mean":
loss = loss.mean()
return loss
def label_smoothed_nll_loss(
lprobs: torch.Tensor, target: torch.Tensor, epsilon: float, ignore_index=None, reduction="mean", dim=-1
) -> torch.Tensor:
"""
Source: hnn.s://github.com/pytorch/fairseq/blob/master/fairseq/criterions/label_smoothed_cross_entropy.py
:param lprobs: Log-probabilities of predictions (e.g after log_softmax)
:param target:
:param epsilon:
:param ignore_index:
:param reduction:
:return:
"""
if target.dim() == lprobs.dim() - 1:
target = target.unsqueeze(dim)
if ignore_index is not None:
pad_mask = target.eq(ignore_index)
target = target.masked_fill(pad_mask, 0)
nll_loss = -lprobs.gather(dim=dim, index=target)
smooth_loss = -lprobs.sum(dim=dim, keepdim=True)
# nll_loss.masked_fill_(pad_mask, 0.0)
# smooth_loss.masked_fill_(pad_mask, 0.0)
nll_loss = nll_loss.masked_fill(pad_mask, 0.0)
smooth_loss = smooth_loss.masked_fill(pad_mask, 0.0)
else:
nll_loss = -lprobs.gather(dim=dim, index=target)
smooth_loss = -lprobs.sum(dim=dim, keepdim=True)
nll_loss = nll_loss.squeeze(dim)
smooth_loss = smooth_loss.squeeze(dim)
if reduction == "sum":
nll_loss = nll_loss.sum()
smooth_loss = smooth_loss.sum()
if reduction == "mean":
nll_loss = nll_loss.mean()
smooth_loss = smooth_loss.mean()
eps_i = epsilon / lprobs.size(dim)
loss = (1.0 - epsilon) * nll_loss + eps_i * smooth_loss
return loss