-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_no_npz.py
530 lines (452 loc) · 17.5 KB
/
train_no_npz.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
import argparse
import os
import json
from datetime import datetime
import pandas as pd
import numpy as np
import monai
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader
import torchvision.transforms.functional as TF
from typing import Any, Iterable
from tqdm import tqdm
from sklearn.model_selection import KFold, train_test_split
from torch.utils.tensorboard import SummaryWriter
from segment_anything import sam_model_registry
from segment_anything.utils.transforms import ResizeLongestSide
from utils.dataset import MedSamDataset
def dice_score(preds, targets):
smooth = 1.0
assert preds.size() == targets.size()
iflat = preds.contiguous().view(-1)
tflat = targets.contiguous().view(-1)
intersection = (iflat * tflat).sum()
dice = (2.0 * intersection + smooth) / (iflat.sum() + tflat.sum() + smooth)
return dice
class TrainMedSam:
BEST_VAL_LOSS = float("inf")
BEST_EPOCH = 0
def __init__(
self,
lr: float = 3e-4,
batch_size: int = 4,
epochs: int = 100,
device: str = "cuda:0",
model_type: str = "vit_b",
image_dir="data/image_dir",
mask_dir="data/image_dir",
checkpoint: str = "work_dir/SAM/sam_vit_b_01ec64.pth",
):
self.lr = lr
self.batch_size = batch_size
self.epochs = epochs
self.device = device
self.image_dir = image_dir
self.mask_dir = mask_dir
self.sam_checkpoint_dir = checkpoint
self.model_type = model_type
def __call__(self, train_df, test_df, val_df, image_col, mask_col):
"""Entry method
prepare `dataset` and `dataloader` objects
"""
train_ds = MedSamDataset(
train_df,
image_col,
mask_col,
self.image_dir,
self.mask_dir,
)
val_ds = MedSamDataset(
val_df,
image_col,
mask_col,
self.image_dir,
self.mask_dir,
)
test_ds = MedSamDataset(
test_df,
image_col,
mask_col,
self.image_dir,
self.mask_dir,
)
# Define dataloaders
train_loader = DataLoader(
dataset=train_ds, batch_size=self.batch_size, shuffle=True
)
val_loader = DataLoader(
dataset=val_ds, batch_size=self.batch_size, shuffle=False
)
test_loader = DataLoader(
dataset=test_ds, batch_size=self.batch_size, shuffle=False
)
# get the model
model = self.get_model()
model.to(self.device)
# Train and evaluate model
self.train(model, train_loader, val_loader)
# Evaluate model on test data
loss, dice_score = self.test(model, test_loader, desc="Testing")
del model
torch.cuda.empty_cache()
self.BEST_EPOCH = 0
self.BEST_VAL_LOSS = float("inf")
return dice_score
def get_model(self):
sam_model = sam_model_registry[self.model_type](
checkpoint=self.sam_checkpoint_dir
).to(self.device)
return sam_model
@torch.inference_mode()
def evaluate(self, model, val_loader, desc="Validating") -> float:
"""Perform evaluation on test or validation data
Args:
model (_type_): pytorch model
val_loader (_type_): dataloader
desc (str, optional): _description_. Defaults to "Validating".
Returns:
np.array: (mean validation loss, mean validation dice)
"""
seg_loss = monai.losses.DiceCELoss(
sigmoid=True, squared_pred=True, reduction="mean"
)
progress_bar = tqdm(val_loader, total=len(val_loader))
val_loss = []
val_dice = []
for image, mask, bbox in progress_bar:
image = image.to(self.device)
mask = mask.to(self.device)
# resize image to 1024 by 1024
image = TF.resize(image, (1024, 1024), antialias=True)
H, W = mask.shape[-2], mask.shape[-1]
sam_trans = ResizeLongestSide(model.image_encoder.img_size)
box = sam_trans.apply_boxes(bbox, (H, W))
box_tensor = torch.as_tensor(box, dtype=torch.float, device=self.device)
# Get predictioin mask
image_embeddings = model.image_encoder(image) # (B,256,64,64)
sparse_embeddings, dense_embeddings = model.prompt_encoder(
points=None,
boxes=box_tensor,
masks=None,
)
mask_predictions, _ = model.mask_decoder(
image_embeddings=image_embeddings.to(self.device), # (B, 256, 64, 64)
image_pe=model.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,
)
mask_predictions = (mask_predictions > 0.5).float()
# get the dice loss
loss = seg_loss(mask_predictions, mask)
dice = dice_score(mask_predictions, mask)
val_loss.append(loss.detach().item())
val_dice.append(dice.detach().item())
# Update the progress bar
progress_bar.set_description(desc)
progress_bar.set_postfix(
eval_loss=np.mean(val_loss), eval_dice=np.mean(val_dice)
)
progress_bar.update()
return np.mean(val_loss), np.mean(val_dice)
@torch.inference_mode()
def test(self, model, val_loader, desc="Testing") -> float:
"""Perform evaluation on test or validation data
Args:
model (_type_): pytorch model
val_loader (_type_): dataloader
desc (str, optional): _description_.
Returns:
float: mean validation loss
"""
seg_loss = monai.losses.DiceCELoss(
sigmoid=True, squared_pred=True, reduction="mean"
)
progress_bar = tqdm(val_loader, total=len(val_loader))
val_loss = []
dice_scores = []
for image, mask, bbox in progress_bar:
image = image.to(self.device)
mask = mask.to(self.device)
# resize image to 1024 by 1024
image = TF.resize(image, (1024, 1024), antialias=True)
H, W = mask.shape[-2], mask.shape[-1]
sam_trans = ResizeLongestSide(model.image_encoder.img_size)
box = sam_trans.apply_boxes(bbox, (H, W))
box_tensor = torch.as_tensor(box, dtype=torch.float, device=self.device)
# Get predictioin mask
image_embeddings = model.image_encoder(image) # (B,256,64,64)
sparse_embeddings, dense_embeddings = model.prompt_encoder(
points=None,
boxes=box_tensor,
masks=None,
)
mask_predictions, _ = model.mask_decoder(
image_embeddings=image_embeddings.to(self.device), # (B, 256, 64, 64)
image_pe=model.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,
)
# get the dice loss
loss = seg_loss(mask_predictions, mask)
mask_predictions = (mask_predictions > 0.5).float()
dice = dice_score(mask_predictions, mask)
val_loss.append(loss.item())
dice_scores.append(dice.detach().item())
# Update the progress bar
progress_bar.set_description(desc)
progress_bar.set_postfix(
eval_loss=np.mean(val_loss), eval_dice=np.mean(dice_scores)
)
progress_bar.update()
return np.mean(val_loss), np.mean(dice_scores)
def train(self, model, train_loader: Iterable, val_loader: Iterable, logg=True):
"""Train the model"""
sam_trans = ResizeLongestSide(model.image_encoder.img_size)
writer = SummaryWriter()
optimizer = optim.Adam(model.parameters(), lr=self.lr, weight_decay=0.001)
scheduler = optim.lr_scheduler.ReduceLROnPlateau(
optimizer, mode="min", patience=3, factor=0.01, verbose=True
)
seg_loss = monai.losses.DiceCELoss(
sigmoid=True, squared_pred=True, reduction="mean"
)
model.train()
for epoch in range(self.epochs):
epoch_loss = []
epoch_dice = []
progress_bar = tqdm(train_loader, total=len(train_loader))
for image, mask, bbox in progress_bar:
image = image.to(self.device)
mask = mask.to(self.device)
# resize image to 1024 by 1024
image = TF.resize(image, (1024, 1024), antialias=True)
H, W = mask.shape[-2], mask.shape[-1]
box = sam_trans.apply_boxes(bbox, (H, W))
box_tensor = torch.as_tensor(box, dtype=torch.float, device=self.device)
# Get predictioin mask
with torch.inference_mode():
image_embeddings = model.image_encoder(image) # (B,256,64,64)
sparse_embeddings, dense_embeddings = model.prompt_encoder(
points=None,
boxes=box_tensor,
masks=None,
)
mask_predictions, _ = model.mask_decoder(
image_embeddings=image_embeddings.to(
self.device
), # (B, 256, 64, 64)
image_pe=model.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,
)
# Calculate loss
loss = seg_loss(mask_predictions, mask)
mask_predictions = (mask_predictions > 0.5).float()
dice = dice_score(mask_predictions, mask)
epoch_loss.append(loss.detach().item())
epoch_dice.append(dice.detach().item())
# empty gradient
optimizer.zero_grad()
loss.backward()
optimizer.step()
progress_bar.set_description(f"Epoch {epoch+1}/{self.epochs}")
progress_bar.set_postfix(
loss=np.mean(epoch_loss), dice=np.mean(epoch_dice)
)
progress_bar.update()
# Evaluate every two epochs
if epoch % 2 == 0:
validation_loss, validation_dice = self.evaluate(
model, val_loader, desc=f"Validating"
)
scheduler.step(torch.tensor(validation_loss))
if self.early_stopping(model, validation_loss, epoch):
print(f"[INFO:] Early Stopping!!")
break
if logg:
writer.add_scalars(
"loss",
{
"train": round(np.mean(epoch_loss), 4),
"val": round(validation_loss, 4),
},
epoch,
)
writer.add_scalars(
"dice",
{
"train": round(np.mean(epoch_dice), 4),
"val": round(validation_dice, 4),
},
epoch,
)
def save_model(self, model):
date_postfix = datetime.now().strftime("%Y-%m-%d-%H-%S")
model_name = f"medsam_finetune_{date_postfix}.pth"
save_path = "finetune_weights"
if not os.path.exists(save_path):
os.mkdir(save_path)
print(f"[INFO:] Saving model to {os.path.join(save_path,model_name)}")
torch.save(model.state_dict(), os.path.join(save_path, model_name))
def early_stopping(
self,
model,
val_loss: float,
epoch: int,
patience: int = 10,
min_delta: int = 0.001,
):
"""Helper function for model training early stopping
Args:
val_loss (float): _description_
epoch (int): _description_
patience (int, optional): _description_. Defaults to 10.
min_delta (int, optional): _description_. Defaults to 0.01.
"""
if self.BEST_VAL_LOSS - val_loss >= min_delta:
print(
f"[INFO:] Validation loss improved from {self.BEST_VAL_LOSS} to {val_loss}"
)
self.BEST_VAL_LOSS = val_loss
self.BEST_EPOCH = epoch
self.save_model(model)
return False
if (
self.BEST_VAL_LOSS - val_loss < min_delta
and epoch - self.BEST_EPOCH >= patience
):
return True
return False
class CrossValidate(TrainMedSam):
def __init__(
self,
lr: float = 3e-4,
batch_size: int = 4,
epochs: int = 100,
device: str = "cuda:0",
model_type: str = "vit_b",
image_dir="data/image_dir",
mask_dir="data/image_dir",
checkpoint: str = "work_dir/SAM/sam_vit_b_01ec64.pth",
):
super().__init__(
lr=lr,
batch_size=batch_size,
epochs=epochs,
device=device,
model_type=model_type,
image_dir=image_dir,
mask_dir=mask_dir,
checkpoint=checkpoint,
)
def __call__(self, train_df, test_df, image_col, mask_col, k: int = 5) -> Any:
"""Performs kfold cross validation
Args:
k (int, optional): Fold size. Defaults to 5.
"""
# Define the cross-validation splitter
kf = KFold(n_splits=k, shuffle=True)
# loop over each fold
fold_scores = {}
for fold, (train_idx, val_idx) in enumerate(kf.split(train_df)):
print(f"Cross validating for fold {fold + 1}")
# Define training and validation sets for this fold
f_train_df = train_df.iloc[train_idx]
f_val_df = train_df.iloc[val_idx]
dice_score = super().__call__(
f_train_df, test_df, f_val_df, image_col, mask_col
)
fold_scores[f"fold_{fold + 1}_mean_dice"] = dice_score
return fold_scores
def main():
# set up parser
parser = argparse.ArgumentParser()
parser.add_argument("--csv", type=str, required=True, help="Path to the CSV file")
parser.add_argument(
"--image_col",
type=str,
default=None,
help="Name of the column on the dataframe that holds the image file names",
)
parser.add_argument(
"--mask_col",
type=str,
default=None,
help="the name of the column on the dataframe that holds the mask file names",
)
parser.add_argument(
"--image", type=str, required=False, help="Path to the input image directory"
)
parser.add_argument(
"--mask",
type=str,
required=False,
help="Path to the ground truth mask directory",
)
parser.add_argument(
"--num_epochs", type=int, required=False, default=100, help="number of epochs"
)
parser.add_argument(
"--lr", type=float, required=False, default=3e-4, help="learning rate"
)
parser.add_argument(
"--batch_size", type=int, required=False, default=4, help="batch size"
)
parser.add_argument(
"-k",
type=int,
default=None,
required=False,
help="Number of folds for cross validation",
)
parser.add_argument("--model_type", type=str, required="False", default="vit_b")
parser.add_argument(
"--checkpoint", type=str, required=True, help="Path to SAM checkpoint"
)
args = parser.parse_args()
try:
df = pd.read_csv(args.csv)
except FileNotFoundError:
print(f"{args.csv} does not exist")
# split the dataset into train and test set
train_df, test_df = train_test_split(df, train_size=0.8, random_state=2023)
# if `k` argument is specified, run the cross validation
if args.k:
print(f"[INFO] Starting {args.k} fold cross validation ....")
if args.k < 5:
raise ValueError("K should be a value greater than or equal to 5")
cross_validate = CrossValidate(
lr=args.lr,
batch_size=args.batch_size,
epochs=args.num_epochs,
image_dir=args.image,
mask_dir=args.mask,
checkpoint=args.checkpoint,
)
scores = cross_validate(
train_df, test_df, args.image_col, args.mask_col, args.k
)
# write cross-validation scores to file
with open("medsam.json", "w") as f:
json.dump(scores, f)
# if `k` is not specified, normal training mode
if not args.k:
print(f"[INFO] Starting training for {args.num_epochs} epochs ....")
# create a validation set
train_df, val_df = train_test_split(train_df, train_size=0.8, random_state=2023)
train = TrainMedSam(
lr=args.lr,
batch_size=args.batch_size,
epochs=args.num_epochs,
image_dir=args.image,
mask_dir=args.mask,
checkpoint=args.checkpoint,
)
train(train_df, test_df, val_df, args.image_col, args.mask_col)
if __name__ == "__main__":
main()