-
Notifications
You must be signed in to change notification settings - Fork 0
/
trainer_dgt.py
181 lines (167 loc) · 7.13 KB
/
trainer_dgt.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
import os
import time
from datetime import datetime
import cv2
import torch
import numpy as np
import torch.backends.cudnn as cudnn
import torch.nn as nn
import torchvision.transforms.functional as TF
from loguru import logger
from torch.utils import tensorboard
from tqdm import tqdm
from utils.helpers import dir_exists, get_instance, remove_files, double_threshold_iteration, get_optimizer_instance
from utils.metrics import AverageMeter, get_metrics, get_metrics, count_connect_component
import ttach as tta
from PIL import Image
np.set_printoptions(threshold=np.inf)
class Trainer:
def __init__(self, model, CFG=None, loss=None, train_loader=None, val_loader=None):
self.CFG = CFG
if self.CFG.amp is True:
self.scaler = torch.cuda.amp.GradScaler(enabled=True)
self.loss = loss
self.model = nn.DataParallel(model.cuda())
self.train_loader = train_loader
self.val_loader = val_loader
self.optimizer = get_optimizer_instance(
torch.optim, "optimizer", CFG, self.model.parameters())
self.lr_scheduler = get_instance(
torch.optim.lr_scheduler, "lr_scheduler", CFG, self.optimizer)
start_time = datetime.now().strftime('%y%m%d%H%M%S')
self.checkpoint_dir = os.path.join(
CFG.save_dir, self.CFG['model']['type'], start_time)
self.writer = tensorboard.SummaryWriter(self.checkpoint_dir)
dir_exists(self.checkpoint_dir)
cudnn.benchmark = True
def train(self):
for epoch in range(1, self.CFG.epochs + 1):
self._train_epoch(epoch)
# break
if self.val_loader is not None and epoch % self.CFG.val_per_epochs == 0:
results = self._valid_epoch(epoch)
logger.info(f'## Info for epoch {epoch} ## ')
for k, v in results.items():
logger.info(f'{str(k):15s}: {v}')
if epoch % self.CFG.save_period == 0:
self._save_checkpoint(epoch)
def _train_epoch(self, epoch):
self.model.train()
wrt_mode = 'train'
self._reset_metrics()
tbar = tqdm(self.train_loader, ncols=160)
tic = time.time()
for img, gt, dgt, edm in tbar:
self.data_time.update(time.time() - tic)
img = img.cuda(non_blocking=True)
gt = gt.cuda(non_blocking=True)
dgt = dgt.cuda(non_blocking=True)
edm = edm.cuda(non_blocking=True)
self.optimizer.zero_grad()
#amp是加速训练用的
if self.CFG.amp is True:
with torch.cuda.amp.autocast(enabled=True):
# pre, _, _, _ = self.model(img)
pre = self.model(img)
loss = self.loss(pre, dgt, edm)
self.scaler.scale(loss).backward()
self.scaler.step(self.optimizer)
self.scaler.update()
else:
pre = self.model(img)
loss = self.loss(pre, dgt)
loss.backward()
self.optimizer.step()
self.total_loss.update(loss.item())
self.batch_time.update(time.time() - tic)
self._metrics_update(
*get_metrics(pre, dgt, threshold=self.CFG.threshold).values())
tbar.set_description(
'TRAIN ({}) | Loss: {:.4f} | AUC {:.4f} F1 {:.4f} Acc {:.4f} Sen {:.4f} Spe {:.4f} Pre {:.4f} IOU {:.4f} |B {:.2f} D {:.2f} |'.format(
epoch, self.total_loss.average, *self._metrics_ave().values(), self.batch_time.average, self.data_time.average))
self.writer.add_scalar(
f'{wrt_mode}/loss', self.total_loss.average, epoch)
for k, v in list(self._metrics_ave().items())[:-1]:
self.writer.add_scalar(f'{wrt_mode}/{k}', v, epoch)
for i, opt_group in enumerate(self.optimizer.param_groups):
self.writer.add_scalar(
f'{wrt_mode}/Learning_rate_{i}', opt_group['lr'], epoch)
self.lr_scheduler.step()
def _valid_epoch(self, epoch):
logger.info('\n###### EVALUATION ######')
self.model.eval()
wrt_mode = 'val'
self._reset_metrics()
tbar = tqdm(self.val_loader, ncols=160)
with torch.no_grad():
for img, gt in tbar:
img = img.cuda(non_blocking=True)
gt = gt.cuda(non_blocking=True)
if self.CFG.amp is True:
with torch.cuda.amp.autocast(enabled=True):
predict = self.model(img)
loss = self.loss(predict, gt)
else:
predict = self.model(img)
loss = self.loss(predict, gt)
self.total_loss.update(loss.item())
self._metrics_update(
*get_metrics(predict, gt, threshold=self.CFG.threshold).values())
tbar.set_description(
'EVAL ({}) | Loss: {:.4f} | AUC {:.4f} F1 {:.4f} Acc {:.4f} Sen {:.4f} Spe {:.4f} Pre {:.4f} IOU {:.4f} |'.format(
epoch, self.total_loss.average, *self._metrics_ave().values()))
self.writer.add_scalar(
f'{wrt_mode}/loss', self.total_loss.average, epoch)
self.writer.add_scalar(
f'{wrt_mode}/loss', self.total_loss.average, epoch)
for k, v in list(self._metrics_ave().items())[:-1]:
self.writer.add_scalar(f'{wrt_mode}/{k}', v, epoch)
log = {
'val_loss': self.total_loss.average,
**self._metrics_ave()
}
return log
def _save_checkpoint(self, epoch):
state = {
'arch': type(self.model).__name__,
'epoch': epoch,
'state_dict': self.model.state_dict(),
'optimizer': self.optimizer.state_dict(),
'config': self.CFG
}
filename = os.path.join(self.checkpoint_dir,
f'checkpoint-epoch{epoch}.pth')
logger.info(f'Saving a checkpoint: {filename} ...')
torch.save(state, filename)
return filename
def _reset_metrics(self):
self.batch_time = AverageMeter()
self.data_time = AverageMeter()
self.total_loss = AverageMeter()
self.auc = AverageMeter()
self.f1 = AverageMeter()
self.acc = AverageMeter()
self.sen = AverageMeter()
self.spe = AverageMeter()
self.pre = AverageMeter()
self.iou = AverageMeter()
self.CCC = AverageMeter()
def _metrics_update(self, auc, f1, acc, sen, spe, pre, iou):
# def _metrics_update(self, f1, acc, sen, spe, pre, iou):
self.auc.update(auc)
self.f1.update(f1)
self.acc.update(acc)
self.sen.update(sen)
self.spe.update(spe)
self.pre.update(pre)
self.iou.update(iou)
def _metrics_ave(self):
return {
"AUC": self.auc.average,
"F1": self.f1.average,
"Acc": self.acc.average,
"Sen": self.sen.average,
"Spe": self.spe.average,
"pre": self.pre.average,
"IOU": self.iou.average
}