-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
205 lines (167 loc) · 6.89 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import os
import torch
from torch.optim import Adam
from tqdm import tqdm, trange
from torchvision import transforms
from torch.utils.data import DataLoader
from torch.optim.lr_scheduler import ReduceLROnPlateau
from images_dataset import ImagesDataset
from unet import UNet
from helpers import DiceLoss, accuracy_score_tensors, f1_score_tensors, train_test_split, predict_labels
seed = 0
data_path = os.path.abspath("Data/")
models_path = os.path.abspath("models/")
model_path = os.path.join(models_path, 'unetBest.pt')
data_train_path = os.path.join(data_path, 'augmented_training')
grdTruth_path = os.path.join(data_train_path, 'groundtruth')
image_path = os.path.join(data_train_path, 'images')
class Trainer:
def __init__(self, model, lossF, optimizer, weights_path, data_loader, lr_scheduler,
valid_data_loader=None, threshold=0.25):
"""
:param model: The NN Model (torch Module)
:param lossF: The loss function
:param optimizer: The optimizer function
:param weights_path: The path to save the model
:param data_loader:
:param lr_scheduler:
:param valid_data_loader: Loader of validation data
:param threshold:
"""
self.tqdm = tqdm
self.trange = trange
self.lr_scheduler = lr_scheduler
self.model = model
self.lossF = lossF
self.optimizer = optimizer
self.weights_path = weights_path
self.data_loader = data_loader
self.valid_data_loader = valid_data_loader
self.threshold = threshold
batchSize = self.data_loader.batch_size
dataSize = len(self.data_loader.dataset)
self.train_steps = dataSize // batchSize
if self.valid_data_loader is not None:
validDataSize = len(self.valid_data_loader.dataset)
validBatchSize = self.valid_data_loader.batch_size
self.valid_steps = validDataSize // validBatchSize
def train_epoch(self, epoch):
"""
:param epoch: Epoch number
:returns: [loss, accuracy, f1]
Runs the epoch on the training set while updating the parameters
"""
self.model.train()
f1 = 0.0
loss = 0.0
accuracy = 0.0
with self.tqdm(self.data_loader, desc=f'Training Epoch {epoch}', leave=False, unit='batch') as tq:
tq.set_postfix({"loss": loss, "accuracy": accuracy, "f1": f1})
for data, target in tq:
self.optimizer.zero_grad()
output = self.model(data)
cur_loss = self.lossF(output, target)
cur_loss.backward()
self.optimizer.step()
output = predict_labels(output, self.threshold)
target = predict_labels(target, self.threshold)
accuracy += accuracy_score_tensors(target, output)
f1 += f1_score_tensors(target, output)
loss += cur_loss.item()
tq.set_postfix({"loss": loss, "accuracy": accuracy, "f1": f1})
return [loss / self.train_steps, accuracy / self.train_steps, f1 / self.train_steps]
def valid_epoch(self, epoch):
"""
:param epoch: Epoch number
:returns: [loss, accuracy, f1]
Runs the epoch on the validation set
"""
self.model.eval()
f1 = 0.0
loss = 0.0
accuracy = 0.0
with torch.no_grad():
with self.tqdm(self.valid_data_loader, desc=f'Validation epoch {epoch}',
unit='batch', leave=False) as tq:
tq.set_postfix({"loss": loss, "accuracy": accuracy, "f1": f1})
for data, target in tq:
output = self.model(data)
cur_loss = self.lossF(output, target)
output = predict_labels(output, self.threshold)
target = predict_labels(target, self.threshold)
loss += cur_loss.item()
f1 += f1_score_tensors(target, output)
accuracy += accuracy_score_tensors(target, output)
tq.set_postfix({"loss": loss, "accuracy": accuracy, "f1": f1})
return [loss / self.valid_steps, accuracy / self.valid_steps, f1 / self.valid_steps]
def train(self, epochs):
"""
:param epochs: Number of epochs
Trains the model
"""
max_f1 = 0.0
stats = dict()
with self.trange(1, epochs + 1, unit='epoch', desc='Training') as t:
for epoch in t:
[train_loss, train_accuracy, train_f1] = self.train_epoch(epoch)
stats.update({"loss": train_loss, "accuracy": train_accuracy, "f1": train_f1})
if self.valid_data_loader is not None:
[valid_loss, valid_accuracy, valid_f1] = self.valid_epoch(epoch)
stats.update({"loss": valid_loss, "accuracy": valid_accuracy, "f1": valid_f1})
if valid_f1 > max_f1:
max_f1 = valid_f1
stats['max_f1'] = max_f1
torch.save(self.model.state_dict(), self.weights_path)
else:
if train_f1 > max_f1:
max_f1 = train_f1
stats['max_f1'] = max_f1
torch.save(self.model.state_dict(), self.weights_path)
self.lr_scheduler.step(train_loss)
t.set_postfix(stats)
def train(batch_size=10, epochs=50, lr=2e-4, split_ratio=0.15, weight_decay=1e-4):
torch.manual_seed(seed)
if not os.path.exists(models_path):
os.mkdir(models_path)
dataset = ImagesDataset(
image_dir=image_path,
grdTruth_dir=grdTruth_path,
image_transform=transforms.Compose([transforms.ToTensor()]),
mask_transform=transforms.Compose([transforms.ToTensor()]),
)
train_loader = None
test_loader = None
if split_ratio == 0:
train_loader = DataLoader(
dataset=dataset,
batch_size=batch_size,
shuffle=True,
)
else:
train_set, test_set = train_test_split(dataset=dataset, split_ratio=split_ratio)
test_loader = DataLoader(
dataset=test_set,
shuffle=False,
num_workers=2,
)
train_loader = DataLoader(
dataset=train_set,
batch_size=batch_size,
shuffle=True,
num_workers=2,
)
model = UNet()
optimizer = Adam(model.parameters(), lr=lr, weight_decay=weight_decay)
lr_scheduler = ReduceLROnPlateau(optimizer=optimizer, mode='min', patience=5)
trainer = Trainer(
model=model,
lossF=DiceLoss(),
optimizer=optimizer,
weights_path=model_path,
data_loader=train_loader,
lr_scheduler=lr_scheduler,
valid_data_loader=test_loader
)
trainer.train(epochs)
if __name__ == '__main__':
train(split_ratio=0)