-
Notifications
You must be signed in to change notification settings - Fork 86
/
losses_and_metrics_for_mesh.py
194 lines (160 loc) · 6.02 KB
/
losses_and_metrics_for_mesh.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
import torch
import numpy as np
def weighting_DSC(y_pred, y_true, class_weights, smooth = 1.0):
'''
inputs:
y_pred [n_classes, x, y, z] probability
y_true [n_classes, x, y, z] one-hot code
class_weights
smooth = 1.0
'''
smooth = 1.
mdsc = 0.0
n_classes = y_pred.shape[-1]
# convert probability to one-hot code
max_idx = torch.argmax(y_pred, dim=-1, keepdim=True)
one_hot = torch.zeros_like(y_pred)
one_hot.scatter_(-1, max_idx, 1)
for c in range(0, n_classes):
pred_flat = one_hot[:, :, c].reshape(-1)
true_flat = y_true[:, :, c].reshape(-1)
intersection = (pred_flat * true_flat).sum()
w = class_weights[c]/class_weights.sum()
mdsc += w*((2. * intersection + smooth) / (pred_flat.sum() + true_flat.sum() + smooth))
return mdsc
def weighting_SEN(y_pred, y_true, class_weights, smooth = 1.0):
'''
inputs:
y_pred [n_classes, x, y, z] probability
y_true [n_classes, x, y, z] one-hot code
class_weights
smooth = 1.0
'''
smooth = 1.
msen = 0.0
n_classes = y_pred.shape[-1]
# convert probability to one-hot code
max_idx = torch.argmax(y_pred, dim=-1, keepdim=True)
one_hot = torch.zeros_like(y_pred)
one_hot.scatter_(-1, max_idx, 1)
for c in range(0, n_classes):
pred_flat = one_hot[:, :, c].reshape(-1)
true_flat = y_true[:, :, c].reshape(-1)
intersection = (pred_flat * true_flat).sum()
w = class_weights[c]/class_weights.sum()
msen += w*((intersection + smooth) / (true_flat.sum() + smooth))
return msen
def weighting_PPV(y_pred, y_true, class_weights, smooth = 1.0):
'''
inputs:
y_pred [n_classes, x, y, z] probability
y_true [n_classes, x, y, z] one-hot code
class_weights
smooth = 1.0
'''
smooth = 1.
mppv = 0.0
n_classes = y_pred.shape[-1]
# convert probability to one-hot code
max_idx = torch.argmax(y_pred, dim=-1, keepdim=True)
one_hot = torch.zeros_like(y_pred)
one_hot.scatter_(-1, max_idx, 1)
for c in range(0, n_classes):
pred_flat = one_hot[:, :, c].reshape(-1)
true_flat = y_true[:, :, c].reshape(-1)
intersection = (pred_flat * true_flat).sum()
w = class_weights[c]/class_weights.sum()
mppv += w*((intersection + smooth) / (pred_flat.sum() + smooth))
return mppv
def Generalized_Dice_Loss(y_pred, y_true, class_weights, smooth = 1.0):
'''
inputs:
y_pred [n_classes, x, y, z] probability
y_true [n_classes, x, y, z] one-hot code
class_weights
smooth = 1.0
'''
smooth = 1.
loss = 0.
n_classes = y_pred.shape[-1]
for c in range(0, n_classes):
pred_flat = y_pred[:, :, c].reshape(-1)
true_flat = y_true[:, :, c].reshape(-1)
intersection = (pred_flat * true_flat).sum()
# with weight
w = class_weights[c]/class_weights.sum()
loss += w*(1 - ((2. * intersection + smooth) /
(pred_flat.sum() + true_flat.sum() + smooth)))
return loss
def DSC(y_pred, y_true, ignore_background=True, smooth = 1.0):
'''
inputs:
y_pred [npts, n_classes] one-hot code
y_true [npts, n_classes] one-hot code
'''
smooth = 1.
n_classes = y_pred.shape[-1]
dsc = []
if ignore_background:
for c in range(1, n_classes): #pass 0 because 0 is background
pred_flat = y_pred[:, c].reshape(-1)
true_flat = y_true[:, c].reshape(-1)
intersection = (pred_flat * true_flat).sum()
dsc.append(((2. * intersection + smooth) / (pred_flat.sum() + true_flat.sum() + smooth)))
dsc = np.asarray(dsc)
else:
for c in range(0, n_classes):
pred_flat = y_pred[:, c].reshape(-1)
true_flat = y_true[:, c].reshape(-1)
intersection = (pred_flat * true_flat).sum()
dsc.append(((2. * intersection + smooth) / (pred_flat.sum() + true_flat.sum() + smooth)))
dsc = np.asarray(dsc)
return dsc
def SEN(y_pred, y_true, ignore_background=True, smooth = 1.0):
'''
inputs:
y_pred [npts, n_classes] one-hot code
y_true [npts, n_classes] one-hot code
'''
smooth = 1.
n_classes = y_pred.shape[-1]
sen = []
if ignore_background:
for c in range(1, n_classes): #pass 0 because 0 is background
pred_flat = y_pred[:, c].reshape(-1)
true_flat = y_true[:, c].reshape(-1)
intersection = (pred_flat * true_flat).sum()
sen.append(((intersection + smooth) / (true_flat.sum() + smooth)))
sen = np.asarray(sen)
else:
for c in range(0, n_classes):
pred_flat = y_pred[:, c].reshape(-1)
true_flat = y_true[:, c].reshape(-1)
intersection = (pred_flat * true_flat).sum()
sen.append(((intersection + smooth) / (true_flat.sum() + smooth)))
sen = np.asarray(sen)
return sen
def PPV(y_pred, y_true, ignore_background=True, smooth = 1.0):
'''
inputs:
y_pred [npts, n_classes] one-hot code
y_true [npts, n_classes] one-hot code
'''
smooth = 1.
n_classes = y_pred.shape[-1]
ppv = []
if ignore_background:
for c in range(1, n_classes): #pass 0 because 0 is background
pred_flat = y_pred[:, c].reshape(-1)
true_flat = y_true[:, c].reshape(-1)
intersection = (pred_flat * true_flat).sum()
ppv.append(((intersection + smooth) / (pred_flat.sum() + smooth)))
ppv = np.asarray(ppv)
else:
for c in range(0, n_classes):
pred_flat = y_pred[:, c].reshape(-1)
true_flat = y_true[:, c].reshape(-1)
intersection = (pred_flat * true_flat).sum()
ppv.append(((intersection + smooth) / (pred_flat.sum() + smooth)))
ppv = np.asarray(ppv)
return ppv