-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_viz_model_5.py
566 lines (473 loc) · 21 KB
/
get_viz_model_5.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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
"""
This file is used to generate the visualizations
of the pretrained models
it will take the dataset list the pretrained models path the name of the pretrained model to get the visualization in the desired folder
example -> python gen_viz_model_5.py --datasetlist paottest5 --os_save_fold fold_name
To get the visualization the file should be saved in ./dataset/datasetlist/
moreover it should be saved with _val.txt suffix.
For above case paottest5_val.txt must be present
"""
import torch
import numpy as np
import matplotlib.pyplot as plt
from tqdm import tqdm
from monai.inferers import sliding_window_inference
import os
import argparse
from types import SimpleNamespace
from monai.data import DataLoader, Dataset
from mpl_toolkits.axes_grid1 import make_axes_locatable
from matplotlib.patches import Patch
from gg_tools import debug_label_distribution, process_ground_truth, modified_merge_label_v1, merge_label_v1, get_val_txt_loader, get_test_data_loader, dice_score, TEMPLATE, get_key, NUM_CLASS, ORGAN_NAME, organ_post_process, dice_score_np, save_result_2
def create_fixed_colormap():
"""
Create a fixed colormap for all organ labels
"""
# Start with transparent background
colors = [[1, 1, 1, 0]] # Background (transparent)
# Get colors from tab20 colormap for first 20 labels
colors.extend(plt.cm.tab20(np.linspace(0, 1, 20)))
# Get additional colors from Set3 for remaining labels
colors.extend(plt.cm.Set3(np.linspace(0, 1, 12)))
# Ensure consistent alpha for all non-background colors
colors = np.array(colors)
colors[1:, 3] = 0.7 # Set alpha for all non-background colors
return plt.cm.colors.ListedColormap(colors)
def get_max_area_slice(gt_mask):
"""
Find slice with maximum organ area, properly handling multi-label data
Args:
gt_mask: 3D numpy array with multiple labels (1,H,W,D)
Returns:
slice_idx: Index of the axial slice with maximum organ area
"""
if gt_mask.shape[0] == 1:
gt_mask = gt_mask[0]
# Calculate areas for each slice
areas = []
for i in range(gt_mask.shape[2]):
slice_data = gt_mask[:, :, i]
# Count pixels that belong to any organ (any non-zero value)
area = np.sum(slice_data > 0)
areas.append(area)
# Find slice with maximum organ area
max_slice = np.argmax(areas)
# Print diagnostic information
print("\nSlice Selection Statistics:")
print(f"Maximum area slice: {max_slice}")
print(f"Area in maximum slice: {areas[max_slice]} pixels")
print(f"Average area across slices: {np.mean(areas):.2f} pixels")
print(f"Number of slices with organs: {np.sum(np.array(areas) > 0)}")
return max_slice
def visualize_predictions(gt_volume, predictions_dict, save_path, input_image_volume, slice_idx):
"""
Improved visualization handling multi-label data
"""
n_models = len(predictions_dict) + 1
fig, axes = plt.subplots(1, n_models, figsize=(4*n_models, 4))
custom_cmap = create_fixed_colormap()
# Get input slice
input_slice = input_image_volume[0, :, :, slice_idx]
input_normalized = (input_slice - input_slice.min()) / (input_slice.max() - input_slice.min())
# Plot ground truth
gt_slice = gt_volume[0, :, :, slice_idx]
axes[0].imshow(input_normalized, cmap='gray')
axes[0].imshow(gt_slice, cmap=custom_cmap, alpha=0.7)
axes[0].set_title('Ground Truth')
axes[0].axis('off')
# Plot predictions
for idx, (model_name, pred_volume) in enumerate(predictions_dict.items(), 1):
pred_slice = pred_volume[0, :, :, slice_idx]
axes[idx].imshow(input_normalized, cmap='gray')
axes[idx].imshow(pred_slice, cmap=custom_cmap, alpha=0.7)
axes[idx].set_title(model_name)
axes[idx].axis('off')
plt.tight_layout()
plt.savefig(save_path, dpi=300, bbox_inches='tight', facecolor='white')
plt.close()
def test_all_models(args):
"""Test the models on all images in the dataset"""
# Get data loader
film_loader, film_transform = get_val_txt_loader(args)
# Initialize models dictionary
models = {}
print("Loading models...")
try:
# Deep Film model
print("Loading Deep Film model...")
from model.SwinUNETR_DEEP_FILM import SwinUNETR_DEEP_FILM
models['CLIP-Deep'] = SwinUNETR_DEEP_FILM(
img_size=(args.roi_x, args.roi_y, args.roi_z),
in_channels=1,
out_channels=32,
precomputed_prompt_path=args.precomputed_prompt_path
)
# Universal model
print("Loading Universal model...")
from model.Universal_model import Universal_model
models['Universal'] = Universal_model(
img_size=(args.roi_x, args.roi_y, args.roi_z),
in_channels=1,
out_channels=32,
backbone=args.backbone,
encoding=args.trans_encoding
)
# SwinUNETR
print("Loading SwinUNETR model...")
from monai.networks.nets import SwinUNETR
models['SwinUNETR'] = SwinUNETR(
img_size=(args.roi_x, args.roi_y, args.roi_z),
in_channels=1,
out_channels=32
)
# UNETR
print("Loading UNETR model...")
from monai.networks.nets import UNETR
models['UNETR'] = UNETR(
img_size=(args.roi_x, args.roi_y, args.roi_z),
in_channels=1,
out_channels=32
)
except Exception as e:
print(f"Error loading models: {str(e)}")
return
print("Loading model weights...")
# Load pretrained weights
for model_name, model in models.items():
try:
checkpoint_path = args.model_checkpoints[model_name]
print(f"Loading weights for {model_name} from {checkpoint_path}")
checkpoint = torch.load(checkpoint_path)
store_dict = model.state_dict()
load_dict = checkpoint['net']
if model_name == 'Universal' and args.universal_author:
for key, value in load_dict.items():
key = '.'.join(key.split('.')[1:])
if 'swinViT' in key or 'encoder' in key or 'decoder' in key:
key = '.'.join(['backbone', key])
if key in store_dict.keys():
store_dict[key] = value
else:
for key, value in load_dict.items():
if 'swinViT' in key or 'encoder' in key or 'decoder' in key:
name = '.'.join(key.split('.')[1:])
else:
name = '.'.join(key.split('.')[1:])
if name in store_dict.keys():
store_dict[name] = value
model.load_state_dict(store_dict)
model = model.to(args.device)
model.eval()
print(f"Successfully loaded {model_name}")
except Exception as e:
print(f"Error loading weights for {model_name}: {str(e)}")
return
# Process all images
print("Processing all images...")
for batch_idx, batch in enumerate(tqdm(film_loader, desc="Processing images")):
try:
image = batch['image'].to(args.device)
name = batch['name']
prompt = batch['prompt']
print(f"\nProcessing image {batch_idx + 1}: {name[0]}")
ground_truth = batch['post_label'][0].numpy()
debug_label_distribution(ground_truth, name[0]) # Add this to understand label distribution
ground_truth_merged = process_ground_truth(ground_truth, name[0])
input_image = batch['image'][0].cpu().numpy()
predictions = {}
with torch.no_grad():
for model_name, model in models.items():
print(f"Running inference with {model_name}...")
if model_name == 'CLIP-Deep':
predictor = lambda image_patch: model(image_patch, prompt)
pred = sliding_window_inference(
image,
(args.roi_x, args.roi_y, args.roi_z),
1,
predictor
)
else:
pred = sliding_window_inference(
image,
(args.roi_x, args.roi_y, args.roi_z),
1,
model
)
pred_sigmoid = torch.squeeze(torch.sigmoid(pred))
pred_mask = torch.where(pred_sigmoid >= 0.5, 1, 0).cpu().numpy()
if args.post_process:
template_key = get_key(name[0])
organ_list = TEMPLATE[template_key]
pred_mask = organ_post_process(pred_mask, organ_list)
pred_mask_merged = merge_label_v1(pred_mask, name[0])
predictions[model_name] = pred_mask_merged.astype(np.uint8)
print(f"Completed {model_name} inference")
# Find slice with maximum area
max_slice = get_max_area_slice(ground_truth_merged)
# Save visualization
file_name = name[0].split('.')[0].replace('/', '_')
save_path = os.path.join(
args.visualization_path,
f'{file_name}_axial_slice_{max_slice}_comparison.png'
)
print(f"Saving visualization to {save_path}")
visualize_predictions(
ground_truth_merged,
predictions,
save_path,
input_image,
max_slice
)
# Calculate and print Dice scores for each model
print("\nDice Scores:")
for model_name, pred in predictions.items():
dice = dice_score_np(pred, ground_truth_merged)
print(f"{model_name}: {dice:.4f}")
except Exception as e:
print(f"Error processing batch {batch_idx}: {str(e)}")
continue
print("\nCompleted processing all images")
def test_single_image(args):
"""Test the models on a single image first"""
# Get the first image from the dataset
film_loader, film_transform = get_val_txt_loader(args)
first_batch = next(iter(film_loader))
# Initialize models dictionary
models = {}
print("Loading models...")
try:
# Deep Film model
print("Loading Deep Film model...")
from model.SwinUNETR_DEEP_FILM import SwinUNETR_DEEP_FILM
models['CLIP-Deep'] = SwinUNETR_DEEP_FILM(
img_size=(args.roi_x, args.roi_y, args.roi_z),
in_channels=1,
out_channels=32,
precomputed_prompt_path=args.precomputed_prompt_path
)
# Universal model
print("Loading Universal model...")
from model.Universal_model import Universal_model
models['Universal'] = Universal_model(
img_size=(args.roi_x, args.roi_y, args.roi_z),
in_channels=1,
out_channels=32,
backbone=args.backbone,
encoding=args.trans_encoding
)
# SwinUNETR
print("Loading SwinUNETR model...")
from monai.networks.nets import SwinUNETR
models['SwinUNETR'] = SwinUNETR(
img_size=(args.roi_x, args.roi_y, args.roi_z),
in_channels=1,
out_channels=32
)
# UNETR
print("Loading UNETR model...")
from monai.networks.nets import UNETR
models['UNETR'] = UNETR(
img_size=(args.roi_x, args.roi_y, args.roi_z),
in_channels=1,
out_channels=32
)
except Exception as e:
print(f"Error loading models: {str(e)}")
return
print("Loading model weights...")
# Load pretrained weights
for model_name, model in models.items():
try:
checkpoint_path = args.model_checkpoints[model_name]
print(f"Loading weights for {model_name} from {checkpoint_path}")
checkpoint = torch.load(checkpoint_path)
store_dict = model.state_dict()
load_dict = checkpoint['net']
if model_name == 'Universal' and args.universal_author:
for key, value in load_dict.items():
key = '.'.join(key.split('.')[1:])
if 'swinViT' in key or 'encoder' in key or 'decoder' in key:
key = '.'.join(['backbone', key])
if key in store_dict.keys():
store_dict[key] = value
else:
for key, value in load_dict.items():
if 'swinViT' in key or 'encoder' in key or 'decoder' in key:
name = '.'.join(key.split('.')[1:])
else:
name = '.'.join(key.split('.')[1:])
if name in store_dict.keys():
store_dict[name] = value
model.load_state_dict(store_dict)
model = model.to(args.device)
model.eval()
print(f"Successfully loaded {model_name}")
except Exception as e:
print(f"Error loading weights for {model_name}: {str(e)}")
return
print("Processing single image...")
# Process the single image
try:
image = first_batch['image'].to(args.device)
name = first_batch['name']
prompt = first_batch['prompt']
print(f"Processing image: {name[0]}")
print(first_batch['post_label'].shape)
ground_truth = first_batch['post_label'][0].numpy()
debug_label_distribution(ground_truth, name[0]) # Add this to understand label distribution
ground_truth_merged = process_ground_truth(ground_truth, name[0])
# Store original input image for visualization
input_image = first_batch['image'][0].cpu().numpy()
predictions = {}
with torch.no_grad():
for model_name, model in models.items():
print(f"Running inference with {model_name}...")
if model_name == 'CLIP-Deep':
predictor = lambda image_patch: model(image_patch, prompt)
pred = sliding_window_inference(
image,
(args.roi_x, args.roi_y, args.roi_z),
1,
predictor
)
else:
pred = sliding_window_inference(
image,
(args.roi_x, args.roi_y, args.roi_z),
1,
model
)
pred_sigmoid = torch.squeeze(torch.sigmoid(pred))
pred_mask = torch.where(pred_sigmoid >= 0.5, 1, 0).cpu().numpy()
if args.post_process:
template_key = get_key(name[0])
organ_list = TEMPLATE[template_key]
pred_mask = organ_post_process(pred_mask, organ_list)
pred_mask_merged = merge_label_v1(pred_mask, name[0])
predictions[model_name] = pred_mask_merged.astype(np.uint8)
print(f"Completed {model_name} inference")
max_slice = get_max_area_slice(ground_truth_merged)
# Save visualization
file_name = name[0].split('.')[0].replace('/', '_')
save_path = os.path.join(
args.visualization_path,
f'{file_name}_axial_slice_{max_slice}_comparison.png'
)
print(f"Saving visualization to {save_path}")
visualize_predictions(
ground_truth_merged,
predictions,
save_path,
input_image,
max_slice
)
except Exception as e:
print(f"Error processing image: {str(e)}")
raise e
def process(args):
# Add new arguments needed for multi-model comparison
args.visualization_path = os.path.join(args.os_save_fold, 'visualizations')
args.model_checkpoints = {
'CLIP-Deep': args.deep_film_checkpoint,
'Universal': args.universal_checkpoint,
'SwinUNETR': args.swinunetr_checkpoint,
'UNETR': args.unetr_checkpoint
}
# Create visualization directory
os.makedirs(args.visualization_path, exist_ok=True)
# Test with single image first
print("Testing with single image...")
test_single_image(args)
# If successful, proceed with all images
user_input = input("Continue with all images? (y/n): ")
if user_input.lower() == 'y':
print("Processing all images...")
test_all_models(args)
else:
print("Exiting after single image test")
def main():
# Define default arguments
args = SimpleNamespace(
space_x = 1.5,
space_y = 1.5,
space_z = 1.5,
roi_x = 96,
roi_y = 96,
roi_z = 96,
num_samples = 2,
data_root_path = '/blue/kgong/s.kapoor/language_guided_segmentation/CLIP-Driven-Universal-Model/',
data_txt_path = './dataset/dataset_list/',
batch_size = 4,
num_workers = 1,
a_min = -175,
a_max = 250,
b_min = 0.0,
b_max = 1.0,
dataset_list = ['PAOTtest'], #here it is used to validate the model
NUM_CLASS = 32, # Make sure this matches your actual number of classes
backbone = 'swinunetr',
trans_encoding = 'word_embedding',
lr = 4e-4,
weight_decay = 1e-5,
precomputed_prompt_path = './pretrained_weights/embeddings_template.pkl',
word_embedding = './pretrained_weights/txt_encoding.pth',
dist = False,
device = torch.device("cuda" if torch.cuda.is_available() else "cpu"),
model_type = 'film',
file_name = 'paot_test_universal_postprocess.txt',
os_save_fold = './default_prediction_space',
deep_film_checkpoint = './out/deep_film_org_setting/epoch_380.pth',
universal_checkpoint = './out/universal_total_org/epoch_380.pth',
swinunetr_checkpoint = './out/swinunetr_monai/epoch_120.pth',
unetr_checkpoint = './out/unetr_monai/epoch_120.pth',
universal_author = False,
post_process = True
)
# Set up argument parser for command line overrides
parser = argparse.ArgumentParser(description='Multi-model comparison for medical image segmentation')
parser.add_argument('--precomputed_prompt_path',
default='./pretrained_weights/embeddings_template_flare.pkl',
help='the text embeddings to use')
parser.add_argument('--dataset_list',
nargs='+',
default=['PAOTtest'],
help='The dataset to be used, its txt file with location')
parser.add_argument('--deep_film_checkpoint',
default='./out/deep_film_org_setting/epoch_380.pth',
help='Path to Deep Film model checkpoint')
parser.add_argument('--universal_checkpoint',
default='./out/universal_total_org/epoch_380.pth',
help='Path to Universal model checkpoint')
parser.add_argument('--swinunetr_checkpoint',
default='./out/swinunetr_monai/epoch_120.pth',
help='Path to SwinUNETR model checkpoint')
parser.add_argument('--unetr_checkpoint',
default='./out/unetr_monai/epoch_120.pth',
help='Path to UNETR model checkpoint')
parser.add_argument('--universal_author',
action='store_true',
default=False,
help='Use Universal author weight loading method')
parser.add_argument('--os_save_fold',
default='./default_prediction_space',
help='Directory to save output predictions')
parser.add_argument('--post_process',
action='store_true',
default=True,
help='Apply post-processing to predictions')
# Parse command line arguments
parsed_args = parser.parse_args()
# Update default arguments with any command line overrides
args_dict = vars(parsed_args)
for key, value in args_dict.items():
if value is not None:
setattr(args, key, value)
# Print configuration
print("\nRunning with configuration:")
for key, value in vars(args).items():
print(f"{key}: {value}")
print("\n")
# Run the main process
process(args=args)
if __name__ == '__main__':
main()