This repository has been archived by the owner on Jan 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
criterions.py
133 lines (87 loc) · 2.63 KB
/
criterions.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
#!/usr/bin/env python
"""
PyTorch loss functions and metrics
"""
###########
# Imports #
###########
import torch
import torch.nn as nn
from scipy.ndimage.morphology import distance_transform_edt as dt
###########
# Classes #
###########
class MultiTaskLoss(nn.Module):
"""
Distance loss and Dice loss sum
References
----------
Multi-Task Learning for Segmentation of Building Footprints with Deep Neural Networks
(Bischke et al., 2019)
https://arxiv.org/abs/1709.05932
"""
def __init__(self, smooth=1., R=5):
super().__init__()
self.dice = DiceLoss(smooth=smooth)
self.cross = nn.CrossEntropyLoss()
self.R = R
def forward(self, outputs, targets):
dists = self.transform(targets)
return self.dice(outputs[0], targets) + self.cross(outputs[1], dists)
def transform(self, targets):
'''Transforms targets into distances.'''
dists = targets.cpu().squeeze(dim=1).numpy()
for i in range(len(dists)):
dists[i, ...] = dt(dists[i, ...]) - dt(1 - dists[i, ...])
return torch.clamp(
torch.tensor(dists, dtype=int, device=targets.device),
min=-self.R,
max=self.R
) + self.R
class DiceLoss(nn.Module):
'''Dice Loss (F-score, ...)'''
def __init__(self, smooth=1.):
super().__init__()
self.smooth = smooth
def forward(self, outputs, targets):
inter = (outputs * targets).sum()
dice = (2. * inter + self.smooth) / (outputs.sum() + targets.sum() + self.smooth)
return 1. - dice
class IOULoss(nn.Module):
'''Intersection Over Union Loss'''
def __init__(self, smooth=1.):
super().__init__()
self.smooth = smooth
def forward(self, outputs, targets):
inter = (outputs * targets).sum()
union = outputs.sum() + targets.sum() - inter
iou = (inter + self.smooth) / (union + self.smooth)
return 1. - iou
class TP(nn.Module):
'''True Positive'''
def __init__(self, threshold=0.5):
super().__init__()
self.threshold = threshold
def forward(self, outputs, targets):
return ((outputs > self.threshold) * targets).sum()
class TN(nn.Module):
'''True Negative'''
def __init__(self, threshold=0.5):
super().__init__()
self.threshold = threshold
def forward(self, outputs, targets):
return ((outputs < self.threshold) * (1 - targets)).sum()
class FP(nn.Module):
'''False Positive'''
def __init__(self, threshold=0.5):
super().__init__()
self.threshold = threshold
def forward(self, outputs, targets):
return ((outputs > self.threshold) * (1 - targets)).sum()
class FN(nn.Module):
'''False Negative'''
def __init__(self, threshold=0.5):
super().__init__()
self.threshold = threshold
def forward(self, outputs, targets):
return ((outputs < self.threshold) * targets).sum()