Repository navigation
Expand file tree
/
Copy pathtrain.py
More file actions
155 lines (120 loc) · 5.06 KB
/
Copy pathtrain.py
File metadata and controls
155 lines (120 loc) · 5.06 KB
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
import torch
import monai
from tqdm import tqdm
from statistics import mean
from torch.utils.data import Dataset, DataLoader
from torch.optim import Adam
from torchvision.utils import save_image
import src.utils as utils
from src.dataloader import DatasetSegmentation, collate_fn
from src.processor import Samprocessor
from src.segment_anything import build_sam_vit_b, SamPredictor
from src.lora import LoRA_sam
import matplotlib.pyplot as plt
import yaml
import torch.nn.functional as F
import numpy as np
from sklearn.metrics import jaccard_score, accuracy_score
"""
This file is used to train a LoRA_sam model. We use the monai DiceLoss for the training. The batch size and number of epochs are taken from the configuration file.
The model is saved at the end as a safetensor.
"""
# Load the config file
with open("./config.yaml", "r") as ymlfile:
config_file = yaml.load(ymlfile, Loader=yaml.Loader)
# Take dataset path
train_dataset_path = config_file["DATASET"]["TRAIN_PATH"]
# Load SAM model
sam = build_sam_vit_b(checkpoint=config_file["SAM"]["CHECKPOINT"])
# Create SAM LoRA
sam_lora = LoRA_sam(sam, config_file["SAM"]["RANK"])
model = sam_lora.sam
# Process the dataset
processor = Samprocessor(model)
train_ds = DatasetSegmentation(config_file, processor, mode="train")
# Create a dataloader
train_dataloader = DataLoader(train_ds, batch_size=config_file["TRAIN"]["BATCH_SIZE"], shuffle=True,
collate_fn=collate_fn)
# Initialize optimizer and Loss
optimizer = Adam(model.image_encoder.parameters(), lr=1e-4, weight_decay=0)
seg_loss = monai.losses.DiceCELoss(sigmoid=True, squared_pred=True, reduction='mean')
num_epochs = config_file["TRAIN"]["NUM_EPOCHS"]
device = "cuda" if torch.cuda.is_available() else "cpu"
# Set model to train and into the device
model.train()
model.to(device)
total_loss = []
# Compute development mean IoU and pixel accuracy (PA).
def calculate_metrics(pred_mask, true_mask):
pred_mask = pred_mask.detach().cpu().numpy()
true_mask = true_mask.detach().cpu().numpy()
pred_mask = (pred_mask > 0.5).astype(int)
true_mask = true_mask.astype(int)
# Flatten to calculate metrics
pred_mask = pred_mask.flatten()
true_mask = true_mask.flatten()
miou = jaccard_score(true_mask, pred_mask, average='macro')
pa = accuracy_score(true_mask, pred_mask)
return miou, pa
# Evaluate the current model on the development validation set
def evaluate_model(model, config_file):
test_ds = DatasetSegmentation(config_file, processor, mode="test")
test_dataloader = DataLoader(test_ds, batch_size=config_file["TRAIN"]["BATCH_SIZE"], shuffle=False,
collate_fn=collate_fn)
was_training = model.training
model.eval()
miou_scores = []
pa_scores = []
with torch.no_grad():
for batch in test_dataloader:
outputs = model(batched_input=batch, multimask_output=False)
stk_gt, stk_out = utils.stacking_batch(batch, outputs)
stk_out = stk_out.squeeze(1)
stk_gt = stk_gt.unsqueeze(1) # We need to get the [B, C, H, W] starting from [H, W]
miou, pa = calculate_metrics(stk_out, stk_gt)
miou_scores.append(miou)
pa_scores.append(pa)
avg_miou = np.mean(miou_scores)
avg_pa = np.mean(pa_scores)
model.train(was_training)
return avg_miou, avg_pa
# ====== Track the best development metrics ======
best_miou = 0.0
best_epoch = -1
best_pa = 0.0
for epoch in range(num_epochs):
model.train()
epoch_losses = []
for i, batch in enumerate(tqdm(train_dataloader)):
outputs = model(batched_input=batch,
multimask_output=False)
stk_gt, stk_out = utils.stacking_batch(batch, outputs)
stk_out = stk_out.squeeze(1)
stk_gt = stk_gt.unsqueeze(1) # [B, C, H, W]
loss = seg_loss(stk_out, stk_gt.float().to(device))
optimizer.zero_grad()
loss.backward()
optimizer.step()
epoch_losses.append(loss.item())
print(f'EPOCH: {epoch}')
print(f'Mean loss training: {mean(epoch_losses)}')
# Evaluate development metrics every three epochs
if epoch >= 14 and (epoch + 1) % 3 == 0:
avg_miou, avg_pa = evaluate_model(model, config_file)
# Update the best development metrics
if avg_miou > best_miou:
best_miou = avg_miou
best_pa = avg_pa
best_epoch = epoch + 1
# Save the best checkpoint
rank = config_file["SAM"]["RANK"]
sam_lora.save_lora_parameters(f"lora_rank{rank}_best.safetensors")
if avg_miou > 0.87:
rank = config_file["SAM"]["RANK"]
sam_lora.save_lora_parameters(f"lora_rank{rank}_{epoch + 1}.safetensors")
print(f'EPOCH: {epoch + 1}, MIOU: {avg_miou}, PA: {avg_pa}')
# Save the parameters of the model in safetensors format (final checkpoint)
rank = config_file["SAM"]["RANK"]
sam_lora.save_lora_parameters(f"lora_rank{rank}_final.safetensors")
# Print the best development result
print(f"Best Epoch: {best_epoch}, Best MIOU: {best_miou}, Best PA: {best_pa}")