This repository has been archived by the owner on Jan 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtraining.py
295 lines (228 loc) · 9.05 KB
/
training.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
import os
from functools import partial
import numpy as np
import torch
import torch.optim as optim
import statistics as s
import shutup
from numpy import savetxt
shutup.please()
from tqdm.auto import tqdm as prog
from provider.dataset_provider import get_loader
from metrics.buffered_accuarcy import bbox_accuracy
from utils.pytorchtools import (
EarlyStopping
)
from config.configuration import (
base_path,
train_path_rel,
valid_path_rel,
device,
num_workers,
pin_memory,
batch_size,
lr
)
from model.unet_model import GlacierUNET
from segmentation_models_pytorch.losses.dice import DiceLoss
from ray import tune
from ray.air import Checkpoint, session
from ray.tune.schedulers import ASHAScheduler
# The train method runs through its subset of data and puts it through the model. After that, the error will be
# calculated and back-propagated through the network just following the typical theme with some performance increasing
# tweaks of pytorch like "scaler" and "autocast"
def train(epoch, loader, loss_fn, optimizer, scaler, model):
# Set the network to "learn" mode
torch.enable_grad()
model.train()
loop = prog(loader)
running_loss = []
running_bacc = []
# Go through the dataset
for batch_index, (data, target) in enumerate(loop):
# Reset the optimizer and prepare new data
optimizer.zero_grad(set_to_none=True)
data = data.to(device)
# Predict the data-slice
data = model(data)
# Prepare the target
target = target.to(device)
# Calculate the loss and back-propagate
with torch.cuda.amp.autocast():
loss = loss_fn(data, target)
scaler.scale(loss).backward()
scaler.step(optimizer)
scaler.update()
# Retrieve the actual loss value and a float value
loss_value = loss.item()
# Safe loss and calculate metrics (bbox accuracy, described in the git)
running_loss.append(loss_value)
data = torch.round(torch.sigmoid(data))
running_bacc.append(bbox_accuracy(data, target))
loop.set_postfix(info="Epoch {}, train, loss={:.5f}".format(epoch, loss_value))
return s.mean(running_loss), s.mean(running_bacc)
# The validation methid is very similar to the train method, except it does not back-propagate the error through
# the network. However, it still calculates everything else.
def valid(epoch, loader, loss_fn, model):
# Set the network to "do not learn" mode
model.eval()
torch.no_grad()
loop = prog(loader)
running_loss = []
running_bacc = []
# Go through the dataset
for batch_index, (data, target) in enumerate(loop):
# Reset the optimizer and prepare new data (no optimizer reset needed here since no learning happens)
data = data.to(device)
# Predict the data-slice
data = model(data)
# Prepare the target
target = target.to(device)
# Only calculate, do not back-propagate the loss
with torch.no_grad():
loss = loss_fn(data, target)
# Retrieve the actual loss value and a float value
loss_value = loss.item()
# Safe loss and calculate metrics (bbox accuracy, described in the git)
running_loss.append(loss_value)
data = torch.round(torch.sigmoid(data))
running_bacc.append(bbox_accuracy(data, target))
loop.set_postfix(info="Epoch {}, valid, loss={:.5f}".format(epoch, loss_value))
return s.mean(running_loss), s.mean(running_bacc)
# The run method assembles the cycle of training and validating the model in an alternating fashion
def run(config):
# Reset the GPU cache
torch.cuda.empty_cache()
# Load a new empty model with random weights
model = GlacierUNET().to(device)
# Initialize an optimizer
optimizer = optim.Adam(model.parameters(), lr=config["lr"] if ray_tune else lr)
# Initialize the loss, the back-propagation manager and the early stopping
loss_fn = DiceLoss(mode="binary")
scaler = torch.cuda.amp.GradScaler()
early_stopping = EarlyStopping(patience=5, verbose=True)
checkpoint = session.get_checkpoint()
# If no ray-tune is set, we want to train a specific model to its end. The parameters set helps us to distinguish
# the current run from other runs via the directory paths which is directly modified by those params.
if not ray_tune:
overall_training_loss = []
overall_validation_loss = []
overall_training_bacc = []
overall_validation_bacc = []
path = "{}_{}_{}_{}/".format(
"results",
str(loss_fn.__class__.__name__),
str(optimizer.__class__.__name__),
str(GlacierUNET.__qualname__)
)
if not os.path.isdir(path):
os.mkdir(path)
else:
# If raytune is active, we try out parameters randomly many times. For more details, head to the git
if checkpoint:
checkpoint_state = checkpoint.to_dict()
model.load_state_dict(checkpoint_state["net_state_dict"])
optimizer.load_state_dict(checkpoint_state["optimizer_state_dict"])
model.to(device)
# Load both datasets
train_loader = get_loader(
os.path.join(base_path, train_path_rel),
config["batch_size"] if ray_tune else batch_size,
num_workers,
pin_memory
)
validation_loader = get_loader(
os.path.join(base_path, valid_path_rel),
config["batch_size"] if ray_tune else batch_size,
num_workers,
pin_memory
)
# For to endless (or max 100) epochs, train and validate in an alternating way.
for epoch in range(0, 100):
training_loss, training_bacc = train(
epoch,
train_loader,
loss_fn,
optimizer,
scaler,
model
)
validation_loss, validation_bacc = valid(
epoch,
validation_loader,
loss_fn,
model
)
# For saving, we again distinguish between if we currently try to figure out a good param set with raytune or if
# we train out a specific parameter set.
if ray_tune:
checkpoint_data = {
"epoch": epoch,
"net_state_dict": model.state_dict(),
"optimizer_state_dict": optimizer.state_dict(),
}
checkpoint = Checkpoint.from_dict(checkpoint_data)
session.report(
{
"training_loss": training_loss,
"validation_loss": validation_loss,
"training_bacc": training_bacc,
"validation_bacc": validation_bacc
},
checkpoint=checkpoint,
)
else:
overall_training_loss.append(training_loss)
overall_validation_loss.append(validation_loss)
overall_training_bacc.append(training_bacc)
overall_validation_bacc.append(validation_bacc)
torch.save({
'epoch': epoch,
'model_state_dict': model.cpu().state_dict(),
'optimizer_state_dict': optimizer.state_dict(),
'training_losses': overall_training_loss,
'validation_losses': overall_validation_loss,
'training_baccs': overall_training_bacc,
'validation_baccs': overall_validation_bacc,
'early_stopping': early_stopping
}, path + "model_epoch" + str(epoch) + ".pt")
model.to(device)
metrics = np.array([
overall_training_loss,
overall_validation_loss,
overall_training_bacc,
overall_validation_bacc,
], dtype='object')
# Save also a little textfile for checking everything in between that we could halt training if we see
# something is wrong
savetxt(path + "metrics.csv", metrics, delimiter=',',
header="tloss,vloss,tbacc,vbacc", fmt='%s')
if early_stopping(validation_loss):
print("Early stopping")
break
model.to(device)
def start():
# Start the whole bs. If we use raytune, we set ranges for lr, batch-sizes, ...
if ray_tune:
config_space = {
"lr": tune.loguniform(1e-6, 1e-3),
"batch_size": tune.choice([4, 8, 16, 32])
}
result = tune.run(
partial(run),
config=config_space,
num_samples=20,
resources_per_trial={
"gpu": 0.33
}
)
best_trial = result.get_best_trial("validation_loss", "min", "last")
print(f"Best trial config: {best_trial.config}")
print(f"Best trial final validation loss: {best_trial.last_result['validation_loss']}")
print(f"Best trial final validation buffered accuracy: {best_trial.last_result['validation_bacc']}")
else:
run({})
# Modify this for changing between using raytune or not
ray_tune = False
if __name__ == '__main__':
start()