|
a |
|
b/utils.py |
|
|
1 |
import numpy as np |
|
|
2 |
from medpy.filter.binary import largest_connected_component |
|
|
3 |
from skimage.exposure import rescale_intensity |
|
|
4 |
from skimage.transform import resize |
|
|
5 |
|
|
|
6 |
|
|
|
7 |
def dsc(y_pred, y_true, lcc=True): |
|
|
8 |
if lcc and np.any(y_pred): |
|
|
9 |
y_pred = np.round(y_pred).astype(int) |
|
|
10 |
y_true = np.round(y_true).astype(int) |
|
|
11 |
y_pred = largest_connected_component(y_pred) |
|
|
12 |
return np.sum(y_pred[y_true == 1]) * 2.0 / (np.sum(y_pred) + np.sum(y_true)) |
|
|
13 |
|
|
|
14 |
|
|
|
15 |
def crop_sample(x): |
|
|
16 |
volume, mask = x |
|
|
17 |
volume[volume < np.max(volume) * 0.1] = 0 |
|
|
18 |
z_projection = np.max(np.max(np.max(volume, axis=-1), axis=-1), axis=-1) |
|
|
19 |
z_nonzero = np.nonzero(z_projection) |
|
|
20 |
z_min = np.min(z_nonzero) |
|
|
21 |
z_max = np.max(z_nonzero) + 1 |
|
|
22 |
y_projection = np.max(np.max(np.max(volume, axis=0), axis=-1), axis=-1) |
|
|
23 |
y_nonzero = np.nonzero(y_projection) |
|
|
24 |
y_min = np.min(y_nonzero) |
|
|
25 |
y_max = np.max(y_nonzero) + 1 |
|
|
26 |
x_projection = np.max(np.max(np.max(volume, axis=0), axis=0), axis=-1) |
|
|
27 |
x_nonzero = np.nonzero(x_projection) |
|
|
28 |
x_min = np.min(x_nonzero) |
|
|
29 |
x_max = np.max(x_nonzero) + 1 |
|
|
30 |
return ( |
|
|
31 |
volume[z_min:z_max, y_min:y_max, x_min:x_max], |
|
|
32 |
mask[z_min:z_max, y_min:y_max, x_min:x_max], |
|
|
33 |
) |
|
|
34 |
|
|
|
35 |
|
|
|
36 |
def pad_sample(x): |
|
|
37 |
volume, mask = x |
|
|
38 |
a = volume.shape[1] |
|
|
39 |
b = volume.shape[2] |
|
|
40 |
if a == b: |
|
|
41 |
return volume, mask |
|
|
42 |
diff = (max(a, b) - min(a, b)) / 2.0 |
|
|
43 |
if a > b: |
|
|
44 |
padding = ((0, 0), (0, 0), (int(np.floor(diff)), int(np.ceil(diff)))) |
|
|
45 |
else: |
|
|
46 |
padding = ((0, 0), (int(np.floor(diff)), int(np.ceil(diff))), (0, 0)) |
|
|
47 |
mask = np.pad(mask, padding, mode="constant", constant_values=0) |
|
|
48 |
padding = padding + ((0, 0),) |
|
|
49 |
volume = np.pad(volume, padding, mode="constant", constant_values=0) |
|
|
50 |
return volume, mask |
|
|
51 |
|
|
|
52 |
|
|
|
53 |
def resize_sample(x, size=256): |
|
|
54 |
volume, mask = x |
|
|
55 |
v_shape = volume.shape |
|
|
56 |
out_shape = (v_shape[0], size, size) |
|
|
57 |
mask = resize( |
|
|
58 |
mask, |
|
|
59 |
output_shape=out_shape, |
|
|
60 |
order=0, |
|
|
61 |
mode="constant", |
|
|
62 |
cval=0, |
|
|
63 |
anti_aliasing=False, |
|
|
64 |
) |
|
|
65 |
out_shape = out_shape + (v_shape[3],) |
|
|
66 |
volume = resize( |
|
|
67 |
volume, |
|
|
68 |
output_shape=out_shape, |
|
|
69 |
order=2, |
|
|
70 |
mode="constant", |
|
|
71 |
cval=0, |
|
|
72 |
anti_aliasing=False, |
|
|
73 |
) |
|
|
74 |
return volume, mask |
|
|
75 |
|
|
|
76 |
|
|
|
77 |
def normalize_volume(volume): |
|
|
78 |
p10 = np.percentile(volume, 10) |
|
|
79 |
p99 = np.percentile(volume, 99) |
|
|
80 |
volume = rescale_intensity(volume, in_range=(p10, p99)) |
|
|
81 |
m = np.mean(volume, axis=(0, 1, 2)) |
|
|
82 |
s = np.std(volume, axis=(0, 1, 2)) |
|
|
83 |
volume = (volume - m) / s |
|
|
84 |
return volume |
|
|
85 |
|
|
|
86 |
|
|
|
87 |
def log_images(x, y_true, y_pred, channel=1): |
|
|
88 |
images = [] |
|
|
89 |
x_np = x[:, channel].cpu().numpy() |
|
|
90 |
y_true_np = y_true[:, 0].cpu().numpy() |
|
|
91 |
y_pred_np = y_pred[:, 0].cpu().numpy() |
|
|
92 |
for i in range(x_np.shape[0]): |
|
|
93 |
image = gray2rgb(np.squeeze(x_np[i])) |
|
|
94 |
image = outline(image, y_pred_np[i], color=[255, 0, 0]) |
|
|
95 |
image = outline(image, y_true_np[i], color=[0, 255, 0]) |
|
|
96 |
images.append(image) |
|
|
97 |
return images |
|
|
98 |
|
|
|
99 |
|
|
|
100 |
def gray2rgb(image): |
|
|
101 |
w, h = image.shape |
|
|
102 |
image += np.abs(np.min(image)) |
|
|
103 |
image_max = np.abs(np.max(image)) |
|
|
104 |
if image_max > 0: |
|
|
105 |
image /= image_max |
|
|
106 |
ret = np.empty((w, h, 3), dtype=np.uint8) |
|
|
107 |
ret[:, :, 2] = ret[:, :, 1] = ret[:, :, 0] = image * 255 |
|
|
108 |
return ret |
|
|
109 |
|
|
|
110 |
|
|
|
111 |
def outline(image, mask, color): |
|
|
112 |
mask = np.round(mask) |
|
|
113 |
yy, xx = np.nonzero(mask) |
|
|
114 |
for y, x in zip(yy, xx): |
|
|
115 |
if 0.0 < np.mean(mask[max(0, y - 1) : y + 2, max(0, x - 1) : x + 2]) < 1.0: |
|
|
116 |
image[max(0, y) : y + 1, max(0, x) : x + 1] = color |
|
|
117 |
return image |