forked from automl/CVPR24-MedSAM-on-Laptop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinetune.py
473 lines (434 loc) · 17.6 KB
/
finetune.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
# %%
import os
import random
import monai
from os import listdir, makedirs
from os.path import join, exists, isfile, isdir, basename
from glob import glob
from tqdm import tqdm, trange
from copy import deepcopy
from time import time
import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import Dataset, DataLoader
from datetime import datetime
from efficientvit.sam_model_zoo import create_sam_model
import cv2
import torch.nn.functional as F
from carbontracker.tracker import CarbonTracker
import pandas as pd
from matplotlib import pyplot as plt
import argparse
# %%
parser = argparse.ArgumentParser()
parser.add_argument(
"--traincsv", type=str, default="datasplit/train.csv",
help="csv file containing all files in the training set"
)
parser.add_argument(
"--valcsv", type=str, default="datasplit/val.csv",
help="csv file containing all files in the validation set"
)
parser.add_argument(
"-pretrained_checkpoint", type=str, default="lite_medsam.pth",
help="Path to the pretrained Lite-MedSAM checkpoint."
)
parser.add_argument(
"-resume", type=str, default='work_dir_evit/base/medsam_lite_latest.pth',
help="Path to the checkpoint to continue training."
)
parser.add_argument(
"-work_dir", type=str, default="work_dir_evit/base",
help="Path to the working directory where checkpoints and logs will be saved."
)
parser.add_argument(
"-num_epochs", type=int, default=10,
help="Number of epochs to train."
)
parser.add_argument(
"-batch_size", type=int, default=4,
help="Batch size."
)
parser.add_argument(
"-num_workers", type=int, default=16,
help="Number of workers for dataloader."
)
parser.add_argument(
"-device", type=str, default="cuda",
help="Device to train on."
)
parser.add_argument(
"-bbox_shift", type=int, default=5,
help="Perturbation to bounding box coordinates during training."
)
parser.add_argument(
"-patience", type=int, default=7,
help="Early stopping patience"
)
parser.add_argument(
"-lr", type=float, default=0.00005,
help="Learning rate."
)
parser.add_argument(
"-weight_decay", type=float, default=0.01,
help="Weight decay."
)
parser.add_argument(
"-iou_loss_weight", type=float, default=1.0,
help="Weight of IoU loss."
)
parser.add_argument(
"-seg_loss_weight", type=float, default=1.0,
help="Weight of segmentation loss."
)
parser.add_argument(
"-ce_loss_weight", type=float, default=1.0,
help="Weight of cross entropy loss."
)
parser.add_argument(
"--sanity_check", action="store_true",
help="Whether to do sanity check for dataloading."
)
args = parser.parse_args()
# %%
work_dir = args.work_dir
medsam_lite_checkpoint = args.pretrained_checkpoint
num_epochs = args.num_epochs
batch_size = args.batch_size
num_workers = args.num_workers
device = args.device
bbox_shift = args.bbox_shift
lr = args.lr
weight_decay = args.weight_decay
iou_loss_weight = args.iou_loss_weight
seg_loss_weight = args.seg_loss_weight
ce_loss_weight = args.ce_loss_weight
do_sancheck = args.sanity_check
checkpoint = args.resume
traincsv = args.traincsv
valcsv = args.valcsv
patience = args.patience
makedirs(work_dir, exist_ok=True)
tracker = CarbonTracker(epochs=num_epochs)
# %%
torch.cuda.empty_cache()
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.45])], axis=0)
else:
color = np.array([251/255, 252/255, 30/255, 0.45])
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='blue', facecolor=(0,0,0,0), lw=2))
def cal_iou(result, reference):
intersection = torch.count_nonzero(torch.logical_and(result, reference), dim=[i for i in range(1, result.ndim)])
union = torch.count_nonzero(torch.logical_or(result, reference), dim=[i for i in range(1, result.ndim)])
iou = intersection.float() / union.float()
return iou.unsqueeze(1)
# %%
class NpzDataset(Dataset):
def __init__(self, filescsv, image_size=256, bbox_shift=5, data_aug=True):
files=pd.read_csv(filescsv)["file"].tolist()
print(len(files), "files in total")
self.file_paths = sorted(files)
self.image_size = image_size
self.target_length = image_size
self.bbox_shift = bbox_shift
self.data_aug = data_aug
def __len__(self):
return len(self.file_paths)
def __getitem__(self, index):
npz = np.load(self.file_paths[index], 'r', allow_pickle=True)
gts = npz["gts"] # multiple labels [0, 1,4,5...], (256,256)
imgs = npz["imgs"]
if len(gts.shape) > 2: ## 3D image
i=random.randint(0,gts.shape[0]-1)
img_i = imgs[i, :, :]
gt_i = gts[i, :, :]
img_i = self.resize_longest_side(img_i)
gt_i = cv2.resize(gt_i, (img_i.shape[1], img_i.shape[0]), interpolation=cv2.INTER_NEAREST)
img_i = self.pad_image(img_i)
gts = self.pad_image(gt_i)
img_3c = np.repeat(img_i[:, :, None], 3, axis=-1)# (H, W, 3)
else:
if len(imgs.shape) < 3:
img_3c = np.repeat(imgs[:, :, None], 3, axis=-1)
else:
img_3c = imgs
img_3c = self.resize_longest_side(img_3c)
gts = cv2.resize(gts, (img_3c.shape[1], img_3c.shape[0]), interpolation=cv2.INTER_NEAREST)
img_3c = self.pad_image(img_3c)
gts = self.pad_image(gts)
gts = np.uint16(gts)
img_resize = img_3c#self.resize_longest_side(img_3c)
# Resizing
img_resize = (img_resize - img_resize.min()) / np.clip(img_resize.max() - img_resize.min(), a_min=1e-8, a_max=None) # normalize to [0, 1], (H, W, 3
img_padded = img_resize#self.pad_image(img_resize) # (256, 256, 3)
# convert the shape to (3, H, W)
img_padded = np.transpose(img_padded, (2, 0, 1)) # (3, 256, 256)
assert np.max(img_padded)<=1.0 and np.min(img_padded)>=0.0, 'image should be normalized to [0, 1]'
label_ids = np.unique(gts)[1:]
try:
gt2D = np.uint8(gts == random.choice(label_ids.tolist())) # only one label, (256, 256)
except:
# print(self.file_paths[index], 'label_ids.tolist()', label_ids.tolist())
return self.__getitem__(random.randint(0,len(self)-1))
# add data augmentation: random fliplr and random flipud
if self.data_aug:
if random.random() > 0.5:
img_padded = np.ascontiguousarray(np.flip(img_padded, axis=-1))
gt2D = np.ascontiguousarray(np.flip(gt2D, axis=-1))
# print('DA with flip left right')
if random.random() > 0.5:
img_padded = np.ascontiguousarray(np.flip(img_padded, axis=-2))
gt2D = np.ascontiguousarray(np.flip(gt2D, axis=-2))
# print('DA with flip upside down')
gt2D = np.uint8(gt2D > 0)
y_indices, x_indices = np.where(gt2D > 0)
x_min, x_max = np.min(x_indices), np.max(x_indices)
y_min, y_max = np.min(y_indices), np.max(y_indices)
# 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-1, x_max + random.randint(0, self.bbox_shift))
y_min = max(0, y_min - random.randint(0, self.bbox_shift))
y_max = min(H-1, y_max + random.randint(0, self.bbox_shift))
bboxes = np.array([x_min, y_min, x_max, y_max])
#print(gt2D.shape)
gt2D=cv2.resize(gt2D,(256,256))#[None, :,:]
return {
"image": torch.tensor(img_padded).float(),
"gt2D": torch.tensor(gt2D[None, :,:]).long(),
"bboxes": torch.tensor(bboxes[None, None, ...]).float(), # (B, 1, 4)
"image_name": self.file_paths[index],
"new_size": torch.tensor(np.array([img_resize.shape[0], img_resize.shape[1]])).long(),
"original_size": torch.tensor(np.array([img_3c.shape[0], img_3c.shape[1]])).long()
}
def resize_longest_side(self, image):
"""
Expects a numpy array with shape HxWxC in uint8 format.
"""
long_side_length = self.target_length
oldh, oldw = image.shape[0], image.shape[1]
scale = long_side_length * 1.0 / max(oldh, oldw)
newh, neww = oldh * scale, oldw * scale
neww, newh = int(neww + 0.5), int(newh + 0.5)
target_size = (neww, newh)
return cv2.resize(image, target_size, interpolation=cv2.INTER_AREA)
def pad_image(self, image):
"""
Expects a numpy array with shape HxWxC in uint8 format.
"""
# Pad
h, w = image.shape[0], image.shape[1]
padh = self.image_size - h
padw = self.image_size - w
if len(image.shape) == 3: ## Pad image
image_padded = np.pad(image, ((0, padh), (0, padw), (0, 0)))
else: ## Pad gt mask
image_padded = np.pad(image, ((0, padh), (0, padw)))
return image_padded
medsam_lite_model = create_sam_model("l0", False)
medsam_lite_model.prompt_encoder.input_image_size=(256,256)
medsam_lite_model.to(device)
medsam_lite_model.eval()
if medsam_lite_checkpoint is not None:
if isfile(medsam_lite_checkpoint):
print(f"Finetuning with pretrained weights {medsam_lite_checkpoint}")
medsam_lite_ckpt = torch.load(
medsam_lite_checkpoint,
map_location="cpu"
)
medsam_lite_model.load_state_dict(medsam_lite_ckpt, strict=True)
else:
print(f"Pretained weights {medsam_lite_checkpoint} not found, training from scratch")
medsam_lite_model = nn.DataParallel(medsam_lite_model)
medsam_lite_model = medsam_lite_model.to(device)
medsam_lite_model.train()
# %%
print(f"MedSAM Lite size: {sum(p.numel() for p in medsam_lite_model.parameters())}")
# %%
optimizer = optim.AdamW(
medsam_lite_model.parameters(),
lr=lr,
betas=(0.9, 0.999),
eps=1e-08,
weight_decay=weight_decay,
)
lr_scheduler = optim.lr_scheduler.ReduceLROnPlateau(
optimizer,
mode='min',
factor=0.9,
patience=5,
cooldown=0
)
seg_loss = monai.losses.DiceLoss(sigmoid=True, squared_pred=True, reduction='mean')
ce_loss = nn.BCEWithLogitsLoss(reduction='mean')
iou_loss = nn.MSELoss(reduction='mean')
# %%
train_dataset = NpzDataset(filescsv=traincsv, data_aug=True)
train_loader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True, num_workers=num_workers, pin_memory=True)
val_dataset = NpzDataset(filescsv=valcsv, data_aug=True)
val_loader = DataLoader(val_dataset, batch_size=batch_size, shuffle=False, num_workers=num_workers, pin_memory=True)
seed=2024
if checkpoint and isfile(checkpoint):
print(f"Resuming from checkpoint {checkpoint}")
checkpoint = torch.load(checkpoint)
medsam_lite_model.load_state_dict(checkpoint["model"], strict=True)
optimizer.load_state_dict(checkpoint["optimizer"])
start_epoch = checkpoint["epoch"]
best_loss = checkpoint["best_loss"]
epochs_since_improvement = checkpoint["epochs_since_improvement"]
random.setstate(checkpoint["rng_state"])
np.random.set_state(checkpoint["np_rng_state"])
torch.set_rng_state(checkpoint["torch_rng_state"])
if torch.cuda.is_available():
torch.cuda.set_rng_state(checkpoint["torch_cuda_rng_state"])
print(f"Loaded checkpoint from epoch {start_epoch}")
else:
start_epoch = 0
best_loss = 1e10
epochs_since_improvement = 0
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
if torch.cuda.is_available():
torch.cuda.manual_seed(seed)
# %%
for parameter in medsam_lite_model.module.prompt_encoder.parameters():
parameter.requires_grad = False
train_losses = []
for epoch in range(start_epoch + 1, num_epochs+1):
tracker.epoch_start()
if epochs_since_improvement > patience:
print("Early Stopped")
break
epoch_loss = [1e10 for _ in range(len(train_loader))]
epoch_start_time = time()
pbar = tqdm(train_loader)
medsam_lite_model.train()
for step, batch in enumerate(pbar):
image = batch["image"]
gt2D = batch["gt2D"]
boxes = batch["bboxes"]
optimizer.zero_grad()
image, gt2D, boxes = image.to(device), gt2D.to(device), boxes.to(device)
image_embedding = medsam_lite_model.module.image_encoder(image) # (B, 256, 64, 64)
sparse_embeddings, dense_embeddings = medsam_lite_model.module.prompt_encoder(
points=None,
boxes=boxes,
masks=None,
)
logits_pred, iou_pred = medsam_lite_model.module.mask_decoder(
image_embeddings=image_embedding, # (B, 256, 64, 64)
image_pe=medsam_lite_model.module.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=False,
) # (B, 1, 256, 256)
#print(logits_pred.shape)
#logits_pred, iou_pred = medsam_lite_model(image, boxes)
l_seg = seg_loss(logits_pred, gt2D)
l_ce = ce_loss(logits_pred, gt2D.float())
#mask_loss = l_seg + l_ce
mask_loss = seg_loss_weight * l_seg + ce_loss_weight * l_ce
iou_gt = cal_iou(torch.sigmoid(logits_pred) > 0.5, gt2D.bool())
l_iou = iou_loss(iou_pred, iou_gt)
#loss = mask_loss + l_iou
loss = mask_loss + iou_loss_weight * l_iou
epoch_loss[step] = loss.item()
loss.backward()
optimizer.step()
optimizer.zero_grad()
pbar.set_description(f"Epoch {epoch} at {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}, loss: {loss.item():.4f}")
# validation loop deterministic to improve reproducibility
rng_state = random.getstate()
np_rng_state = np.random.get_state()
torch_rng_state = torch.get_rng_state()
if torch.cuda.is_available():
torch_cuda_rng_state = torch.cuda.get_rng_state()
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
if torch.cuda.is_available():
torch.cuda.manual_seed(seed)
val_loss = 0
medsam_lite_model.eval()
with torch.no_grad():
for step, batch in enumerate(val_loader):
image = batch["image"]
gt2D = batch["gt2D"]
boxes = batch["bboxes"]
image, gt2D, boxes = image.to(device), gt2D.to(device), boxes.to(device)
image_embedding = medsam_lite_model.module.image_encoder(image) # (B, 256, 64, 64)
sparse_embeddings, dense_embeddings = medsam_lite_model.module.prompt_encoder(
points=None,
boxes=boxes,
masks=None,
)
logits_pred, iou_pred = medsam_lite_model.module.mask_decoder(
image_embeddings=image_embedding, # (B, 256, 64, 64)
image_pe=medsam_lite_model.module.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=False,
)
l_seg = seg_loss(logits_pred, gt2D)
l_ce = ce_loss(logits_pred, gt2D.float())
mask_loss = seg_loss_weight * l_seg + ce_loss_weight * l_ce
iou_gt = cal_iou(torch.sigmoid(logits_pred) > 0.5, gt2D.bool())
l_iou = iou_loss(iou_pred, iou_gt)
loss = mask_loss + iou_loss_weight * l_iou
val_loss += loss.item()
print(val_loss)
# continue with old random states for training, to make sure that different boxes and slices of the same datapoint get selected
random.setstate(rng_state)
np.random.set_state(np_rng_state)
torch.set_rng_state(torch_rng_state)
if torch.cuda.is_available():
torch.cuda.set_rng_state(torch_cuda_rng_state)
epoch_end_time = time()
epoch_loss_reduced = sum(epoch_loss) / len(epoch_loss)
train_losses.append(epoch_loss_reduced)
lr_scheduler.step(epoch_loss_reduced)
model_weights = medsam_lite_model.state_dict()
epochs_since_improvement = 0 if val_loss < best_loss else epochs_since_improvement + 1
checkpoint = {
"model": model_weights,
"epoch": epoch,
"optimizer": optimizer.state_dict(),
"loss": epoch_loss_reduced,
"best_loss": val_loss if val_loss < best_loss else best_loss,
"epochs_since_improvement": epochs_since_improvement,
"rng_state": random.getstate(),
"np_rng_state": np.random.get_state(),
"torch_rng_state": torch.get_rng_state(),
}
if torch.cuda.is_available():
checkpoint["torch_cuda_rng_state"] = torch.cuda.get_rng_state()
torch.save(checkpoint, join(work_dir, "medsam_lite_latest.pth"))
if val_loss < best_loss:
print(f"New best loss in epoch {epoch}: {best_loss:.5f} -> {val_loss:.5f}")
best_loss = val_loss
checkpoint["best_loss"] = best_loss
torch.save(checkpoint, join(work_dir, "medsam_lite_best.pth"))
# %% plot loss
plt.plot(train_losses)
plt.title("Dice + Binary Cross Entropy + IoU Loss")
plt.xlabel("Epoch")
plt.ylabel("Loss")
plt.savefig(join(work_dir, "train_loss.png"))
plt.close()
tracker.epoch_end()
tracker.stop()