-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinference_avali_annot_with_dice_Score.py
504 lines (427 loc) · 20.3 KB
/
inference_avali_annot_with_dice_Score.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
# -*- coding: utf-8 -*-
"""
train the image encoder and mask decoder
freeze prompt image encoder
"""
# %% setup environment
import numpy as np
import nibabel as nib
import matplotlib.pyplot as plt
import os
import re
join = os.path.join
from tqdm import tqdm
from skimage import transform
import torch
import torch.nn as nn
from torch.utils.data import Dataset, DataLoader, SubsetRandomSampler
import monai
from segment_anything import sam_model_registry
import torch.nn.functional as F
import argparse
import random
from datetime import datetime
import shutil
import glob
import wandb
from torchmetrics.utilities.data import to_categorical
from torchmetrics.utilities.distributed import reduce
from cal_dice import dice_score
from importlib import import_module
from scipy.ndimage import zoom
# set seeds
torch.manual_seed(2024)
torch.cuda.empty_cache()
# torch.distributed.init_process_group(backend="gloo")
os.environ["OMP_NUM_THREADS"] = "4" # export OMP_NUM_THREADS=4
os.environ["OPENBLAS_NUM_THREADS"] = "4" # export OPENBLAS_NUM_THREADS=4
os.environ["MKL_NUM_THREADS"] = "6" # export MKL_NUM_THREADS=6
os.environ["VECLIB_MAXIMUM_THREADS"] = "4" # export VECLIB_MAXIMUM_THREADS=4
os.environ["NUMEXPR_NUM_THREADS"] = "6" # export NUMEXPR_NUM_THREADS=6
def show_mask(mask, ax, random_color=False):
if random_color:
color = np.concatenate([np.random.random(3), np.array([0.6])], axis=0)
else:
color = np.array([251 / 255, 252 / 255, 30 / 255, 0.6])
h, w = mask.shape[-2:]
mask_image = mask.reshape(h, w, 1) * color.reshape(1, 1, -1)
ax.imshow(mask_image)
def show_box(box, ax):
x0, y0 = box[0], box[1]
w, h = box[2] - box[0], box[3] - box[1]
ax.add_patch(
plt.Rectangle((x0, y0), w, h, edgecolor="red", facecolor=(1, 1, 1, 0), lw=2)
)
class NpyDataset(Dataset):
def __init__(self, data_root, bbox_pt_lst = [10, 10, 502, 502], bbox_shift=20, num_classes =4):
self.data_root = data_root
self.bbox_pt_lst = bbox_pt_lst
self.gt_path = join(data_root, "gts")
self.img_path = join(data_root, "imgs")
self.num_classes = num_classes
print("self.gt_path", self.gt_path)
print("self.img_path", self.img_path)
#self.gt_path_files = sorted(
# glob.glob(join(self.gt_path, "**/*.npy"), recursive=True)
#)
# List and sort image files numerically
# List and sort image files numerically
img_files = [f for f in os.listdir(self.img_path) if f.endswith('.npy')]
self.img_path_files = sorted(
[os.path.join(self.img_path, file) for file in img_files],
key=lambda x: int(''.join(filter(str.isdigit, os.path.basename(x))))
)
# List and sort ground truth files numerically
gt_files = [f for f in os.listdir(self.gt_path) if f.endswith('.npy')]
self.gt_path_files = sorted(
[os.path.join(self.gt_path, file) for file in gt_files],
key=lambda x: int(''.join(filter(str.isdigit, os.path.basename(x))))
)
self.bbox_shift = bbox_shift
print(f"number of images in img_path: {len(self.img_path_files)}")
print(f"number of labels in gt_path: {len(self.gt_path_files)}")
def __len__(self):
return len(self.gt_path_files)
def __getitem__(self, index):
# load npy image (1024, 1024, 3), [0,1]
img_name = os.path.basename(self.img_path_files[index])
gt_pathname = self.gt_path + '/Final_' +img_name
#print("img_name", img_name)
#img_1024 here is the original size - changed code.
img_1024 = np.load(
join(self.img_path, img_name), "r", allow_pickle=True
) # (1024, 1024, 3)
#print("image path name",join(self.img_path, img_name))
#print("gt_path_name", gt_pathname)
# convert the shape to (3, H, W)
#img_1024 = np.transpose(img_1024, (2, 0, 1))
# Preprocessing data.
# Clip values: if greater than 80, set to 80; if below 0, set to 0
img_1024 = np.clip(img_1024, 0, 1)
#print("image range", img_1024.min(), img_1024.max())
gt = np.load(
gt_pathname, "r", allow_pickle=True
) # multiple labels [0, 1,4,5...], (256,256)
#assert img_name == os.path.basename(self.gt_path_files[index]), (
# "img gt name error" + self.gt_path_files[index] + self.npy_files[index]
#)
label_ids = np.unique(gt)[1:]
selected_label=3
gt2D = np.uint8(gt == selected_label) # only one label, (256, 256)
gt2D = gt2D[1, ...]
assert np.all(np.isin(np.unique(gt2D), [0, 1])), "Ground truth should be 0 or 1"
y_indices, x_indices = np.where(gt2D ==1)
#print("x_indices, y_indices ", x_indices, y_indices)
if x_indices.size == 0 or y_indices.size == 0:
x_min, x_max = 10, 502
y_min, y_max = 10, 502
else:
x_min, x_max = np.min(x_indices), np.max(x_indices)
y_min, y_max = np.min(y_indices), np.max(y_indices)
#print("x_min, x_max, y_min, y_max", x_min, x_max, y_min, y_max)
# add perturbation to bounding box coordinates
H, W = gt2D.shape
x_min = max(0, x_min - random.randint(0, self.bbox_shift))
x_max = min(W, x_max + random.randint(0, self.bbox_shift))
y_min = max(0, y_min - random.randint(0, self.bbox_shift))
y_max = min(H, y_max + random.randint(0, self.bbox_shift))
bboxes = np.array([x_min, y_min, x_max, y_max])
# Fix for now.
bboxes = np.array(self.bbox_pt_lst)
return (
torch.tensor(img_1024).float(),
torch.tensor(gt[None, 1, :, :]).long(),
torch.tensor(bboxes).float(),
img_name,
)
# %% set up parser
parser = argparse.ArgumentParser()
parser.add_argument(
"-i",
"--test_dir",
type=str,
default='/data/home/umang/Vader_umang/Seg_models/data/CTScans/test_scans',
help="path to training npy files; two subfolders: gts and imgs",
)
parser.add_argument("--scans_test_save_path", type= str, default="/data/home/umang/Vader_umang/Seg_models/MedSAM/Inference_scans/temp/", help="give path to save scans")
parser.add_argument("--org_data_dir", type=str, default='/data/home/umang/Vader_umang/Seg_models/data/CTScans/Scans_org/Scans_org')
parser.add_argument("-task_name", type=str, default="MedSAM-ViT-B")
parser.add_argument("-model_type", type=str, default="vit_b")
parser.add_argument(
"-sam_checkpoint", type=str, default="/data/home/umang/Vader_umang/Seg_models/MedSAM/medsam_vit_b.pth")
parser.add_argument(
"--load_pretrain", type=bool, default=True, help="load pretrain model"
)
parser.add_argument(
"-trained_model_path", type=str, default="/data/home/umang/Vader_umang/Seg_models/MedSAM/checkpoint_dir/MEDSAM_finetune_CT/MedSAM_finetune_CT-20240801-1152/medsam_model_latest.pth")
parser.add_argument('-device', type=str, default='cuda:7')
parser.add_argument("-work_dir", type=str, default="./work_dir")
# train
parser.add_argument("-num_epochs", type=int, default=1000)
parser.add_argument("-batch_size", type=int, default=1)
parser.add_argument("-num_workers", type=int, default=0)
# Optimizer parameters
parser.add_argument(
"-weight_decay", type=float, default=0.01, help="weight decay (default: 0.01)"
)
parser.add_argument(
"-lr", type=float, default=0.0001, metavar="LR", help="learning rate (absolute lr)"
)
parser.add_argument(
"-use_wandb", type=bool, default=False, help="use wandb to monitor training"
)
parser.add_argument("-use_amp", action="store_true", default=False, help="use amp")
parser.add_argument(
"--resume", type=str, default="", help="Resuming training from checkpoint"
)
parser.add_argument("--device", type=str, default="cuda:0")
parser.add_argument("--num_classes", type=int, default=4)
parser.add_argument("--img_size", type=int, default=512)
parser.add_argument("--include_bg", type=bool, default=False, help='include bacground for seg loss calculation')
parser.add_argument("--dice_param", type=float, default=0.8, help="ratio of dice loss to ce loss")
parser.add_argument("--train_split_ratio", type= float, default=0.75, help="train test split ratio")
args = parser.parse_args()
# Define parameters for the run name
timestamp = datetime.now().strftime("%Y%m%d-%H%M%S")
run_name = f"{args.task_name}_{args.model_type}_{timestamp}"
# %% set up model for training
device = args.device
run_id = datetime.now().strftime("%Y%m%d-%H%M")
model_save_path = join("TEST_CTSCANS", args.task_name + "-" + run_id)
device = torch.device(args.device)
# %% set up model
class MedSAM(nn.Module):
def __init__(
self,
image_encoder,
mask_decoder,
prompt_encoder,
):
super().__init__()
self.image_encoder = image_encoder
self.mask_decoder = mask_decoder
self.prompt_encoder = prompt_encoder
# Freeze all layers of image_encoder, mask_decoder, and prompt_encoder
self._freeze_parameters(self.image_encoder)
self._freeze_parameters(self.mask_decoder)
self._freeze_parameters(self.prompt_encoder)
def _freeze_parameters(self, module):
"""Freeze all parameters of the given module."""
for param in module.parameters():
param.requires_grad = False
def forward(self, image, box):
# print("image shape", image.shape)
image_embedding = self.image_encoder(image) # (B, 256, 64, 64)
# do not compute gradients for prompt encoder
with torch.no_grad():
box_torch = torch.as_tensor(box, dtype=torch.float32, device=image.device)
if len(box_torch.shape) == 2:
box_torch = box_torch[:, None, :] # (B, 1, 4)
sparse_embeddings, dense_embeddings = self.prompt_encoder(
points=None,
boxes=box_torch,
masks=None,
)
low_res_masks, iou_pred = self.mask_decoder(
image_embeddings=image_embedding, # (B, 256, 64, 64)
image_pe=self.prompt_encoder.get_dense_pe(), # (1, 256, 64, 64)
sparse_prompt_embeddings=sparse_embeddings, # (B, 2, 256)
dense_prompt_embeddings=dense_embeddings, # (B, 256, 64, 64)
multimask_output=True,
)
ori_res_masks = F.interpolate(
low_res_masks,
size=(image.shape[2], image.shape[3]),
mode="bilinear",
align_corners=False,
)
return ori_res_masks
def main():
os.makedirs(model_save_path, exist_ok=True)
shutil.copyfile(
__file__, join(model_save_path, run_id + "_" + os.path.basename(__file__))
)
sam_model = sam_model_registry[args.model_type](image_size=args.img_size,
num_classes=args.num_classes,
checkpoint=args.sam_checkpoint,
pixel_mean=[0, 0, 0],
pixel_std=[1, 1, 1])
medsam_model = MedSAM(
image_encoder= sam_model.image_encoder,
mask_decoder= sam_model.mask_decoder,
prompt_encoder= sam_model.prompt_encoder,
).to(device)
pretrained_model = torch.load(args.trained_model_path, map_location=device)
medsam_model.load_state_dict(pretrained_model["model"])
medsam_model.train()
print(
"Number of total parameters: ",
sum(p.numel() for p in medsam_model.parameters()),
) # 93735472
print(
"Number of trainable parameters: ",
sum(p.numel() for p in medsam_model.parameters() if p.requires_grad),
) # 93729252
img_mask_encdec_params = list(medsam_model.image_encoder.parameters()) + list(
medsam_model.mask_decoder.parameters()
)
optimizer = torch.optim.AdamW(
img_mask_encdec_params, lr=args.lr, weight_decay=args.weight_decay
)
print(
"Number of image encoder and mask decoder parameters: ",
sum(p.numel() for p in img_mask_encdec_params if p.requires_grad),
) # 93729252
# %% test
num_epochs = args.num_epochs
iter_num = 0
losses = []
seg_losses =[]
ce_losses =[]
dice_scores = [[] for _ in range(args.num_classes)]
best_loss = 1e10
import csv
# Define the path to the CSV file
csv_file_path = args.scans_test_save_path+'log_file.csv'
# Ensure the directory exists
os.makedirs(os.path.dirname(csv_file_path), exist_ok=True)
# Open the CSV file in write mode and write the header
# Define the number of classes
# Check if CSV file exists to decide if headers need to be written
file_exists = not os.path.exists(csv_file_path)
# Open the CSV file in append mode
with open(csv_file_path, 'a', newline='') as csvfile:
fieldnames = [
'Image Name', 'Seg Loss of Scan', 'CE Loss of Scan', 'Loss of Scan',
'Mean Dice Scores of Scan'
] + [f'Dice Score Class {i}' for i in range(args.num_classes+1)]
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
if file_exists:
writer.writeheader()
PER_CLASS_SEG_LOSS=[]
PER_CLASS_CE_LOSS=[]
PER_CLASS_LOSS=[]
PER_CLASS_DICE_SCORE = []
sample_list = [f for f in os.listdir(args.test_dir)]
for num, sample_name in enumerate(sample_list):
print("sample_name", sample_name)
test_sample_dir = os.path.join(args.test_dir, sample_name)
scan_dataset = NpyDataset(test_sample_dir, num_classes=4, bbox_pt_lst=[10, 10, 502, 502])
test_dataloader = DataLoader(
scan_dataset,
batch_size=args.batch_size,
num_workers=args.num_workers,
pin_memory=True,
shuffle=False
)
if args.resume is not None:
if os.path.isfile(args.resume):
## Map model to be loaded to specified single GPU
checkpoint = torch.load(args.resume, map_location=device)
medsam_model.load_state_dict(checkpoint["model"])
optimizer.load_state_dict(checkpoint["optimizer"])
if args.use_amp:
scaler = torch.cuda.amp.GradScaler()
# Dice score segmentation loss.
seg_loss = monai.losses.DiceLoss(sigmoid=True, squared_pred=True, to_onehot_y=False, reduction="mean", include_background = True)
# cross entropy loss
ce_loss = nn.BCEWithLogitsLoss(reduction="mean")
dice_param = args.dice_param
scan_seg_loss=0
scan_ce_loss=0
net_loss=0
list_perclass_dice_scores_batch=[]
mean_dice_scores =[]
for step, (image, gt2D, boxes, img_name) in enumerate(tqdm(test_dataloader)):
print("img name:", img_name)
optimizer.zero_grad()
boxes_np = boxes.detach().cpu().numpy()
image, gt2D = image.to(device), gt2D.to(device)
# Compute Dice scores
with torch.no_grad():
if args.use_amp:
## AMP - not using for now
with torch.autocast(device_type=device, dtype=torch.float32):
medsam_pred = medsam_model(image, boxes_np)
loss = dice_param*seg_loss(medsam_pred, gt2D) \
+ (1-dice_param)*ce_loss(medsam_pred, gt2D.float())
else:
medsam_pred = medsam_model(image, boxes_np)
#print("medsam pred shape", medsam_pred.shape)
gt2D=gt2D.squeeze(dim=1) # becomes [B,H,W]
# Convert ground truth to one-hot format
gt2D_one_hot = F.one_hot(gt2D, num_classes=args.num_classes + 1).permute(0, 3, 1, 2).float()
# Compute Dice loss for all classes
segmentation_loss = seg_loss(medsam_pred, gt2D_one_hot)
# Cross entropy on all classes.
cross_entropy_loss = ce_loss(medsam_pred, gt2D_one_hot)
loss = dice_param * segmentation_loss + (1-dice_param)*cross_entropy_loss
pred_probs = torch.sigmoid(medsam_pred) # Get probabilities
dice_scores_batch, mean_dice_score = dice_score(pred_probs, gt2D, bg=True)
for cls in range(args.num_classes):
dice_scores[cls].append(dice_scores_batch[cls].item()) # Store individual Dice score for the batch
mean_dice_scores.append(mean_dice_score.item()) # Store average Dice score for the batch
list_perclass_dice_scores_batch.append(dice_scores_batch.detach().cpu().numpy())
print("dice_scores_batch", dice_scores_batch)
# concatenate numpy predictions for each slice
# Get argmax predictions
pred_class = torch.argmax(pred_probs, dim=1) # [B, H, W] or [B, D, H, W] depending on the network output
# Convert predictions to numpy arrays and concatenate slices
pred_class_np = pred_class.cpu().numpy()
if step == 0:
all_preds = pred_class_np # Initialize with first batch
else:
all_preds = np.concatenate((all_preds, pred_class_np), axis=0) # Concatenate along the batch dimension
scan_seg_loss += segmentation_loss.item()
scan_ce_loss += cross_entropy_loss.item()
net_loss += loss.item()
scan_per_class_dice_Score = mean_excluding_below_threshold_axis(list_perclass_dice_scores_batch, 0.01, axis=0)
PER_CLASS_SEG_LOSS.append(scan_seg_loss)
PER_CLASS_CE_LOSS.append(scan_ce_loss)
PER_CLASS_LOSS.append(net_loss)
PER_CLASS_DICE_SCORE.append(scan_per_class_dice_Score)
print(
f'Image Name: {img_name}, Seg Loss of scan: {scan_seg_loss}, CE_Loss of scan: {scan_ce_loss}, Loss of scan: {loss}, 'f'mean_dice_scores of scan: {np.mean(mean_dice_scores)}, 'f'Dice Score each class of scan: {scan_per_class_dice_Score}'
)
csv_entry = {
'Image Name': img_name,
'Seg Loss of Scan': scan_seg_loss,
'CE Loss of Scan': scan_ce_loss,
'Loss of Scan': loss,
'Mean Dice Scores of Scan': np.mean(mean_dice_scores),
}
# Add Dice scores for each class to the CSV entry
for i in range(args.num_classes+1):
csv_entry[f'Dice Score Class {i}'] = scan_per_class_dice_Score[i]
# Save to CSV file
with open(csv_file_path, 'a', newline='') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writerow(csv_entry)
os.makedirs(args.scans_test_save_path, exist_ok=True)
# Convert to NIfTI format
# Open the corresponding original scan and the gt truth
org_data_dir=args.org_data_dir
# load scan
org_Scan = nib.load(os.path.join(org_data_dir, sample_name+'.nii.gz'))
# Extract the image data and affine matrix
img_data = org_Scan.get_fdata() # or nifti_image.get_data() in older nibabel versions
header=org_Scan.header
affine = org_Scan.affine
img_shape = img_data.shape
all_preds = np.transpose(all_preds, (1, 2, 0))
print("Shape of all preds", all_preds.shape)
# Compute the zoom factors for height and width
zoom_factors = np.array(img_shape[:2]) / np.array(all_preds.shape[:2])
# Resize predictions
resized_preds = np.zeros((*img_shape[:2], all_preds.shape[2]))
for i in range(all_preds.shape[2]):
resized_preds[:, :, i] = zoom(all_preds[:, :, i], zoom_factors, order=1) # 'order' defines interpolation method
# Get original data type
original_dtype = img_data.dtype # Get the original data type
nifti_img = nib.Nifti1Image(resized_preds, affine, header=header) # np.eye(4) is a dummy affine matrix
nifti_img.set_data_dtype(original_dtype) # Ensure the data type is set to the original type
nifti_img.to_filename(args.scans_test_save_path+f"Segmentation_{sample_name}.nii.gz")
print(f"Saved volume {num} as Segmentation_{sample_name}.nii.gz")
if __name__ == "__main__":
main()
# %%