forked from rayanirban/BlastoSeg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
181 lines (157 loc) · 6.01 KB
/
train.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
"""Definition of the training and validation loops"""
import torch
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter
import torch.nn as nn
import torchvision.transforms.v2 as transforms_v2
import numpy as np
#device = torch.device("cuda") if torch.cuda.is_available() else
device = torch.device("cpu")
assert torch.cuda.is_available()
from model import UNet
from dataset import (
BlastoDataset
)
# Create training dataset
train_data = BlastoDataset("/group/dl4miacourse/projects/BlastoSeg/training")
train_loader = DataLoader(train_data, batch_size=1, shuffle=True, num_workers=8)
val_data = BlastoDataset("/group/dl4miacourse/projects/BlastoSeg/validation")
val_loader = DataLoader(val_data, batch_size = 1, shuffle=True, num_workers=8)
unet = UNet(depth=4, in_channels=1, out_channels=4, num_fmaps=2).to(device)
optimizer = torch.optim.Adam(unet.parameters())
def train(
model,
loader,
optimizer,
loss_function,
epoch,
log_interval=100,
log_image_interval=20,
tb_logger=None,
device=None,
early_stop=False,
batchsize = 5
):
if device is None:
# You can pass in a device or we will default to using
# the gpu. Feel free to try training on the cpu to see
# what sort of performance difference there is
#if torch.cuda.is_available():
# device = torch.device("cuda")
#else:
device = torch.device("cpu")
# set the model to train mode
model.train()
# move model to device
model = model.to(device)
# iterate over the batches of this epoch
for batch_id, (x_batch, y_batch) in enumerate(loader):
# move input and target to the active device (either cpu or gpu)
x_batch, y_batch = x_batch.to(device), y_batch.to(device)
# Loop over each slice in the batch
for slice_id in range(0, x_batch.shape[1], batchsize):
x = x_batch[:, slice_id:slice_id + batchsize, ...]
y = y_batch[:, slice_id:slice_id + batchsize, ...]
x = x.permute(1, 0, 2, 3) # Assuming the first dimension is the batch dimension
y = y.permute(1, 0, 2, 3)
y = torch.squeeze(y,1)
optimizer.zero_grad()
# apply model and calculate loss
prediction = model(x) # Assuming model expects a batch dimension
loss = loss_function(prediction, y)
# backpropagate the loss and adjust the parameters
loss.backward()
optimizer.step()
# log to console
if batch_id % log_interval == 0:
print(
"Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}".format(
epoch,
batch_id * len(x_batch) + slice_id, # Adjusted index calculation
len(loader.dataset),
100.0 * batch_id / len(loader),
loss.item(),
)
)
# log to tensorboard
if tb_logger is not None:
step = epoch * len(loader.dataset) + batch_id * len(x_batch) + slice_id # Adjusted step calculation
tb_logger.add_scalar(
tag="train_loss", scalar_value=loss.item(), global_step=step
)
# check if we log images in this iteration
if step % log_image_interval == 0:
tb_logger.add_images(
tag="input", img_tensor=x.to("cpu"), global_step=step # Assuming input requires batch dimension
)
tb_logger.add_images(
tag="target", img_tensor=y.to("cpu"), global_step=step # Assuming target requires batch dimension
)
tb_logger.add_images(
tag="prediction",
img_tensor=prediction.to("cpu").detach(),
global_step=step,
)
if early_stop and batch_id > 5:
print("Stopping test early!")
break
def validate(
model,
loader,
loss_function,
batchsize,
step=None,
tb_logger=None,
device=None,
):
if device is None:
# You can pass in a device or we will default to using
# the gpu. Feel free to try training on the cpu to see
# what sort of performance difference there is
if torch.cuda.is_available():
device = torch.device("cuda")
else:
device = torch.device("cpu")
# set model to eval mode
model.eval()
model.to(device)
# running loss and metric values
val_loss = 0
# disable gradients during validation
with torch.no_grad():
# iterate over validation loader and update loss and metric values
for x_batch, y_batch in loader:
# move input and target to the activedevice
for slice_id in range(0, x_batch.shape[1], batchsize):
x = x_batch[:, slice_id:slice_id + batchsize, ...]
y = y_batch[:, slice_id:slice_id + batchsize, ...]
x = x.permute(1, 0, 2, 3) # Assuming the first dimension is the batch dimension
y = y.permute(1, 0, 2, 3)
y = torch.squeeze(y,1)
# apply model and calculate loss
prediction = model(x) # Assuming model expects a batch dimension
val_loss += loss_function(prediction, y).item()
# normalize loss and metric
val_loss /= len(loader)
print(
"\nValidate: Average loss: {:.4f}".format(
val_loss
)
)
loss = nn.CrossEntropyLoss(ignore_index=1)
n_epochs = 40
for epoch in range(n_epochs):
train(
unet,
train_loader,
optimizer=optimizer,
loss_function=loss,
epoch=epoch,
log_interval=5,
device=device,
)
step = epoch * len(train_loader)
validate(
unet, val_loader, loss,batchsize=5, step=step, device=device
)
torch.save(unet.state_dict(), 'unet_model.pth')