-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsample_out_viz.py
173 lines (131 loc) · 6.78 KB
/
sample_out_viz.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
import numpy as np
from scipy.io import loadmat
import matplotlib.pyplot as plt
def img_plot(mat_path, img_save_path):
data = loadmat(mat_path)
orig_imgs, seg, pred = data["all_test_images"], data["all_test_labels"], data["all_test_outputs"]
# Choose four random indices from the length of the batch
indices = np.random.choice(len(orig_imgs), 4, replace=False)
fig, axs = plt.subplots(4, 7, figsize=(30, 15)) # Create a 4x3 subplot grid
for i, idx in enumerate(indices):
slice_id = np.random.randint(30, 91)
# Plotting original image
axs[i, 0].imshow(orig_imgs[idx][1,1,slice_id], cmap="gray")
axs[i, 0].set_title("Original Image")
# Plotting segmentation mask
axs[i, 1].imshow(seg[idx][1,0,slice_id]>0, cmap="gray")
axs[i, 1].set_title("Segmentation Mask (Whole Tumor)")
# Plotting prediction
axs[i, 2].imshow(pred[idx][1,0,slice_id]>0, cmap="gray")
axs[i, 2].set_title("Prediction for Whole Tumor Mask")
# Plotting segmentation mask
axs[i, 3].imshow(((seg[idx][1,0,slice_id]==1) + (seg[idx][1,0,slice_id]==3))>0, cmap="gray")
axs[i, 3].set_title("Segmentation Mask (Tumor Core)")
# Plotting prediction
axs[i, 4].imshow(pred[idx][1,1,slice_id]>0, cmap="gray")
axs[i, 4].set_title("Prediction for Tumor Core Mask")
# Plotting segmentation mask
axs[i, 5].imshow(seg[idx][1,0,slice_id]==3, cmap="gray")
axs[i, 5].set_title("Segmentation Mask (Enhancing Tumor)")
# Plotting prediction
axs[i, 6].imshow(pred[idx][1,2,slice_id]>0, cmap="gray")
axs[i, 6].set_title("Prediction for Enhancing Tumor Mask")
# Hide x and y ticks for all subplots
for ax in axs.flatten():
ax.axis('off')
plt.tight_layout()
plt.savefig(img_save_path)
def img_plot_deep_supervision(mat_path, img_save_path):
data = loadmat(mat_path)
orig_imgs, seg, pred, ds_outs = data["all_test_images"], data["all_test_labels"], data["all_test_outputs"], data["ds_outs"]
# Choose four random indices from the length of the batch
indices = np.random.choice(len(orig_imgs), 4, replace=False)
fig, axs = plt.subplots(4, 13, figsize=(90, 20)) # Create a 4x3 subplot grid
for i, idx in enumerate(indices):
slice_id = np.random.randint(30, 91)
# Plotting original image
axs[i, 0].imshow(orig_imgs[idx][1,1,slice_id], cmap="gray")
axs[i, 0].set_title("Original Image")
# Plotting segmentation mask
axs[i, 1].imshow(seg[idx][1,0,slice_id]>0, cmap="gray")
axs[i, 1].set_title("Segmentation Mask (Whole Tumor)")
# Plotting prediction
axs[i, 2].imshow(ds_outs[idx][2][1,0,slice_id], cmap="gray")
axs[i, 2].set_title("ds2 for Whole Tumor Mask")
axs[i, 3].imshow(ds_outs[idx][1][1,0,slice_id], cmap="gray")
axs[i, 3].set_title("ds1 for Whole Tumor Mask")
axs[i, 4].imshow(pred[idx][1,0,slice_id]>0, cmap="gray")
axs[i, 4].set_title("Prediction for Whole Tumor Mask")
# Plotting segmentation mask
axs[i, 5].imshow(((seg[idx][1,0,slice_id]==1) + (seg[idx][1,0,slice_id]==3))>0, cmap="gray")
axs[i, 5].set_title("Segmentation Mask (Tumor Core)")
# Plotting prediction
axs[i, 6].imshow(ds_outs[idx][2][1,1,slice_id], cmap="gray")
axs[i, 6].set_title("ds2 for Whole Tumor Mask")
axs[i, 7].imshow(ds_outs[idx][1][1,1,slice_id], cmap="gray")
axs[i, 7].set_title("ds1 for Whole Tumor Mask")
axs[i, 8].imshow(pred[idx][1,1,slice_id]>0, cmap="gray")
axs[i, 8].set_title("Prediction for Whole Tumor Mask")
# Plotting segmentation mask
axs[i, 9].imshow(seg[idx][1,0,slice_id]==3, cmap="gray")
axs[i, 9].set_title("Segmentation Mask (Enhancing Tumor)")
# Plotting prediction
axs[i, 10].imshow(ds_outs[idx][2][1,2,slice_id], cmap="gray")
axs[i, 10].set_title("ds2 for Whole Tumor Mask")
axs[i, 11].imshow(ds_outs[idx][1][1,2,slice_id], cmap="gray")
axs[i, 11].set_title("ds1 for Whole Tumor Mask")
axs[i, 12].imshow(pred[idx][1,2,slice_id]>0, cmap="gray")
axs[i, 12].set_title("Prediction for Whole Tumor Mask")
# Hide x and y ticks for all subplots
for ax in axs.flatten():
ax.axis('off')
plt.tight_layout()
plt.savefig(img_save_path)
def img_plot_deep_supervision2(mat_path, img_save_path):
data = loadmat(mat_path)
orig_imgs, seg, pred, ds_outs = data["all_test_images"], data["all_test_labels"], data["all_test_outputs"], data["ds_outs"]
idx = np.random.randint(len(orig_imgs)) # Choose a random index
slice_id = np.random.randint(30, 91)
fig, axs = plt.subplots(3, 4, figsize=(40, 30)) # Create a 3x4 subplot grid
# Plotting for Whole Tumor
axs[0, 0].imshow(seg[idx][1, 0, slice_id] > 0, cmap="gray")
axs[0, 0].axis('off')
axs[0, 1].imshow(ds_outs[idx][2][1, 0, slice_id], cmap="gray")
axs[0, 1].axis('off')
axs[0, 2].imshow(ds_outs[idx][1][1, 0, slice_id], cmap="gray")
axs[0, 2].axis('off')
axs[0, 3].imshow(pred[idx][1, 0, slice_id] > 0, cmap="gray")
axs[0, 3].axis('off')
# Plotting for Tumor Core
axs[1, 0].imshow(((seg[idx][1, 0, slice_id] == 1) + (seg[idx][1, 0, slice_id] == 3)) > 0, cmap="gray")
axs[1, 0].axis('off')
axs[1, 1].imshow(ds_outs[idx][2][1, 1, slice_id], cmap="gray")
axs[1, 1].axis('off')
axs[1, 2].imshow(ds_outs[idx][1][1, 1, slice_id], cmap="gray")
axs[1, 2].axis('off')
axs[1, 3].imshow(pred[idx][1, 1, slice_id] > 0, cmap="gray")
axs[1, 3].axis('off')
# Plotting for Enhancing Tumor
axs[2, 0].imshow(seg[idx][1, 0, slice_id] == 3, cmap="gray")
axs[2, 0].axis('off')
axs[2, 1].imshow(ds_outs[idx][2][1, 2, slice_id], cmap="gray")
axs[2, 1].axis('off')
axs[2, 2].imshow(ds_outs[idx][1][1, 2, slice_id], cmap="gray")
axs[2, 2].axis('off')
axs[2, 3].imshow(pred[idx][1, 2, slice_id] > 0, cmap="gray")
axs[2, 3].axis('off')
for ax, col in zip(axs[0], ['Segmentation mask', 'First DS Output', 'Second DS Output', 'Prediction']):
ax.set_title(col, fontsize=60)
for ax, row in zip(axs[:,0], ['Class Whole Tumor', 'Class Tumor Core', 'Class Enhancing Tumor']):
ax.set_ylabel(row, fontsize=60, labelpad=1000)
plt.subplots_adjust(wspace=5, hspace=5) # Adjust spacing between subplots
plt.tight_layout()
# Save the grid of subplots
plt.savefig(img_save_path + "_grid.png", bbox_inches='tight')
# Show the original image separately
plt.figure(figsize=(5, 5))
plt.imshow(orig_imgs[idx][1, 1, slice_id], cmap="gray")
plt.title("Original Image")
plt.axis('off')
# Save the original image separately
plt.savefig(img_save_path + "_original.png", bbox_inches='tight')