-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathloss.py
More file actions
333 lines (272 loc) · 12.5 KB
/
loss.py
File metadata and controls
333 lines (272 loc) · 12.5 KB
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# Copyright (c) 2025 Robert Bosch GmbH
# SPDX-License-Identifier: AGPL-3.0
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from math import exp
from typing import Optional, List
import pdb
class FocalLoss(nn.Module):
"""
copy from: https://github.com/Hsuxu/Loss_ToolBox-PyTorch/blob/master/FocalLoss/FocalLoss.py
This is a implementation of Focal Loss with smooth label cross entropy supported which is proposed in
'Focal Loss for Dense Object Detection. (https://arxiv.org/abs/1708.02002)'
Focal_Loss= -1*alpha*(1-pt)*log(pt)
:param alpha: (tensor) 3D or 4D the scalar factor for this criterion
:param gamma: (float,double) gamma > 0 reduces the relative loss for well-classified examples (p>0.5) putting more
focus on hard misclassified example
:param smooth: (float,double) smooth value when cross entropy
:param balance_index: (int) balance class index, should be specific when alpha is float
:param size_average: (bool, optional) By default, the losses are averaged over each loss element in the batch.
"""
def __init__(self, apply_nonlin=None, alpha=None, gamma=2, balance_index=0, smooth=1e-5, size_average=True):
super(FocalLoss, self).__init__()
self.apply_nonlin = apply_nonlin
self.alpha = alpha
self.gamma = gamma
self.balance_index = balance_index
self.smooth = smooth
self.size_average = size_average
if self.smooth is not None:
if self.smooth < 0 or self.smooth > 1.0:
raise ValueError('smooth value should be in [0,1]')
def forward(self, logit, target):
if self.apply_nonlin is not None:
logit = self.apply_nonlin(logit)
num_class = logit.shape[1]
if logit.dim() > 2:
# N,C,d1,d2 -> N,C,m (m=d1*d2*...)
logit = logit.view(logit.size(0), logit.size(1), -1)
logit = logit.permute(0, 2, 1).contiguous()
logit = logit.view(-1, logit.size(-1))
target = torch.squeeze(target, 1) #
target = target.view(-1, 1)
alpha = self.alpha
if alpha is None:
alpha = torch.ones(num_class, 1)
elif isinstance(alpha, (list, np.ndarray)):
assert len(alpha) == num_class
alpha = torch.FloatTensor(alpha).view(num_class, 1)
alpha = alpha / alpha.sum()
elif isinstance(alpha, float):
alpha = torch.ones(num_class, 1)
alpha = alpha * (1 - self.alpha)
alpha[self.balance_index] = self.alpha
else:
raise TypeError('Not support alpha type')
if alpha.device != logit.device:
alpha = alpha.to(logit.device)
idx = target.long()
one_hot_key = torch.zeros(target.size(0), num_class).to(logit.device)
one_hot_key = one_hot_key.scatter_(1, idx, 1)
if one_hot_key.device != logit.device:
one_hot_key = one_hot_key.to(logit.device)
if self.smooth:
one_hot_key = torch.clamp(
one_hot_key, self.smooth / (num_class - 1), 1.0 - self.smooth)
pt = (one_hot_key * logit).sum(1) + self.smooth
logpt = pt.log()
gamma = self.gamma
alpha = alpha[idx]
alpha = torch.squeeze(alpha)
loss = -1 * alpha * torch.pow((1 - pt), gamma) * logpt
if self.size_average:
loss = loss.mean()
return loss
class BinaryDiceLoss(nn.Module):
def __init__(self):
super(BinaryDiceLoss, self).__init__()
def forward(self, input, targets):
# 获取每个批次的大小 N
N = targets.size()[0]
# 平滑变量
smooth = 1
# 将宽高 reshape 到同一纬度
input_flat = input.view(N, -1)
targets_flat = targets.view(N, -1) * 5.0
# 计算交集
intersection = input_flat * targets_flat
N_dice_eff = (2 * intersection.sum(1) + smooth) / (input_flat.sum(1) + targets_flat.sum(1) + smooth)
# 计算一个批次中平均每张图的损失
loss = 1 - N_dice_eff.sum() / N
return loss
# class DiceLoss(nn.Module):
# # https://github.com/rogertrullo/pytorch/blob/rogertrullo-dice_loss/torch/nn/functional.py#L708
# def __init__(self):
# super(DiceLoss, self).__init__()
# def forward(self, input, target):
# """
# input is a torch variable of size BatchxnclassesxHxW representing log probabilities for each class
# target is a 1-hot representation of the groundtruth, shoud have same size as the input
# """
# pdb.set_trace()
# assert input.size() == target.size(), "Input sizes must be equal."
# assert input.dim() == 4, "Input must be a 4D Tensor."
# uniques=np.unique(target.numpy())
# assert set(list(uniques))<=set([0,1]), "target must only contain zeros and ones"
# probs=F.softmax(input)
# num=probs*target#b,c,h,w--p*g
# num=torch.sum(num,dim=3)#b,c,h
# num=torch.sum(num,dim=2)
# den1=probs*probs#--p^2
# den1=torch.sum(den1,dim=3)#b,c,h
# den1=torch.sum(den1,dim=2)
# den2=target*target#--g^2
# den2=torch.sum(den2,dim=3)#b,c,h
# den2=torch.sum(den2,dim=2)#b,c
# dice=2*(num/(den1+den2))
# dice_eso=dice[:,1:]#we ignore bg dice val, and take the fg
# dice_total=-1*torch.sum(dice_eso)/dice_eso.size(0)#divide by batch_sz
# return dice_total
class diceloss_m(torch.nn.Module):
def init(self):
super(diceloss_m, self).init()
def forward(self,pred, target):
smooth = 1.
iflat = pred.contiguous().view(-1)
tflat = target.contiguous().view(-1)
intersection = (iflat * tflat).sum()
A_sum = torch.sum(iflat * iflat)
B_sum = torch.sum(tflat * tflat)
return 1 - ((2. * intersection + smooth) / (A_sum + B_sum + smooth) )
# class DiceLoss(nn.Module):
# """Dice Loss PyTorch
# Created by: Zhang Shuai
# Email: shuaizzz666@gmail.com
# dice_loss = 1 - 2*p*t / (p^2 + t^2). p and t represent predict and target.
# Args:
# weight: An array of shape [C,]
# predict: A float32 tensor of shape [N, C, *], for Semantic segmentation task is [N, C, H, W]
# target: A int64 tensor of shape [N, *], for Semantic segmentation task is [N, H, W]
# Return:
# diceloss
# """
# def __init__(self, weight=None):
# super(DiceLoss, self).__init__()
# if weight is not None:
# weight = torch.Tensor(weight)
# self.weight = weight / torch.sum(weight) # Normalized weight
# self.smooth = 1e-5
# def forward(self, predict, target):
# N, C = predict.size()[:2]
# predict = predict.view(N, C, -1) # (N, C, *)
# target = target.view(N, 1, -1) # (N, 1, *)
# predict = F.softmax(predict, dim=1) # (N, C, *) ==> (N, C, *)
# ## convert target(N, 1, *) into one hot vector (N, C, *)
# target_onehot = torch.zeros(predict.size()).cuda() # (N, 1, *) ==> (N, C, *)
# target_onehot.scatter_(1, target, 1) # (N, C, *)
# intersection = torch.sum(predict * target_onehot, dim=2) # (N, C)
# union = torch.sum(predict.pow(2), dim=2) + torch.sum(target_onehot, dim=2) # (N, C)
# ## p^2 + t^2 >= 2*p*t, target_onehot^2 == target_onehot
# dice_coef = (2 * intersection + self.smooth) / (union + self.smooth) # (N, C)
# if hasattr(self, 'weight'):
# if self.weight.type() != predict.type():
# self.weight = self.weight.type_as(predict)
# dice_coef = dice_coef * self.weight * C # (N, C)
# dice_loss = 1 - torch.mean(dice_coef) # 1
# return dice_loss
class DiceLoss(nn.Module):
def __init__(
self,
classes: Optional[List[int]] = None,
log_loss: bool = False,
from_logits: bool = True,
smooth: float = 0.0,
ignore_index: Optional[int] = None,
eps: float = 1e-7,
alpha: float = None,
balance_index: int = 0
):
"""Dice loss for image segmentation task.
It supports binary, multiclass and multilabel cases
Args:
mode: Loss mode 'binary', 'multiclass' or 'multilabel'
classes: List of classes that contribute in loss computation. By default, all channels are included.
log_loss: If True, loss computed as `- log(dice_coeff)`, otherwise `1 - dice_coeff`
from_logits: If True, assumes input is raw logits
smooth: Smoothness constant for dice coefficient (a)
ignore_index: Label that indicates ignored pixels (does not contribute to loss)
eps: A small epsilon for numerical stability to avoid zero division error
(denominator will be always greater or equal to eps)
Shape
- **y_pred** - torch.Tensor of shape (N, C, H, W)
- **y_true** - torch.Tensor of shape (N, H, W) or (N, C, H, W)
Reference
https://github.com/BloodAxe/pytorch-toolbelt
"""
super(DiceLoss, self).__init__()
self.classes = classes
self.from_logits = from_logits
self.smooth = smooth
self.eps = eps
self.log_loss = log_loss
self.ignore_index = ignore_index
self.alpha = alpha
self.balance_index = balance_index
def forward(self, y_pred: torch.Tensor, y_true: torch.Tensor) -> torch.Tensor:
assert y_true.size(0) == y_pred.size(0)
if self.from_logits:
# Apply activations to get [0..1] class probabilities
# Using Log-Exp as this gives more numerically stable result and does not cause vanishing gradient on
# extreme values 0 and 1
y_pred = y_pred.log_softmax(dim=1).exp()
bs = y_true.size(0)
num_classes = y_pred.size(1)
dims = (0, 2)
y_true = y_true.view(bs, -1)
y_pred = y_pred.view(bs, num_classes, -1)
if self.ignore_index is not None:
mask = y_true != self.ignore_index
y_pred = y_pred * mask.unsqueeze(1)
y_true = F.one_hot(
(y_true * mask).to(torch.long), num_classes
) # N,H*W -> N,H*W, C
y_true = y_true.permute(0, 2, 1) * mask.unsqueeze(1) # N, C, H*W
else:
y_true = F.one_hot(y_true, num_classes) # N,H*W -> N,H*W, C
y_true = y_true.permute(0, 2, 1) # N, C, H*W
scores = self.compute_score(
y_pred, y_true.type_as(y_pred), smooth=self.smooth, eps=self.eps, dims=dims
)
if self.log_loss:
loss = -torch.log(scores.clamp_min(self.eps))
else:
loss = 1.0 - scores
# Dice loss is undefined for non-empty classes
# So we zero contribution of channel that does not have true pixels
# NOTE: A better workaround would be to use loss term `mean(y_pred)`
# for this case, however it will be a modified jaccard loss
mask = y_true.sum(dims) > 0
loss *= mask.to(loss.dtype)
if self.classes is not None:
loss = loss[self.classes]
return self.aggregate_loss(loss, num_classes)
def aggregate_loss(self, loss, num_classes):
if self.alpha:
alpha = torch.ones(num_classes).to(loss)
alpha =alpha * (1 - self.alpha)
alpha[self.balance_index] = self.alpha
return torch.sum(alpha * loss) / alpha.sum()
else:
return loss.mean()
def compute_score(
self, output, target, smooth=0.0, eps=1e-7, dims=None
) -> torch.Tensor:
return soft_dice_score(output, target, smooth, eps, dims)
def soft_dice_score(
output: torch.Tensor,
target: torch.Tensor,
smooth: float = 0.0,
eps: float = 1e-7,
dims=None,
) -> torch.Tensor:
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