-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain.py
More file actions
391 lines (311 loc) · 13.4 KB
/
Copy pathtrain.py
File metadata and controls
391 lines (311 loc) · 13.4 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
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
"""
Main training script for U-Net segmentation models.
Organized for clarity, modularity, and robust logging.
"""
import argparse
import os
import sys
from matplotlib import ticker
import yaml
import torch
from torch.utils.data import DataLoader
import pandas as pd
import matplotlib.pyplot as plt
from datetime import datetime
from thop import profile
from tqdm import tqdm
import math
import numpy as np
import wandb
from utils import logger
# Add src and networks to path
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
import networks
import datasets
import networks
import utils
run = None # Global variable for wandb run
def train_model(config: dict, run):
"""
Returns:
tuple: A tuple containing:
- list: A list of dictionaries with training/validation metrics per epoch.
- str: The file path to the best performing model checkpoint.
"""
# Setup device
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(f"Using device: {device}")
# Create model
# [TODO] Need to be modified in future for flexibility
model = getattr(networks, config['model_type'])(
in_channels=config['input_channels'],
out_channels=config['output_channels'],
channels=config['channels']
)
model.to(device)
# Create data loaders
train_loader, val_loader = datasets.create_split_loaders(
dataset=config['dataset'],
root_dir=config['data_dir'],
image_size=tuple(config['image_size']),
batch_size=config['batch_size'],
val_split=config['val_split']
)
# [TODO] Add support for different optimizers and loss functions
# Optimizer, loss function, and AMP scaler
lr = init_lr = config.get('init_lr')
lr_rampdown_epochs = config.get('lr_rampdown_epochs', config.get('lr_rampdown_epochs'))
optimizer_type = config.get('optimizer')
optimizer = getattr(torch.optim, optimizer_type)(
model.parameters(),
lr=init_lr
)
loss_function_type = config.get('loss')
criterion = getattr(torch.nn, loss_function_type)()
scaler = torch.amp.GradScaler(enabled=config.get('use_amp'))
# Training loop
history = []
best_val_dice = 0.0
best_model_path = None
best_epoch = 0
total_epochs = config.get('epochs')
for epoch in range(total_epochs):
# --- Training Phase ---
model.train()
train_loss_epoch = 0.0
# --- Main Training Loop ---
# train_loss_epoch = train_loop(train_loader, model, criterion, optimizer, device, epoch, lr_rampdown_epochs, scaler, total_epochs, lr)
for x, y in tqdm(train_loader, desc=f"Epoch {epoch+1}/{total_epochs} [T]", leave=False):
x, y = x.to(device), y.to(device)
optimizer.zero_grad()
with torch.amp.autocast(device_type='cuda', enabled=config.get('use_amp', False)):
# Mixed precision training
y_pred = model(x)
loss = criterion(y_pred, y)
scaler.scale(loss).backward()
lr *= utils.cosine_rampdown(epoch, lr_rampdown_epochs)
scaler.step(optimizer)
scaler.update()
for param_group in optimizer.param_groups:
param_group['lr'] = lr
train_loss_epoch = loss.item()
avg_train_loss = train_loss_epoch / len(train_loader)
# --- Validation Phase ---
model.eval()
val_loss_epoch = 0.0
val_dice_epoch = 0.0
with torch.no_grad():
for x, y in tqdm(val_loader, desc=f"Epoch {epoch+1}/{config['epochs']} [V]", leave=False):
x, y = x.to(device), y.to(device)
outputs = model(x)
loss = criterion(outputs, y)
dice = utils.dice_coefficient(outputs, y)
val_loss_epoch += loss.item()
val_dice_epoch += dice.item()
avg_val_loss = val_loss_epoch / len(val_loader)
avg_val_dice = val_dice_epoch / len(val_loader)
print(
f"Epoch {epoch+1}/{config['epochs']} -> "
f"Train Loss: {avg_train_loss:.4f}, "
f"Val Loss: {avg_val_loss:.4f}, "
f"Val Dice: {avg_val_dice:.4f}, "
f"Learning Rate: {lr:.6f}"
)
# Log metrics
history.append({
'epoch': epoch + 1,
'train_loss': avg_train_loss,
'val_loss': avg_val_loss,
'val_dice': avg_val_dice
})
run.log({
'train_loss': avg_train_loss,
'val_loss': avg_val_loss,
'val_dice': avg_val_dice
})
min_delta = config.get('min_improvement', 0.001)
save_after_epochs = config.get('save_after_epochs', 1)
# Save best model
if epoch > save_after_epochs and avg_val_dice >= best_val_dice + min_delta:
best_val_dice = avg_val_dice
best_epoch = epoch
os.makedirs(config['save_dir'], exist_ok=True)
best_model_path = os.path.join(config['save_dir'], f"{config['model_type']}_best.pth")
torch.save(model.state_dict(), best_model_path)
print(f"-> New best model saved to {best_model_path} (Dice: {best_val_dice:.4f})")
# Wait for early convergence
patience = config.get('patience', 15) # Number of epochs to wait without improvement
if epoch > save_after_epochs and epoch >= best_epoch + patience and avg_val_dice < best_val_dice + min_delta:
print(f"Early stopping at epoch {epoch + 1}. No improvement for {patience} epochs since epoch {best_epoch + 1}.")
print(f"Best dice: {best_val_dice:.4f} at epoch {best_epoch + 1}, Current dice: {avg_val_dice:.4f}")
break
# save the last epoch model
os.makedirs(config['save_dir'], exist_ok=True)
last_model_path = os.path.join(config['save_dir'], f"{config['model_type']}_last.pth")
torch.save(model.state_dict(), last_model_path)
print(f"Last model saved to {last_model_path}")
return history, best_model_path, last_model_path
def train_loop(train_loader, model, criterion, optimizer, device, epoch, lr_rampdown_epochs, scaler, total_epochs, lr):
for x, y in tqdm(train_loader, desc=f"Epoch {epoch+1}/{total_epochs} [T]", leave=False):
x, y = x.to(device), y.to(device)
optimizer.zero_grad()
# Mixed precision training
y_pred = model(x)
loss = criterion(y_pred, y)
scaler.scale(loss).backward()
lr *= utils.cosine_rampdown(epoch, lr_rampdown_epochs)
scaler.step(optimizer)
scaler.update()
for param_group in optimizer.param_groups:
param_group['lr'] = lr
return loss.item()
# def mean_teacher(config: dict, run):
# # Setup device
# device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# print(f"Using device: {device}")
# # Create model
# # [TODO] Need to be modified in future for flexibility
# model_source = config['model_source']
# model_type = config['model_type']
# module = getattr(networks, model_source)
# student = getattr(module, model_type)(
# in_channels=config['input_channels'],
# out_channels=config['output_channels'],
# channels=config['channels'])
# student.to(device)
# teacher = getattr(module, model_type)(
# in_channels=config['input_channels'],
# out_channels=config['output_channels'],
# channels=config['channels'])
# teacher.to(device)
# # Create data loaders
# labeled_train_loader, unlabeled_train_loader, val_loader = data_utils.create_split_loaders_semi_supervised(
# dataset=config['dataset'],
# root_dir=config['data_dir'],
# image_size=(config['image_height'], config['image_width']),
# batch_size=config['batch_size'],
# label_split=config.get('label_split', 0.2),
# val_split=config.get('val_split', 0.2)
# )
# # [TODO] Add support for different optimizers and loss functions
# # Optimizer, loss function, and AMP scaler
# lr = config.get('lr')
# lr_rampdown_epochs = config.get('lr_rampdown_epochs', total_epochs)
# optimizer_type = config.get('optimizer')
# optimizer = getattr(torch.optim, optimizer_type)(
# student.parameters(),
# lr=lr,
# weight_decay=config.get('weight_decay', None),
# betas=config.get('betas', None)
# )
# loss_function_type = config.get('loss')
# criterion = getattr(torch.nn, loss_function_type)()
# scaler = torch.cuda.amp.GradScaler(enabled=config.get('use_amp', False))
# # Training loop
# history = []
# best_val_dice = 0.0
# best_model_path = None
# best_epoch = 0
# total_epochs = config.get('epochs')
# for epoch in range(total_epochs):
# # --- Training Phase ---
# student.train()
# train_loss_epoch = 0.0
# # --- Main Training Loop ---
# for x, y in tqdm(train_loader, desc=f"Epoch {epoch+1}/{total_epochs} [T]", leave=False):
# x, y = x.to(device), y.to(device)
# optimizer.zero_grad()
# # Mixed precision training
# y_pred = model(x)
# loss = criterion(y_pred, y)
# scaler.scale(loss).backward()
# lr *= rampups.cosine_rampdown(epoch, lr_rampdown_epochs)
# scaler.step(optimizer)
# scaler.update()
# for param_group in optimizer.param_groups:
# param_group['lr'] = lr
# avg_train_loss = train_loss_epoch / len(train_loader)
# # --- Validation Phase ---
# model.eval()
# val_loss_epoch = 0.0
# val_dice_epoch = 0.0
# with torch.no_grad():
# for x, y in tqdm(val_loader, desc=f"Epoch {epoch+1}/{config['epochs']} [V]", leave=False):
# x, y = x.to(device), y.to(device)
# outputs = model(x)
# loss = criterion(outputs, y)
# dice = metrics.dice_coefficient(outputs, y)
# val_loss_epoch += loss.item()
# val_dice_epoch += dice.item()
# avg_val_loss = val_loss_epoch / len(val_loader)
# avg_val_dice = val_dice_epoch / len(val_loader)
# print(
# f"Epoch {epoch+1}/{config['epochs']} -> "
# f"Train Loss: {avg_train_loss:.4f}, "
# f"Val Loss: {avg_val_loss:.4f}, "
# f"Val Dice: {avg_val_dice:.4f}, "
# f"Learning Rate: {lr:.6f}"
# )
# # Log metrics
# history.append({
# 'epoch': epoch + 1,
# 'train_loss': avg_train_loss,
# 'val_loss': avg_val_loss,
# 'val_dice': avg_val_dice
# })
# run.log({
# 'train_loss': avg_train_loss,
# 'val_loss': avg_val_loss,
# 'val_dice': avg_val_dice
# })
# min_delta = config.get('min_improvement', 0.001)
# save_after_epochs = config.get('save_after_epochs', 1)
# # Save best model
# if epoch > save_after_epochs and avg_val_dice >= best_val_dice + min_delta:
# best_val_dice = avg_val_dice
# best_epoch = epoch
# os.makedirs(config['save_dir'], exist_ok=True)
# best_model_path = os.path.join(config['save_dir'], f"{config['model_type']}_best.pth")
# torch.save(model.state_dict(), best_model_path)
# print(f"-> New best model saved to {best_model_path} (Dice: {best_val_dice:.4f})")
# # Wait for early convergence
# patience = config.get('patience', 15) # Number of epochs to wait without improvement
# if epoch > save_after_epochs and epoch >= best_epoch + patience and avg_val_dice < best_val_dice + min_delta:
# print(f"Early stopping at epoch {epoch + 1}. No improvement for {patience} epochs since epoch {best_epoch + 1}.")
# print(f"Best dice: {best_val_dice:.4f} at epoch {best_epoch + 1}, Current dice: {avg_val_dice:.4f}")
# break
# # save the last epoch model
# os.makedirs(config['save_dir'], exist_ok=True)
# last_model_path = os.path.join(config['save_dir'], f"{config['model_type']}_last.pth")
# torch.save(model.state_dict(), last_model_path)
# print(f"Last model saved to {last_model_path}")
# return history, best_model_path, last_model_path
def main():
"""
Main entry point for the training script.
"""
parser = argparse.ArgumentParser(description="Train U-Net model.")
parser.add_argument('--config', type=str, default='hyper.yaml', help='Path to config YAML file.')
args = parser.parse_args()
with open(args.config, 'r') as f:
config = yaml.safe_load(f)
print("--- Training Configuration ---")
print(yaml.dump(config, sort_keys=False))
print("-----------------------------")
run = wandb.init(
dir="/wandb",
project=config.get('wandb_project'),
entity=config.get('wandb_entity'),
config=config,
name=f"{config['model_type']}_{datetime.now().strftime('%m%d_%H%M')}",
mode=config.get('wandb_mode', 'offline')
)
history, best_model_path, last_model_path = train_model(config, run)
run.finish()
if history:
logger.log_results(config, history, best_model_path, last_model_path)
print("--- Training and Logging Completed ---")
else:
print("--- Training did not produce results to log ---")
if __name__ == "__main__":
main()