-
Notifications
You must be signed in to change notification settings - Fork 10
/
soft_cldice_second_version.py
117 lines (99 loc) · 3.74 KB
/
soft_cldice_second_version.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
import numpy as np
import cv2
import tensorflow as tf
import tensorflow.keras.backend as K
def dice_loss(data_format="channels_first"):
"""dice loss function for tensorflow/keras
calculate dice loss per batch and channel of each sample.
Args:
data_format: either channels_first or channels_last
Returns:
loss_function(y_true, y_pred)
"""
def loss(target, pred):
if data_format == "channels_last":
pred = tf.transpose(pred, (0, 3, 1, 2))
target = tf.transpose(target, (0, 3, 1, 2))
smooth = 1.0
iflat = tf.reshape(
pred, (tf.shape(pred)[0], tf.shape(pred)[1], -1)
) # batch, channel, -1
tflat = tf.reshape(target, (tf.shape(target)[0], tf.shape(target)[1], -1))
intersection = K.sum(iflat * tflat, axis=-1)
return 1 - ((2.0 * intersection + smooth)) / (
K.sum(iflat, axis=-1) + K.sum(tflat, axis=-1) + smooth
)
return loss
def soft_skeletonize(x, thresh_width=10):
"""
Differenciable aproximation of morphological skelitonization operaton
thresh_width - needs to be greater then or equal to the maximum radius for the tube-like structure
"""
minpool = (
lambda y: K.pool2d(
y * -1,
pool_size=(3, 3),
strides=(1, 1),
pool_mode="max",
data_format="channels_first",
padding="same",
)
* -1
)
maxpool = lambda y: K.pool2d(
y,
pool_size=(3, 3),
strides=(1, 1),
pool_mode="max",
data_format="channels_first",
padding="same",
)
for i in range(thresh_width):
min_pool_x = minpool(x)
contour = K.relu(maxpool(min_pool_x) - min_pool_x)
x = K.relu(x - contour)
return x
def norm_intersection(center_line, vessel):
"""
inputs shape (batch, channel, height, width)
intersection formalized by first ares
x - suppose to be centerline of vessel (pred or gt) and y - is vessel (pred or gt)
"""
smooth = 1.0
clf = tf.reshape(
center_line, (tf.shape(center_line)[0], tf.shape(center_line)[1], -1)
)
vf = tf.reshape(vessel, (tf.shape(vessel)[0], tf.shape(vessel)[1], -1))
intersection = K.sum(clf * vf, axis=-1)
return (intersection + smooth) / (K.sum(clf, axis=-1) + smooth)
def soft_cldice_loss_version2(k=10, data_format="channels_first"):
"""clDice loss function for tensorflow/keras
Args:
k: needs to be greater or equal to the maximum radius of the tube structure.
data_format: either channels_first or channels_last
Returns:
loss_function(y_true, y_pred)
"""
def loss(target, pred):
if data_format == "channels_last":
pred = tf.transpose(pred, (0, 3, 1, 2))
target = tf.transpose(target, (0, 3, 1, 2))
cl_pred = soft_skeletonize(pred, thresh_width=k)
target_skeleton = soft_skeletonize(target, thresh_width=k)
iflat = norm_intersection(cl_pred, target)
tflat = norm_intersection(target_skeleton, pred)
intersection = iflat * tflat
return 1 - ((2.0 * intersection) / (iflat + tflat))
return loss
# Or combine dice + cldice similiar to the experiments in the paper
def combined_loss_version2(y_true, y_pred):
alpha = 0.5
data_format="channels_last"
return (alpha * dice_loss(data_format=data_format)(y_true, y_pred) +
(1-alpha) * soft_cldice_loss_version2(k=5, data_format=data_format)(y_true, y_pred))
# use example
# from dice_helpers_tf import dice_loss, soft_cldice_loss
# cldice_loss = soft_cldice_loss(k=5, data_format="channels_last")
# model.compile(loss=cldice_loss, [...])
# or
# model.compile(loss= combined_loss, [...])