-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils_viz.py
369 lines (261 loc) · 10.4 KB
/
utils_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
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
from random import randint
import cv2
import numpy as np
import pandas as pd
import os
import sys
import math
import torch
import torch.nn as nn
import torch.optim as optim
from torch.optim import lr_scheduler
import matplotlib
import matplotlib.pyplot as plt
from PIL import Image
from torchsummary import summary
from scipy import signal
from scipy import misc
from skimage.io import imsave
from tqdm import tqdm
import time
import argparse
import logging
from datetime import datetime
from collections import defaultdict
import pydicom
from pydicom.data import get_testdata_files
from IPython.display import clear_output
from torch.utils.data import DataLoader, Dataset
from albumentations import (HorizontalFlip, VerticalFlip, Rotate, MotionBlur, MedianBlur, Blur,
Compose, OneOf, ElasticTransform, GridDistortion, OpticalDistortion,
CropNonEmptyMaskIfExists, Resize, Normalize)
from utils_temporal import (generate_batch, remove_empty_masks, encode_mask, get_and_process_rows,
calc_loss_new, print_metrics, get_image_and_mask, get_rows)
from utils import process
class_to_color = {
0: [250, 240, 100], # OW - yellow
1: [120, 220, 140], # IW - green
2: [230, 150, 150], # tumor - orange
}
def viz_small_results(df, c, models, models_A2, device):
t=0.8
SIZE = 28
### Get images
img, mask = get_image_and_mask(df.loc[c], df.loc[c].data_path, img_size=256)
### Preprocess
X, mask_ = process(img[0].copy(), mask[:,:,0].copy())
X = torch.tensor(X[None])
X_for_kernel, mask_for_kernel = process(img[0].copy(), mask[:,:,0].copy(), HALF_CROP=50, img_size=20)
X_for_kernel = torch.tensor(X_for_kernel[None])
mask = mask_
### Viz
num_models = 8
fig, ax = plt.subplots(1, num_models, figsize=(num_models*5, 5))
for i in range(num_models):
ax[i].axis("off")
# 0: Input image
img = X[0, 0, :, :].numpy()
img = (img - img.min()) * 255. / (img.max() - img.min())
img = np.array(img, dtype=np.uint8)
img_rgb = np.stack([img, img, img], axis=-1)
ax[0].imshow(img_rgb / 255. , cmap="bone")
ax[0].set_title("Input image", fontsize=SIZE)
# 1: Ground truth
for i in range(3):
mask_ = np.array(mask[i, :, :], dtype=bool)
color = class_to_color[i]
for c_ in range(3):
img_rgb[mask_,c_] = color[c_]
ax[1].imshow(img_rgb/ 255.)
ax[1].set_title("Ground truth", fontsize=SIZE)
j = 2
# 2-4: Baseline models
for model_name in ["U-Net", "U-Net Dilated", "E-Net"]:
with torch.no_grad():
model = models[model_name].to(device)
out_ = model(X.float().to(device))
out = torch.sigmoid(out_)
out_args = torch.argmax(out[0, :, :, :], dim=0).cpu().numpy()
img_rgb = np.stack([img, img, img], axis=-1)
for i in range(3):
mask_ = (out[0, i, :, :].cpu().numpy() > t) & (out_args == i)
mask_ = np.array(mask_, dtype=bool)
color = class_to_color[i]
for c_ in range(3):
img_rgb[mask_,c_] = color[c_]
ax[j].imshow(img_rgb)
ax[j].set_title(model_name, fontsize=SIZE)
j+=1
# 5,7: Temporal and Bi-LSTM models
try:
for t, model_name in zip([0.5, 0.2], ["Bi-LSTM U-Net", "Bi-LSTM Hollow \nKernels U-Net"]):
model = models[model_name].to(device)
df_, ind_ = get_rows(df, c)
imgs, masks = get_and_process_rows(df_, ind_)
with torch.no_grad():
out_ = model(imgs.to(device))
out = torch.sigmoid(out_[1])
out_args = torch.argmax(out[ind_, :, :, :], dim=0).cpu().numpy()
img = imgs[ind_, 0, :, :].numpy()
img = (img - img.min()) * 255. / (img.max() - img.min())
img = np.array(img, dtype=np.uint8)
img_rgb = np.stack([img, img, img], axis=-1)
for i in range(3):
mask_ = (out[ind_, i, :, :].cpu().numpy() > t)
mask_ = np.array(mask_, dtype=bool)
color = class_to_color[i]
for c_ in range(3):
img_rgb[mask_,c_] = color[c_]
ax[j].imshow(img_rgb)
ax[j].set_title(model_name, fontsize=SIZE)
j += 2
except:
pass
### 6: Hollow kernels
model_name = "A2 Config. 2.1"
model = models_A2[model_name]
model = model.to(device)
with torch.no_grad():
out = model(X.to(device))
out = torch.sigmoid(out)
out_args = torch.argmax(out[0, :, :, :], dim=0).cpu().numpy()
img_rgb = np.stack([img, img, img], axis=-1)
for i in range(3):
mask_ = (out[0, i, :, :].cpu().numpy() > t)# & (out_args == i)
mask_ = np.array(mask_, dtype=bool)
color = class_to_color[i]
for c_ in range(3):
img_rgb[mask_,c_] = color[c_]
ax[6].imshow(img_rgb)
title = "Hollow Kernel \nU-Net"
ax[6].set_title(title, fontsize=SIZE)
plt.subplots_adjust(wspace=0.1)
plt.show()
def viz_big_results(df, c, models, models_and_kernels, models_A2, device):
### #1: ORIGINAL IMAGE, GROUND TRUTH, BASELINES
SIZE = 28
### Get images
img, mask = get_image_and_mask(df.loc[c], df.loc[c].data_path, img_size=256)
### Preprocess
X, mask_ = process(img[0].copy(), mask[:,:,0].copy())
X = torch.tensor(X[None])
X_for_kernel, mask_for_kernel = process(img[0].copy(), mask[:,:,0].copy(), HALF_CROP=50, img_size=20)
X_for_kernel = torch.tensor(X_for_kernel[None])
mask = mask_
num_models = 8
fig, ax = plt.subplots(1, num_models, figsize=(num_models*5, 5))
for i in range(num_models):
ax[i].axis("off")
# 0: Input image
img = X[0, 0, :, :].numpy()
img = (img - img.min()) * 255. / (img.max() - img.min())
img = np.array(img, dtype=np.uint8)
img_rgb = np.stack([img, img, img], axis=-1)
ax[0].imshow(img_rgb / 255. , cmap="bone")
ax[0].set_title("Input image", fontsize=SIZE)
# 1: Ground truth
for i in range(3):
mask_ = np.array(mask[i, :, :], dtype=bool)
color = class_to_color[i]
for c_ in range(3):
img_rgb[mask_,c_] = color[c_]
ax[1].imshow(img_rgb/ 255.)
ax[1].set_title("Ground truth", fontsize=SIZE)
j = 2
# 2-5: models
t=0.8
for model_name in ["U-Net", "U-Net Dilated", "U-Net PD", "E-Net"]:
with torch.no_grad():
model = models[model_name].to(device)
out_ = model(X.float().to(device))
out = torch.sigmoid(out_)
out_args = torch.argmax(out[0, :, :, :], dim=0).cpu().numpy()
img_rgb = np.stack([img, img, img], axis=-1)
for i in range(3):
mask_ = (out[0, i, :, :].cpu().numpy() > t) & (out_args == i)
mask_ = np.array(mask_, dtype=bool)
color = class_to_color[i]
for c_ in range(3):
img_rgb[mask_,c_] = color[c_]
ax[j].imshow(img_rgb)
ax[j].set_title(model_name, fontsize=SIZE)
j+=1
device = torch.device("cuda:2" if torch.cuda.is_available() else "cpu")
# 6,7: Temporal and Bi-LSTM models
try:
for model_name in ["Temporal U-Net", "Bi-LSTM U-Net"]:
t = 0.2
model = models[model_name].to(device)
df_, ind_ = get_rows(df, c)
imgs, masks = get_and_process_rows(df_, ind_)
with torch.no_grad():
out_ = model(imgs.to(device))
out = torch.sigmoid(out_[1])
out_args = torch.argmax(out[ind_, :, :, :], dim=0).cpu().numpy()
img = imgs[ind_, 0, :, :].numpy()
img = (img - img.min()) * 255. / (img.max() - img.min())
img = np.array(img, dtype=np.uint8)
img_rgb = np.stack([img, img, img], axis=-1)
for i in range(3):
mask_ = (out[ind_, i, :, :].cpu().numpy() > t)
mask_ = np.array(mask_, dtype=bool)
color = class_to_color[i]
for c_ in range(3):
img_rgb[mask_,c_] = color[c_]
ax[j].imshow(img_rgb)
ax[j].set_title(model_name, fontsize=SIZE)
j += 1
except:
pass
plt.subplots_adjust(wspace=0.1)
plt.show()
### #2: HOLOW KERNELS
# Parameters
num_models = 8
j = 0
t = 0.5
# Viz set-ups
fig, ax = plt.subplots(1, num_models, figsize=(num_models*5, 5))
for i in range(num_models):
ax[i].axis("off")
### A1: Hollow kernels
for model_name in models_and_kernels.keys():
model = models_and_kernels[model_name]["model"]
model = model.to(device)
model_kernel = models_and_kernels[model_name]["kernel"]
model_kernel = model_kernel.to(device)
kernel = model_kernel(X_for_kernel.to(device))
with torch.no_grad():
out = model(X.to(device), kernel)
out = torch.sigmoid(out)
out_args = torch.argmax(out[0, :, :, :], dim=0).cpu().numpy()
img_rgb = np.stack([img, img, img], axis=-1)
for i in range(3):
mask_ = (out[0, i, :, :].cpu().numpy() > t)# & (out_args == i)
mask_ = np.array(mask_, dtype=bool)
color = class_to_color[i]
for c_ in range(3):
img_rgb[mask_,c_] = color[c_]
ax[j].imshow(img_rgb)
ax[j].set_title(model_name, fontsize=SIZE)
j += 1
### A2: Opt
for model_name in models_A2.keys():
with torch.no_grad():
model = models_A2[model_name].to(device)
model = model.to(device)
out_ = model(X.float().to(device))
out = torch.sigmoid(out_)
out_args = torch.argmax(out[0, :, :, :], dim=0).cpu().numpy()
img_rgb = np.stack([img, img, img], axis=-1)
for i in range(3):
mask_ = (out[0, i, :, :].cpu().numpy() > t) & (out_args == i)
mask_ = np.array(mask_, dtype=bool)
color = class_to_color[i]
for c_ in range(3):
img_rgb[mask_,c_] = color[c_]
ax[j].imshow(img_rgb)
ax[j].set_title(model_name, fontsize=SIZE)
j+=1
plt.subplots_adjust(wspace=0.1)
plt.show()