-
Notifications
You must be signed in to change notification settings - Fork 8
/
Learner.py
172 lines (142 loc) · 7.24 KB
/
Learner.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
import os
import time
import numpy as np
import torch
from torch.optim.lr_scheduler import CosineAnnealingWarmRestarts
from tqdm import tqdm
from scripts.utils import init_logger
from scripts.tb_utils import init_tb_logger
from scripts.metric import Evaluator, AverageMeter
from scripts.optimizer import RAdam
from albumentations.core.composition import Compose
from albumentations.augmentations.transforms import Normalize
from albumentations.pytorch.transforms import ToTensorV2
class Learner:
def __init__(self, model, train_loader, valid_loader, config):
self.config = config
self.train_loader = train_loader
self.valid_loader = valid_loader
self.model = model.to(self.config.device)
self.logger = init_logger(self.config.log_dir, 'train_main.log')
self.tb_logger = init_tb_logger(self.config.log_dir, 'train_main')
self.log('\n'.join([f"{k} = {v}" for k, v in self.config.__dict__.items()]))
self.summary_loss = AverageMeter()
self.evaluator = Evaluator()
self.criterion = torch.nn.CrossEntropyLoss(ignore_index=self.config.ignore_index)
self.u_criterion = torch.nn.CrossEntropyLoss(ignore_index=self.config.ignore_index)
train_params = [{'params': getattr(model, 'encoder').parameters(), 'lr': self.config.lr},
{'params': getattr(model, 'decoder').parameters(), 'lr': self.config.lr * 10}]
self.optimizer = RAdam(train_params, weight_decay=self.config.weight_decay)
self.scheduler = CosineAnnealingWarmRestarts(self.optimizer, T_0=2, T_mult=2, eta_min=1e-6)
self.n_ensemble = 0
self.epoch = 0
self.best_epoch = 0
self.best_loss = np.inf
self.best_score = -np.inf
def train_one_epoch(self):
self.model.train()
self.summary_loss.reset()
iters = len(self.train_loader)
for step, (images, scribbles, weights) in enumerate(self.train_loader):
self.tb_logger.add_scalar('Train/lr', self.optimizer.param_groups[0]['lr'],
iters * self.epoch + step)
scribbles = scribbles.to(self.config.device).long()
images = images.to(self.config.device)
batch_size = images.shape[0]
self.optimizer.zero_grad()
outputs = self.model(images)
if self.epoch < self.config.thr_epoch:
loss = self.criterion(outputs, scribbles)
else:
x_loss = self.criterion(outputs, scribbles)
scribbles = scribbles.cpu()
mean = weights[..., 0]
u_labels = torch.where(((mean < (1 - self.config.thr_conf)) |
(mean > self.config.thr_conf)) &
(scribbles == self.config.ignore_index),
mean.round().long(),
self.config.ignore_index * torch.ones_like(scribbles)).to(self.config.device)
u_loss = self.u_criterion(outputs, u_labels)
loss = x_loss + 0.5 * u_loss
loss.backward()
self.summary_loss.update(loss.detach().item(), batch_size)
self.optimizer.step()
if self.scheduler.__class__.__name__ != 'ReduceLROnPlateau':
self.scheduler.step()
return self.summary_loss.avg
def validation(self):
self.model.eval()
self.summary_loss.reset()
self.evaluator.reset()
for step, (_, images, _, targets) in enumerate(self.valid_loader):
with torch.no_grad():
targets = targets.to(self.config.device).long()
batch_size = images.shape[0]
images = images.to(self.config.device)
outputs = self.model(images)
loss = self.criterion(outputs, targets)
targets = targets.cpu().numpy()
outputs = torch.argmax(outputs, dim=1)
outputs = outputs.data.cpu().numpy()
self.evaluator.add_batch(targets, outputs)
self.summary_loss.update(loss.detach().item(), batch_size)
if self.scheduler.__class__.__name__ == 'ReduceLROnPlateau':
self.scheduler.step(self.evaluator.IoU)
return self.summary_loss.avg, self.evaluator.IoU
def ensemble_prediction(self):
ds = self.train_loader.dataset
transforms = Compose([Normalize(), ToTensorV2()])
for idx, images in tqdm(ds.images.items(), total=len(ds)):
augmented = transforms(image=images['image'])
img = augmented['image'].unsqueeze(0).to(self.config.device)
with torch.no_grad():
pred = torch.nn.functional.softmax(self.model(img), dim=1)
weight = torch.tensor(images['weight'])
pred = pred.squeeze(0).cpu()
x = pred[1]
weight[...,0] = self.config.alpha * x + (1-self.config.alpha) * weight[...,0]
self.train_loader.dataset.images[idx]['weight'] = weight.numpy()
self.n_ensemble += 1
def fit(self, epochs):
for e in range(epochs):
t = time.time()
loss = self.train_one_epoch()
self.log(f'[Train] \t Epoch: {self.epoch}, loss: {loss:.5f}, time: {(time.time() - t):.2f}')
self.tb_log(loss, None, 'Train', self.epoch)
t = time.time()
loss, score = self.validation()
self.log(f'[Valid] \t Epoch: {self.epoch}, loss: {loss:.5f}, IoU: {score:.4f}, time: {(time.time() - t):.2f}')
self.tb_log(loss, score, 'Valid', self.epoch)
self.post_processing(loss, score)
if (self.epoch + 1) % self.config.period_epoch == 0:
self.log(f'[Ensemble] \t the {self.n_ensemble}th Prediction Ensemble ...')
self.ensemble_prediction()
self.epoch += 1
self.log(f'best epoch: {self.best_epoch}, best loss: {self.best_loss}, best_score: {self.best_score}')
def post_processing(self, loss, score):
if loss < self.best_loss:
self.best_loss = loss
if score > self.best_score:
self.best_score = score
self.best_epoch = self.epoch
self.model.eval()
torch.save({
'model_state_dict': self.model.state_dict(),
'optimizer_state_dict': self.optimizer.state_dict(),
'scheduler_state_dict': self.scheduler.state_dict(),
'best_score': self.best_score,
'epoch': self.epoch,
}, f'{os.path.join(self.config.log_dir, "best_model.pth")}')
self.log(f'best model: {self.epoch} epoch - {score:.4f}')
def load(self, path):
checkpoint = torch.load(path)
self.model.load_state_dict(checkpoint['model_state_dict'])
self.optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
self.scheduler.load_state_dict(checkpoint['scheduler_state_dict'])
self.best_score = checkpoint['best_score']
self.epoch = checkpoint['epoch'] + 1
def log(self, text):
self.logger.info(text)
def tb_log(self, loss, IoU, split, step):
if loss: self.tb_logger.add_scalar(f'{split}/Loss', loss, step)
if IoU: self.tb_logger.add_scalar(f'{split}/IoU', IoU, step)