forked from sebemery/Cross-Consistency-Prostate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trainer.py
298 lines (247 loc) · 13.5 KB
/
trainer.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
import torch
import time, random, cv2, sys
from math import ceil
import numpy as np
from itertools import cycle
import torch.nn.functional as F
from torchvision.utils import make_grid
from torchvision import transforms
from base import BaseTrainer
from utils.helpers import colorize_mask
from utils.metrics import eval_metrics, AverageMeter
from tqdm import tqdm
from PIL import Image
from utils.helpers import DeNormalize, scale
import matplotlib.pyplot as plt
class Trainer(BaseTrainer):
def __init__(self, model, resume, config, supervised_loader, unsupervised_loader, iter_per_epoch,
val_loader=None, train_logger=None):
super(Trainer, self).__init__(model, resume, config, iter_per_epoch, train_logger)
self.supervised_loader = supervised_loader
self.unsupervised_loader = unsupervised_loader
self.val_loader = val_loader
self.wrt_mode, self.wrt_step = 'train_', 0
self.log_step = config['trainer'].get('log_per_iter', int(np.sqrt(self.val_loader.batch_size)))
if config['trainer']['log_per_iter']:
self.log_step = int(self.log_step / self.val_loader.batch_size) + 1
self.num_classes = self.val_loader.dataset.num_classes
self.mode = self.model.module.mode
self.start_time = time.time()
def _train_epoch(self, epoch):
self.html_results.save()
self.logger.info('\n')
self.model.train()
if self.mode == 'supervised':
dataloader = iter(self.supervised_loader)
tbar = tqdm(range(len(self.supervised_loader)), ncols=135)
else:
dataloader = iter(zip(cycle(self.supervised_loader), cycle(self.unsupervised_loader)))
tbar = tqdm(range(len(self.supervised_loader)), ncols=135)
self._reset_metrics()
for batch_idx in tbar:
if self.mode == 'supervised':
(input_l, target_l), (input_ul, target_ul) = next(dataloader), (None, None)
else:
(input_l, target_l), (input_ul, target_ul) = next(dataloader)
if self.str_device == 'gpu':
input_ul, target_ul = input_ul.cuda(non_blocking=True), target_ul.cuda(non_blocking=True)
if self.str_device == 'gpu':
input_l, target_l = input_l.cuda(non_blocking=True), target_l.cuda(non_blocking=True)
self.optimizer.zero_grad()
total_loss, cur_losses, outputs = self.model(x_l=input_l, target_l=target_l, x_ul=input_ul,curr_iter=batch_idx, target_ul=target_ul, epoch=epoch-1)
total_loss = total_loss.mean()
total_loss.backward()
self.optimizer.step()
self._update_losses(cur_losses)
self._compute_metrics(outputs, target_l, target_ul, epoch-1)
self.count += input_l.size(0)
logs = self._log_values(cur_losses)
if batch_idx % self.log_step == 0:
self.wrt_step = (epoch - 1) * len(self.unsupervised_loader) + batch_idx
self._write_scalars_tb(logs)
if batch_idx % int(len(self.unsupervised_loader)) == 0:
self._write_img_tb(input_l, target_l, input_ul, target_ul, outputs, epoch)
del input_l, target_l, input_ul, target_ul
del total_loss, cur_losses, outputs
tbar.set_description('T ({}) | Ls {:.2f} Lu {:.2f} Lw {:.2f} PW {:.2f} m1 {:.2f} m2 {:.2f} DSL {:.2f} DSUL {:.2f}|'.format(epoch, self.loss_sup.average, self.loss_unsup.average,self.loss_weakly.average, self.pair_wise.average, self.mIoU_l,self.mIoU_ul, self.mdice_l, self.mdice_ul))
self.lr_scheduler.step(epoch=epoch-1)
return logs
def _valid_epoch(self, epoch):
if self.val_loader is None:
self.logger.warning('Not data loader was passed for the validation step, No validation is performed !')
return {}
self.logger.info('\n###### EVALUATION ######')
self.model.eval()
self.wrt_mode = 'val'
total_loss_val = AverageMeter()
total_inter, total_union = 0, 0
total_correct, total_label = 0, 0
total_dice = 0
count = 0
tbar = tqdm(self.val_loader, ncols=130)
with torch.no_grad():
val_visual = []
for batch_idx, (data, target) in enumerate(tbar):
if self.str_device == 'gpu':
target, data = target.cuda(non_blocking=True), data.cuda(non_blocking=True)
output = self.model(data)
# LOSS
loss = F.cross_entropy(output, target)
total_loss_val.update(loss.item())
correct, labeled, inter, union, dice = eval_metrics(output, target, self.num_classes, self.str_device)
total_inter, total_union = total_inter+inter, total_union+union
total_correct, total_label = total_correct+correct, total_label+labeled
total_dice = ((count*total_dice)+(dice*output.size(0)))/(count+output.size(0))
count += output.size(0)
# LIST OF IMAGE TO VIZ (15 images)
if len(val_visual) < 15:
if isinstance(data, list):
data = data[0]
target_np = target.data.cpu().numpy()
output_np = output.data.max(1)[1].cpu().numpy()
val_visual.append([data[0].data.cpu(), target_np[0], output_np[0]])
# PRINT INFO
pixAcc = 1.0 * total_correct / (np.spacing(1) + total_label)
IoU = 1.0 * total_inter / (np.spacing(1) + total_union)
mIoU = IoU.mean()
mdice = dice.mean()
seg_metrics = {"Pixel_Accuracy": np.round(pixAcc, 3), "Mean_IoU": np.round(mIoU, 3),"Mean_dice": np.round(mdice, 3),"Class_IoU": dict(zip(range(self.num_classes), np.round(IoU, 3))),"Class_dice": dict(zip(range(self.num_classes), np.round(dice, 3)))}
tbar.set_description('EVAL ({}) | Loss: {:.3f}, PixelAcc: {:.2f}, Mean IoU: {:.2f} Mean Dice {:.2f} |'.format(epoch, total_loss_val.average, pixAcc, mIoU, mdice))
self._add_img_tb(val_visual, 'val')
# METRICS TO TENSORBOARD
self.wrt_step = (epoch) * len(self.val_loader)
self.writer.add_scalar(f'{self.wrt_mode}/loss', total_loss_val.average, self.wrt_step)
for k, v in list(seg_metrics.items())[:-2]:
self.writer.add_scalar(f'{self.wrt_mode}/{k}', v, self.wrt_step)
log = {
'val_loss': total_loss_val.average,
**seg_metrics
}
self.html_results.add_results(epoch=epoch, seg_resuts=log)
self.html_results.save()
if (time.time() - self.start_time) / 3600 > 22:
self._save_checkpoint(epoch, save_best=self.improved)
return log
def _reset_metrics(self):
self.loss_sup = AverageMeter()
self.loss_unsup = AverageMeter()
self.loss_weakly = AverageMeter()
self.pair_wise = AverageMeter()
self.total_inter_l, self.total_union_l = 0, 0
self.total_correct_l, self.total_label_l = 0, 0
self.total_inter_ul, self.total_union_ul = 0, 0
self.total_correct_ul, self.total_label_ul = 0, 0
self.dice_l, self.dice_ul = 0, 0
self.mIoU_l, self.mIoU_ul = 0, 0
self.mdice_l, self.mdice_ul = 0, 0
self.pixel_acc_l, self.pixel_acc_ul = 0, 0
self.class_iou_l, self.class_iou_ul = {}, {}
self.class_dice_l, self.class_dice_ul = {}, {}
self.count = 0
def _update_losses(self, cur_losses):
if "loss_sup" in cur_losses.keys():
self.loss_sup.update(cur_losses['loss_sup'].mean().item())
if "loss_unsup" in cur_losses.keys():
self.loss_unsup.update(cur_losses['loss_unsup'].mean().item())
if "loss_weakly" in cur_losses.keys():
self.loss_weakly.update(cur_losses['loss_weakly'].mean().item())
if "pair_wise" in cur_losses.keys():
self.pair_wise.update(cur_losses['pair_wise'].mean().item())
def _compute_metrics(self, outputs, target_l, target_ul, epoch):
seg_metrics_l = eval_metrics(outputs['sup_pred'], target_l, self.num_classes, self.str_device)
self._update_seg_metrics(*seg_metrics_l, target_l.size(0), True)
seg_metrics_l = self._get_seg_metrics(True)
self.pixel_acc_l, self.mIoU_l, self.class_iou_l, self.mdice_l, self.class_dice_l = seg_metrics_l.values()
if self.mode == 'semi':
seg_metrics_ul = eval_metrics(outputs['unsup_pred'], target_ul, self.num_classes, self.str_device)
self._update_seg_metrics(*seg_metrics_ul, target_ul.size(0), False)
seg_metrics_ul = self._get_seg_metrics(False)
self.pixel_acc_ul, self.mIoU_ul, self.class_iou_ul, self.mdice_ul, self.class_dice_ul = seg_metrics_ul.values()
def _update_seg_metrics(self, correct, labeled, inter, union, dice, batch_size, supervised=True):
if supervised:
self.total_correct_l += correct
self.total_label_l += labeled
self.total_inter_l += inter
self.total_union_l += union
self.dice_l = ((self.dice_l*self.count)+(dice*batch_size))/(self.count+batch_size)
else:
self.total_correct_ul += correct
self.total_label_ul += labeled
self.total_inter_ul += inter
self.total_union_ul += union
self.dice_ul = ((self.dice_ul*self.count)+(dice*batch_size))/(self.count+batch_size)
def _get_seg_metrics(self, supervised=True):
if supervised:
pixAcc = 1.0 * self.total_correct_l / (np.spacing(1) + self.total_label_l)
IoU = 1.0 * self.total_inter_l / (np.spacing(1) + self.total_union_l)
dice = self.dice_l
else:
pixAcc = 1.0 * self.total_correct_ul / (np.spacing(1) + self.total_label_ul)
IoU = 1.0 * self.total_inter_ul / (np.spacing(1) + self.total_union_ul)
dice = self.dice_ul
mIoU = IoU.mean()
mdice = dice.mean()
return {
"Pixel_Accuracy": np.round(pixAcc, 3),
"Mean_IoU": np.round(mIoU, 3),
"Class_IoU": dict(zip(range(self.num_classes), np.round(IoU, 3))),
"Mean_dice": np.round(mdice, 3),
"Class_dice": dict(zip(range(self.num_classes), np.round(dice, 3)))
}
def _log_values(self, cur_losses):
logs = {}
if "loss_sup" in cur_losses.keys():
logs['loss_sup'] = self.loss_sup.average
if "loss_unsup" in cur_losses.keys():
logs['loss_unsup'] = self.loss_unsup.average
if "loss_weakly" in cur_losses.keys():
logs['loss_weakly'] = self.loss_weakly.average
if "pair_wise" in cur_losses.keys():
logs['pair_wise'] = self.pair_wise.average
logs['mIoU_labeled'] = self.mIoU_l
logs['pixel_acc_labeled'] = self.pixel_acc_l
logs['dice_score_labeled'] = self.mdice_l
if self.mode == 'semi':
logs['mIoU_unlabeled'] = self.mIoU_ul
logs['pixel_acc_unlabeled'] = self.pixel_acc_ul
logs['dice_score_unlabeled'] = self.mdice_ul
return logs
def _write_scalars_tb(self, logs):
for k, v in logs.items():
if 'class_iou' not in k: self.writer.add_scalar(f'train/{k}', v, self.wrt_step)
for i, opt_group in enumerate(self.optimizer.param_groups):
self.writer.add_scalar(f'train/Learning_rate_{i}', opt_group['lr'], self.wrt_step)
current_rampup = self.model.module.unsup_loss_w.current_rampup
self.writer.add_scalar('train/Unsupervised_rampup', current_rampup, self.wrt_step)
def denormalize(self, image, mean, std):
image = (image*std)+mean
image = (image-torch.min(image)) / (torch.max(image)-torch.min(image))
return image
def _add_img_tb(self, val_visual, wrt_mode):
if wrt_mode == 'supervised':
mean = self.supervised_loader.MEAN
std = self.supervised_loader.STD
if wrt_mode == 'unsupervised':
mean = self.unsupervised_loader.MEAN
std = self.unsupervised_loader.STD
if wrt_mode == 'val':
mean = self.val_loader.MEAN
std = self.val_loader.STD
val_img = []
for imgs in val_visual:
imgs = [self.denormalize(i, mean, std) if (isinstance(i, torch.Tensor)) else scale(i) for i in imgs]
imgs = [i if (isinstance(i, torch.Tensor)) else torch.from_numpy(i) for i in imgs]
val_img.extend(imgs)
val_img = torch.stack(val_img, 0) # [3*batch_size,1,384,384]
val_img = make_grid(val_img.cpu(), nrow=val_img.size(0)//len(val_visual), padding=5)
self.writer.add_image(f'{wrt_mode}/inputs_targets_predictions', val_img, self.wrt_step,dataformats='CHW')
def _write_img_tb(self, input_l, target_l, input_ul, target_ul, outputs, epoch):
outputs_l_np = outputs['sup_pred'].data.max(1)[1].cpu().numpy()
targets_l_np = target_l.data.cpu().numpy()
imgs = [[i.data.cpu(), j, k] for i, j, k in zip(input_l, outputs_l_np, targets_l_np)]
self._add_img_tb(imgs, 'supervised')
if self.mode == 'semi':
outputs_ul_np = outputs['unsup_pred'].data.max(1)[1].cpu().numpy()
targets_ul_np = target_ul.data.cpu().numpy()
imgs = [[i.data.cpu(), j, k] for i, j, k in zip(input_ul, outputs_ul_np, targets_ul_np)]
self._add_img_tb(imgs, 'unsupervised')