-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathseg_losses.py
165 lines (120 loc) · 4.96 KB
/
seg_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
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
import keras.backend as K
from keras.losses import binary_crossentropy
from keras.losses import categorical_crossentropy
from keras.utils.generic_utils import get_custom_objects
from seg_metrics import iou_score, f_score
SMOOTH = 1.
__all__ = [
'jaccard_loss', 'bce_jaccard_loss', 'cce_jaccard_loss',
'dice_loss', 'bce_dice_loss', 'cce_dice_loss',
]
# ============================== Jaccard Losses ==============================
def jaccard_loss(gt, pr):
r"""Jaccard loss function for imbalanced datasets:
.. math:: L(A, B) = 1 - \frac{A \cap B}{A \cup B}
Args:
gt: ground truth 4D keras tensor (B, H, W, C)
pr: prediction 4D keras tensor (B, H, W, C)
class_weights: 1. or list of class weights, len(weights) = C
smooth: value to avoid division by zero
per_image: if ``True``, metric is calculated as mean over images in batch (B),
else over whole batch
Returns:
Jaccard loss in range [0, 1]
"""
return 1 - iou_score(gt, pr)
def bce_jaccard_loss(gt, pr):
r"""Sum of binary crossentropy and jaccard losses:
.. math:: L(A, B) = bce_weight * binary_crossentropy(A, B) + jaccard_loss(A, B)
Args:
gt: ground truth 4D keras tensor (B, H, W, C)
pr: prediction 4D keras tensor (B, H, W, C)
class_weights: 1. or list of class weights for jaccard loss, len(weights) = C
smooth: value to avoid division by zero
per_image: if ``True``, jaccard loss is calculated as mean over images in batch (B),
else over whole batch (only for jaccard loss)
Returns:
loss
"""
bce = K.mean(binary_crossentropy(gt, pr))
loss = bce_weight * bce + jaccard_loss(gt, pr)
return loss
def cce_jaccard_loss(gt, pr):
r"""Sum of categorical crossentropy and jaccard losses:
.. math:: L(A, B) = cce_weight * categorical_crossentropy(A, B) + jaccard_loss(A, B)
Args:
gt: ground truth 4D keras tensor (B, H, W, C)
pr: prediction 4D keras tensor (B, H, W, C)
class_weights: 1. or list of class weights for jaccard loss, len(weights) = C
smooth: value to avoid division by zero
per_image: if ``True``, jaccard loss is calculated as mean over images in batch (B),
else over whole batch
Returns:
loss
"""
cce = categorical_crossentropy(gt, pr) * class_weights
cce = K.mean(cce)
return 1 * cce + jaccard_loss(gt, pr)
# Update custom objects
get_custom_objects().update({
'jaccard_loss': jaccard_loss,
'bce_jaccard_loss': bce_jaccard_loss,
'cce_jaccard_loss': cce_jaccard_loss,
})
# ============================== Dice Losses ================================
def dice_loss(gt, pr):
r"""Dice loss function for imbalanced datasets:
.. math:: L(precision, recall) = 1 - (1 + \beta^2) \frac{precision \cdot recall}
{\beta^2 \cdot precision + recall}
Args:
gt: ground truth 4D keras tensor (B, H, W, C)
pr: prediction 4D keras tensor (B, H, W, C)
class_weights: 1. or list of class weights, len(weights) = C
smooth: value to avoid division by zero
per_image: if ``True``, metric is calculated as mean over images in batch (B),
else over whole batch
beta: coefficient for precision recall balance
Returns:
Dice loss in range [0, 1]
"""
return 1 - f_score(gt, pr)
def bce_dice_loss(gt, pr):
r"""Sum of binary crossentropy and dice losses:
.. math:: L(A, B) = bce_weight * binary_crossentropy(A, B) + dice_loss(A, B)
Args:
gt: ground truth 4D keras tensor (B, H, W, C)
pr: prediction 4D keras tensor (B, H, W, C)
class_weights: 1. or list of class weights for dice loss, len(weights) = C
smooth: value to avoid division by zero
per_image: if ``True``, dice loss is calculated as mean over images in batch (B),
else over whole batch
beta: coefficient for precision recall balance
Returns:
loss
"""
bce = K.mean(binary_crossentropy(gt, pr))
loss = bce_weight * bce + dice_loss(gt, pr)
return loss
def cce_dice_loss(gt, pr):
r"""Sum of categorical crossentropy and dice losses:
.. math:: L(A, B) = cce_weight * categorical_crossentropy(A, B) + dice_loss(A, B)
Args:
gt: ground truth 4D keras tensor (B, H, W, C)
pr: prediction 4D keras tensor (B, H, W, C)
class_weights: 1. or list of class weights for dice loss, len(weights) = C
smooth: value to avoid division by zero
per_image: if ``True``, dice loss is calculated as mean over images in batch (B),
else over whole batch
beta: coefficient for precision recall balance
Returns:
loss
"""
cce = categorical_crossentropy(gt, pr) * class_weights
cce = K.mean(cce)
return 1 * cce + dice_loss(gt, pr)
# Update custom objects
get_custom_objects().update({
'dice_loss': dice_loss,
'bce_dice_loss': bce_dice_loss,
'cce_dice_loss': cce_dice_loss,
})