-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathend2end.py
372 lines (288 loc) · 15.8 KB
/
end2end.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
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
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
from models import SegDecNet
import numpy as np
import os
from torch import nn as nn
import torch
import utils
import pandas as pd
from data.dataset_catalog import get_dataset
import random
import cv2
from config import Config
from torch.utils.tensorboard import SummaryWriter
LVL_ERROR = 10
LVL_INFO = 5
LVL_DEBUG = 1
LOG = 1 # Will log all mesages with lvl greater than this
SAVE_LOG = True
WRITE_TENSORBOARD = True # False
class End2End:
def __init__(self, cfg: Config):
self.cfg: Config = cfg
self.storage_path: str = os.path.join(self.cfg.RESULTS_PATH, self.cfg.DATASET)
def _log(self, message, lvl=LVL_INFO):
n_msg = f"{self.run_name} {message}"
if lvl >= LOG:
print(n_msg)
def train(self):
self._set_results_path()
self._create_results_dirs()
self.print_run_params()
if self.cfg.REPRODUCIBLE_RUN:
self._log("Reproducible run, fixing all seeds to:1337", LVL_DEBUG)
np.random.seed(1337)
torch.manual_seed(1337)
random.seed(1337)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
device = self._get_device()
model = self._get_model().to(device)
optimizer = self._get_optimizer(model)
loss_seg, loss_dec = self._get_loss(True), self._get_loss(False)
train_loader = get_dataset("TRAIN", self.cfg)
validation_loader = get_dataset("VAL", self.cfg)
tensorboard_writer = SummaryWriter(log_dir=self.tensorboard_path) if WRITE_TENSORBOARD else None
train_results = self._train_model(device, model, train_loader, loss_seg, loss_dec, optimizer, validation_loader, tensorboard_writer)
self._save_train_results(train_results)
self._save_model(model)
self.eval(model, device, self.cfg.SAVE_IMAGES, False, False)
self._save_params()
def eval(self, model, device, save_images, plot_seg, reload_final):
self.reload_model(model, reload_final)
test_loader = get_dataset("TEST", self.cfg)
self.eval_model(device, model, test_loader, save_folder=self.outputs_path, save_images=save_images, is_validation=False, plot_seg=plot_seg)
def training_iteration(self, data, device, model, criterion_seg, criterion_dec, optimizer, weight_loss_seg, weight_loss_dec,
tensorboard_writer, iter_index):
imgs, seg_masks, seg_loss_masks, is_segmented, _ = data
images = imgs[:,:,:,:256].clone()
rfs = imgs[:,:,:,256:].clone()
import torch.nn.functional as F
_rfs = F.interpolate(rfs, size=(int(rfs.shape[2]/8), int(rfs.shape[3]/8)))
batch_size = self.cfg.BATCH_SIZE
memory_fit = self.cfg.MEMORY_FIT # Not supported yet for >1
num_subiters = int(batch_size / memory_fit)
total_loss = 0
total_correct = 0
optimizer.zero_grad()
total_loss_seg = 0
total_loss_dec = 0
for sub_iter in range(num_subiters):
images_ = images[sub_iter * memory_fit:(sub_iter + 1) * memory_fit, :, :, :].to(device)
rfs_ = _rfs[sub_iter * memory_fit:(sub_iter + 1) * memory_fit, :, :, :].to(device)
seg_masks_ = seg_masks[sub_iter * memory_fit:(sub_iter + 1) * memory_fit, :, :, :].to(device)
seg_loss_masks_ = seg_loss_masks[sub_iter * memory_fit:(sub_iter + 1) * memory_fit, :, :, :].to(device)
is_pos_ = seg_masks_.max().reshape((memory_fit, 1)).to(device)
if tensorboard_writer is not None and iter_index % 100 == 0:
tensorboard_writer.add_image(f"{iter_index}/image", images_[0, :, :, :])
tensorboard_writer.add_image(f"{iter_index}/image", rfs_[0, :, :, :])
tensorboard_writer.add_image(f"{iter_index}/seg_mask", seg_masks[0, :, :, :])
tensorboard_writer.add_image(f"{iter_index}/seg_loss_mask", seg_loss_masks_[0, :, :, :])
decision, output_seg_mask = model(images_, rfs_)
if is_segmented[sub_iter]:
if self.cfg.WEIGHTED_SEG_LOSS:
loss_seg = torch.mean(criterion_seg(output_seg_mask, seg_masks_) * seg_loss_masks_)
else:
loss_seg = criterion_seg(output_seg_mask, seg_masks_)
loss_dec = criterion_dec(decision, is_pos_)
total_loss_seg += loss_seg.item()
total_loss_dec += loss_dec.item()
total_correct += (decision > 0.5).item() == is_pos_.item()
loss = weight_loss_seg * loss_seg + weight_loss_dec * loss_dec
else:
loss_dec = criterion_dec(decision, is_pos_)
total_loss_dec += loss_dec.item()
total_correct += (decision > 0.5).item() == is_pos_.item()
loss = weight_loss_dec * loss_dec
total_loss += loss.item()
loss.backward()
# Backward and optimize
optimizer.step()
optimizer.zero_grad()
return total_loss_seg, total_loss_dec, total_loss, total_correct
def _train_model(self, device, model, train_loader, criterion_seg, criterion_dec, optimizer, validation_set, tensorboard_writer):
losses = []
validation_data = []
max_validation = -1
validation_step = self.cfg.VALIDATION_N_EPOCHS
num_epochs = self.cfg.EPOCHS
samples_per_epoch = len(train_loader) * self.cfg.BATCH_SIZE
self.set_dec_gradient_multiplier(model, 0.0)
for epoch in range(num_epochs):
# if epoch % 5 == 0:
self._save_model(model, f"ep_{epoch:02}.pth")
model.train()
weight_loss_seg, weight_loss_dec = self.get_loss_weights(epoch)
dec_gradient_multiplier = self.get_dec_gradient_multiplier()
self.set_dec_gradient_multiplier(model, dec_gradient_multiplier)
epoch_loss_seg, epoch_loss_dec, epoch_loss = 0, 0, 0
epoch_correct = 0
from timeit import default_timer as timer
time_acc = 0
start = timer()
for iter_index, (data) in enumerate(train_loader):
start_1 = timer()
curr_loss_seg, curr_loss_dec, curr_loss, correct = self.training_iteration(data, device, model,
criterion_seg,
criterion_dec,
optimizer, weight_loss_seg,
weight_loss_dec,
tensorboard_writer, (epoch * samples_per_epoch + iter_index))
end_1 = timer()
time_acc = time_acc + (end_1 - start_1)
epoch_loss_seg += curr_loss_seg
epoch_loss_dec += curr_loss_dec
epoch_loss += curr_loss
epoch_correct += correct
end = timer()
epoch_loss_seg = epoch_loss_seg / samples_per_epoch
epoch_loss_dec = epoch_loss_dec / samples_per_epoch
epoch_loss = epoch_loss / samples_per_epoch
losses.append((epoch_loss_seg, epoch_loss_dec, epoch_loss, epoch))
self._log(
f"Epoch {epoch + 1}/{num_epochs} ==> avg_loss_seg={epoch_loss_seg:.5f}, avg_loss_dec={epoch_loss_dec:.5f}, avg_loss={epoch_loss:.5f}, correct={epoch_correct}/{samples_per_epoch}, in {end - start:.2f}s/epoch (fwd/bck in {time_acc:.2f}s/epoch)")
if tensorboard_writer is not None:
tensorboard_writer.add_scalar("Loss/Train/segmentation", epoch_loss_seg, epoch)
tensorboard_writer.add_scalar("Loss/Train/classification", epoch_loss_dec, epoch)
tensorboard_writer.add_scalar("Loss/Train/joined", epoch_loss, epoch)
tensorboard_writer.add_scalar("Accuracy/Train/", epoch_correct / samples_per_epoch, epoch)
if self.cfg.VALIDATE and (epoch % validation_step == 0 or epoch == num_epochs - 1):
validation_ap, validation_accuracy = self.eval_model(device, model, validation_set, None, False, True, False)
validation_data.append((validation_ap, epoch))
if validation_ap > max_validation:
max_validation = validation_ap
self._save_model(model, "best_state_dict.pth")
model.train()
if tensorboard_writer is not None:
tensorboard_writer.add_scalar("Accuracy/Validation/", validation_accuracy, epoch)
return losses, validation_data
def eval_model(self, device, model, eval_loader, save_folder, save_images, is_validation, plot_seg):
model.eval()
dsize = self.cfg.INPUT_WIDTH, self.cfg.INPUT_HEIGHT
res = []
predictions, ground_truths = [], []
for data_point in eval_loader:
image, seg_mask, seg_loss_mask, _, sample_name = data_point
image, seg_mask = image.to(device), seg_mask.to(device)
is_pos = (seg_mask.max() > 0).reshape((1, 1)).to(device).item()
prediction, pred_seg = model(image)
pred_seg = nn.Sigmoid()(pred_seg)
prediction = nn.Sigmoid()(prediction)
prediction = prediction.item()
image = image.detach().cpu().numpy()
pred_seg = pred_seg.detach().cpu().numpy()
seg_mask = seg_mask.detach().cpu().numpy()
predictions.append(prediction)
ground_truths.append(is_pos)
res.append((prediction, None, None, is_pos, sample_name[0]))
if not is_validation:
if save_images:
image = cv2.resize(np.transpose(image[0, :, :, :], (1, 2, 0)), dsize)
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
pred_seg = cv2.resize(pred_seg[0, 0, :, :], dsize) if len(pred_seg.shape) == 4 else cv2.resize(pred_seg[0, :, :], dsize)
seg_mask = cv2.resize(seg_mask[0, 0, :, :], dsize)
if self.cfg.WEIGHTED_SEG_LOSS:
seg_loss_mask = cv2.resize(seg_loss_mask.numpy()[0, 0, :, :], dsize)
utils.plot_sample(sample_name[0], image, pred_seg, seg_loss_mask, save_folder, decision=prediction, plot_seg=plot_seg)
else:
utils.plot_sample(sample_name[0], image, pred_seg, seg_mask, save_folder, decision=prediction, plot_seg=plot_seg)
if is_validation:
metrics = utils.get_metrics(np.array(ground_truths), np.array(predictions))
FP, FN, TP, TN = list(map(sum, [metrics["FP"], metrics["FN"], metrics["TP"], metrics["TN"]]))
self._log(f"VALIDATION || AUC={metrics['AUC']:f}, and AP={metrics['AP']:f}, with best thr={metrics['best_thr']:f} "
f"at f-measure={metrics['best_f_measure']:.3f} and FP={FP:d}, FN={FN:d}, TOTAL SAMPLES={FP + FN + TP + TN:d}")
return metrics["AP"], metrics["accuracy"]
else:
utils.evaluate_metrics(res, self.run_path, self.run_name)
def get_dec_gradient_multiplier(self):
if self.cfg.GRADIENT_ADJUSTMENT:
grad_m = 0
else:
grad_m = 1
self._log(f"Returning dec_gradient_multiplier {grad_m}", LVL_DEBUG)
return grad_m
def set_dec_gradient_multiplier(self, model, multiplier):
model.set_gradient_multipliers(multiplier)
def get_loss_weights(self, epoch):
total_epochs = float(self.cfg.EPOCHS)
if self.cfg.DYN_BALANCED_LOSS:
seg_loss_weight = 1 - (epoch / total_epochs)
dec_loss_weight = self.cfg.DELTA_CLS_LOSS * (epoch / total_epochs)
else:
seg_loss_weight = 1
dec_loss_weight = self.cfg.DELTA_CLS_LOSS
self._log(f"Returning seg_loss_weight {seg_loss_weight} and dec_loss_weight {dec_loss_weight}", LVL_DEBUG)
return seg_loss_weight, dec_loss_weight
def reload_model(self, model, load_final=False):
if self.cfg.USE_BEST_MODEL:
path = os.path.join(self.model_path, "best_state_dict.pth")
model.load_state_dict(torch.load(path))
self._log(f"Loading model state from {path}")
elif load_final:
path = os.path.join(self.model_path, "final_state_dict.pth")
model.load_state_dict(torch.load(path))
self._log(f"Loading model state from {path}")
else:
self._log("Keeping same model state")
def _save_params(self):
params = self.cfg.get_as_dict()
params_lines = sorted(map(lambda e: e[0] + ":" + str(e[1]) + "\n", params.items()))
fname = os.path.join(self.run_path, "run_params.txt")
with open(fname, "w+") as f:
f.writelines(params_lines)
def _save_train_results(self, results):
losses, validation_data = results
ls, ld, l, le = map(list, zip(*losses))
plt.plot(le, l, label="Loss", color="red")
plt.plot(le, ls, label="Loss seg")
plt.plot(le, ld, label="Loss dec")
plt.ylim(bottom=0)
plt.grid()
plt.xlabel("Epochs")
if self.cfg.VALIDATE:
v, ve = map(list, zip(*validation_data))
plt.twinx()
plt.plot(ve, v, label="Validation AP", color="Green")
plt.ylim((0, 1))
plt.legend()
plt.savefig(os.path.join(self.run_path, "loss_val"), dpi=200)
df_loss = pd.DataFrame(data={"loss_seg": ls, "loss_dec": ld, "loss": l, "epoch": le})
df_loss.to_csv(os.path.join(self.run_path, "losses.csv"), index=False)
if self.cfg.VALIDATE:
df_loss = pd.DataFrame(data={"validation_data": ls, "loss_dec": ld, "loss": l, "epoch": le})
df_loss.to_csv(os.path.join(self.run_path, "losses.csv"), index=False)
def _save_model(self, model, name="final_state_dict.pth"):
output_name = os.path.join(self.model_path, name)
self._log(f"Saving current model state to {output_name}")
if os.path.exists(output_name):
os.remove(output_name)
torch.save(model.state_dict(), output_name)
def _get_optimizer(self, model):
return torch.optim.SGD(model.parameters(), self.cfg.LEARNING_RATE)
def _get_loss(self, is_seg):
reduction = "none" if self.cfg.WEIGHTED_SEG_LOSS and is_seg else "mean"
return nn.BCEWithLogitsLoss(reduction=reduction).to(self._get_device())
def _get_device(self):
return f"cuda:{self.cfg.GPU}"
def _set_results_path(self):
self.run_name = f"{self.cfg.RUN_NAME}_FOLD_{self.cfg.FOLD}" if self.cfg.DATASET in ["KSDD", "DAGM"] else self.cfg.RUN_NAME
results_path = os.path.join(self.cfg.RESULTS_PATH, self.cfg.DATASET)
self.tensorboard_path = os.path.join(results_path, "tensorboard", self.run_name)
run_path = os.path.join(results_path, self.cfg.RUN_NAME)
if self.cfg.DATASET in ["KSDD", "DAGM"]:
run_path = os.path.join(run_path, f"FOLD_{self.cfg.FOLD}")
self._log(f"Executing run with path {run_path}")
self.run_path = run_path
self.model_path = os.path.join(run_path, "models")
self.outputs_path = os.path.join(run_path, "test_outputs")
def _create_results_dirs(self):
list(map(utils.create_folder, [self.run_path, self.model_path, self.outputs_path, ]))
def _get_model(self):
seg_net = SegDecNet(self._get_device(), self.cfg.INPUT_WIDTH, self.cfg.INPUT_HEIGHT, self.cfg.INPUT_CHANNELS)
return seg_net
def print_run_params(self):
for l in sorted(map(lambda e: e[0] + ":" + str(e[1]) + "\n", self.cfg.get_as_dict().items())):
k, v = l.split(":")
self._log(f"{k:25s} : {str(v.strip())}")