-
Notifications
You must be signed in to change notification settings - Fork 14
/
demo3d_signed.py
219 lines (189 loc) · 6.87 KB
/
demo3d_signed.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import math
import os
import time
from functools import wraps
import FastGeodis
import matplotlib.pyplot as plt
import numpy as np
import SimpleITK as sitk
import torch
def demo_geodesic_distance3d(image_path, seed_pos):
SHOW_JOINT_HIST = False
image_folder = os.path.dirname(image_path)
image_sitk = sitk.ReadImage(image_path)
input_image = sitk.GetArrayFromImage(image_sitk)
spacing_raw = image_sitk.GetSpacing()
spacing = [spacing_raw[2], spacing_raw[1], spacing_raw[0]]
input_image = np.asarray(input_image, np.float32)
input_image = input_image[18:38, 63:183, 93:233]
seed_image = np.zeros_like(input_image, np.uint8)
seed_image[seed_pos[0]][seed_pos[1]][seed_pos[2]] = 1
device = "cpu"
input_image_pt = torch.from_numpy(input_image).unsqueeze_(0).unsqueeze_(0)
seed_image_pt = (
torch.from_numpy(1 - seed_image.astype(np.float32)).unsqueeze_(0).unsqueeze_(0)
)
input_image_pt = input_image_pt.to(device)
seed_image_pt = seed_image_pt.to(device)
tic = time.time()
fastmarch_output = np.squeeze(
FastGeodis.signed_geodesic3d_fastmarch(
input_image_pt, seed_image_pt, spacing, 1.0
)
.cpu()
.numpy()
)
fastmarch_time = time.time() - tic
tic = time.time()
toivanenraster_output = np.squeeze(
FastGeodis.signed_generalised_geodesic3d_toivanen(
input_image_pt, seed_image_pt, spacing, 1e10, 1.0, 4
)
.detach()
.cpu()
.numpy()
)
toivanenraster_time = time.time() - tic
tic = time.time()
fastraster_output_cpu = np.squeeze(
FastGeodis.signed_generalised_geodesic3d(
input_image_pt, seed_image_pt, spacing, 1e10, 1.0, 4
)
.detach()
.cpu()
.numpy()
)
fastraster_time_cpu = time.time() - tic
device = (
"cuda" if input_image_pt.shape[1] == 1 and torch.cuda.is_available() else None
)
if device:
input_image_pt = input_image_pt.to(device)
seed_image_pt = seed_image_pt.to(device)
tic = time.time()
fastraster_output_gpu = np.squeeze(
FastGeodis.signed_generalised_geodesic3d(
input_image_pt, seed_image_pt, spacing, 1e10, 1.0, 4
)
.detach()
.cpu()
.numpy()
)
fastraster_time_gpu = time.time() - tic
print(
"Fast Marching CPU: {:.6} s \nToivanen's CPU raster: {:.6f} s \nFastGeodis CPU raster: {:.6f} s".format(
fastmarch_time, toivanenraster_time, fastraster_time_cpu
)
)
if device:
print("FastGeodis GPU raster: {:.6f} s".format(fastraster_time_gpu))
img_toivanenraster_output = sitk.GetImageFromArray(toivanenraster_output)
img_toivanenraster_output.SetSpacing(spacing_raw)
sitk.WriteImage(
img_toivanenraster_output, os.path.join(image_folder, "image3d_dis2.nii.gz")
)
img_d3 = sitk.GetImageFromArray(fastraster_output_cpu)
img_d3.SetSpacing(spacing_raw)
sitk.WriteImage(img_d3, os.path.join(image_folder, "image3d_dis3.nii.gz"))
input_image_sub = sitk.GetImageFromArray(input_image)
input_image_sub.SetSpacing(spacing_raw)
sitk.WriteImage(input_image_sub, os.path.join(image_folder, "image3d_sub.nii.gz"))
input_image = input_image * 255 / input_image.max()
input_image = np.asarray(input_image, np.uint8)
image_slice = input_image[10]
fastmarch_output_slice = fastmarch_output[10]
toivanenraster_output_slice = toivanenraster_output[10]
fastraster_output_cpu_slice = fastraster_output_cpu[10]
if device:
fastraster_output_gpu_slice = fastraster_output_gpu[10]
plt.figure(figsize=(18, 6))
plt.subplot(2, 5, 1)
plt.imshow(image_slice, cmap="gray")
plt.autoscale(False)
plt.plot([70], [60], "ro")
plt.axis("off")
plt.title("(a) Input image")
plt.subplot(2, 4, 5)
plt.imshow(fastmarch_output_slice)
plt.axis("off")
plt.title("(b) Fast Marching (cpu) | ({:.4f} s)".format(fastmarch_time))
plt.subplot(2, 4, 2)
plt.imshow(toivanenraster_output_slice)
plt.axis("off")
plt.title("(c) Toivanen's Raster (cpu) | ({:.4f} s)".format(toivanenraster_time))
plt.subplot(2, 4, 3)
plt.imshow(fastraster_output_cpu_slice)
plt.axis("off")
plt.title("(e) FastGeodis (cpu) | ({:.4f} s)".format(fastraster_time_cpu))
plt.subplot(2, 4, 6)
plt.imshow(toivanenraster_output_slice)
plt.axis("off")
plt.title("(d) Toivanen's Raster (cpu) | ({:.4f} s)".format(toivanenraster_time))
if device:
plt.subplot(2, 4, 7)
plt.imshow(fastraster_output_gpu_slice)
plt.axis("off")
plt.title("(f) FastGeodis (gpu) | ({:.4f} s)".format(fastraster_time_gpu))
diff = (
abs(fastmarch_output - fastraster_output_cpu)
/ (fastmarch_output + 1e-7)
* 100
)
diff_vol = fastmarch_output - fastraster_output_cpu
diff_slice = diff_vol[10]
plt.subplot(2, 4, 4)
plt.imshow(diff_slice)
plt.axis("off")
plt.title(
"(g) Fast Marching vs. FastGeodis (cpu)\ndiff: max: {:.4f} | min: {:.4f}".format(
np.max(diff), np.min(diff)
)
)
if device:
diff = (
abs(fastmarch_output - fastraster_output_gpu)
/ (fastmarch_output + 1e-7)
* 100
)
diff_vol = fastmarch_output - fastraster_output_gpu
diff_slice = diff_vol[10]
plt.subplot(2, 4, 8)
plt.imshow(diff_slice)
plt.axis("off")
plt.title(
"(h) Fast Marching vs. FastGeodis (gpu)\ndiff: max: {:.4f} | min: {:.4f}".format(
np.max(diff), np.min(diff)
)
)
plt.show()
if SHOW_JOINT_HIST:
plt.figure(figsize=(12, 6))
plt.subplot(1, 3, 1)
plt.title("Joint histogram\nFast Marching vs. Toivanen's Raster (cpu)")
plt.hist2d(
fastmarch_output.flatten(), toivanenraster_output.flatten(), bins=50
)
plt.xlabel("Fast Marching (cpu)")
plt.ylabel("Toivanen's Raster (cpu)")
plt.subplot(1, 3, 2)
plt.title("Joint histogram\nFast Marching (cpu) vs. FastGeodis (cpu)")
plt.hist2d(
fastmarch_output.flatten(), fastraster_output_cpu.flatten(), bins=50
)
plt.xlabel("Fast Marching (cpu)")
plt.ylabel("FastGeodis (cpu)")
if device:
plt.subplot(1, 3, 3)
plt.title("Joint histogram\nFast Marching (cpu) vs. FastGeodis (cpu)")
plt.hist2d(
fastmarch_output.flatten(),
fastraster_output_gpu.flatten(),
bins=50,
)
plt.xlabel("Fast Marching (cpu)")
plt.ylabel("FastGeodis (gpu)")
# plt.gca().set_aspect("equal", adjustable="box")
plt.tight_layout()
plt.show()
if __name__ == "__main__":
demo_geodesic_distance3d("data/img3d.nii.gz", [10, 60, 70])