-
Notifications
You must be signed in to change notification settings - Fork 13
/
trainer.py
220 lines (189 loc) · 7.82 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
from solver import solver
import datetime
import os.path as osp
import torch
import numpy as np
import tqdm
import fcn
import math
import pytz
import utils
import os
import imageio
import shutil
class Trainer(solver):
def __init__(self, data_loader, opts):
super(Trainer, self).__init__(data_loader, opts)
self.cuda = opts.cuda
self.opts = opts
self.train_loader = data_loader[0]
self.val_loader = data_loader[1]
if opts.mode in ['val', 'demo']:
return
self.timestamp_start = \
datetime.datetime.now(pytz.timezone('America/Bogota'))
self.interval_validate = opts.cfg.get('interval_validate',
len(self.train_loader))
if self.interval_validate is None:
self.interval_validate = len(self.train_loader)
self.out = opts.out
if not osp.exists(self.out):
os.makedirs(self.out)
self.log_headers = [
'epoch',
'iteration',
'train/loss',
'train/acc',
'train/acc_cls',
'train/mean_iu',
'train/fwavacc',
'valid/loss',
'valid/acc',
'valid/acc_cls',
'valid/mean_iu',
'valid/fwavacc',
'elapsed_time',
]
if not osp.exists(osp.join(self.out, 'log.csv')):
with open(osp.join(self.out, 'log.csv'), 'w') as f:
f.write(','.join(self.log_headers) + '\n')
self.epoch = 0
self.iteration = 0
self.max_iter = opts.cfg['max_iteration']
self.best_mean_iu = 0
def validate(self):
# import matplotlib.pyplot as plt
training = self.model.training
self.model.eval()
n_class = len(self.val_loader.dataset.class_names)
val_loss = 0
visualizations = []
label_trues, label_preds = [], []
with torch.no_grad():
for batch_idx, (data, target) in tqdm.tqdm(
enumerate(self.val_loader),
total=len(self.val_loader),
desc='Valid iteration=%d' % self.iteration,
ncols=80,
leave=False):
data, target = data.to(self.cuda), target.to(self.cuda)
score = self.model(data)
loss = self.cross_entropy2d(score, target)
if np.isnan(float(loss.item())):
raise ValueError('loss is nan while validating')
val_loss += float(loss.item()) / len(data)
imgs = data.data.cpu()
lbl_pred = score.data.max(1)[1].cpu().numpy()[:, :, :]
lbl_true = target.data.cpu()
for img, lt, lp in zip(imgs, lbl_true, lbl_pred):
img, lt = self.val_loader.dataset.untransform(img, lt)
label_trues.append(lt)
label_preds.append(lp)
if len(visualizations) < 9:
viz = fcn.utils.visualize_segmentation(lbl_pred=lp,
lbl_true=lt,
img=img,
n_class=n_class)
visualizations.append(viz)
metrics = utils.label_accuracy_score(label_trues, label_preds, n_class)
out = osp.join(self.out, 'visualization_viz')
if not osp.exists(out):
os.makedirs(out)
out_file = osp.join(out, 'iter%012d.jpg' % self.iteration)
img_ = fcn.utils.get_tile_image(visualizations)
imageio.imwrite(out_file, img_)
# plt.imshow(imageio.imread(out_file))
# plt.show()
val_loss /= len(self.val_loader)
with open(osp.join(self.out, 'log.csv'), 'a') as f:
elapsed_time = (
datetime.datetime.now(pytz.timezone('America/Bogota')) -
self.timestamp_start).total_seconds()
log = [self.epoch, self.iteration] + [''] * 5 + \
[val_loss] + list(metrics) + [elapsed_time]
log = map(str, log)
f.write(','.join(log) + '\n')
mean_iu = metrics[2]
is_best = mean_iu > self.best_mean_iu
if is_best:
self.best_mean_iu = mean_iu
torch.save(
{
'epoch': self.epoch,
'iteration': self.iteration,
'arch': self.model.__class__.__name__,
'optim_state_dict': self.optim.state_dict(),
'model_state_dict': self.model.state_dict(),
'best_mean_iu': self.best_mean_iu,
}, osp.join(self.out, 'checkpoint.pth.tar'))
if is_best:
shutil.copy(osp.join(self.out, 'checkpoint.pth.tar'),
osp.join(self.out, 'model_best.pth.tar'))
if training:
self.model.train()
def train_epoch(self):
self.model.train()
n_class = len(self.train_loader.dataset.class_names)
for batch_idx, (data, target) in tqdm.tqdm(
enumerate(self.train_loader),
total=len(self.train_loader),
desc='Train epoch=%d' % self.epoch,
ncols=80,
leave=False):
iteration = batch_idx + self.epoch * len(self.train_loader)
if self.iteration != 0 and (iteration - 1) != self.iteration:
continue # for resuming
self.iteration = iteration
if self.iteration % self.interval_validate == 0:
self.validate()
assert self.model.training
data, target = data.to(self.cuda), target.to(self.cuda)
self.optim.zero_grad()
score = self.model(data)
loss = self.cross_entropy2d(score, target)
loss /= len(data)
if np.isnan(float(loss.item())):
raise ValueError('loss is nan while training')
loss.backward()
self.optim.step()
metrics = []
lbl_pred = score.data.max(1)[1].cpu().numpy()[:, :, :]
lbl_true = target.data.cpu().numpy()
acc, acc_cls, mean_iu, fwavacc = \
utils.label_accuracy_score(
lbl_true, lbl_pred, n_class=n_class)
metrics.append((acc, acc_cls, mean_iu, fwavacc))
metrics = np.mean(metrics, axis=0)
with open(osp.join(self.out, 'log.csv'), 'a') as f:
elapsed_time = (
datetime.datetime.now(pytz.timezone('America/Bogota')) -
self.timestamp_start).total_seconds()
log = [self.epoch, self.iteration] + [loss.item()] + \
metrics.tolist() + [''] * 5 + [elapsed_time]
log = map(str, log)
f.write(','.join(log) + '\n')
if self.iteration >= self.max_iter:
break
def Train(self):
max_epoch = int(math.ceil(1. * self.max_iter / len(self.train_loader)))
for epoch in tqdm.trange(self.epoch, max_epoch, desc='Train',
ncols=80):
self.epoch = epoch
self.train_epoch()
if self.iteration >= self.max_iter:
break
def Test(self):
from utils import run_fromfile
for image, label in self.val_loader:
run_fromfile(self.model,
image,
self.opts.cuda,
self.val_loader.dataset.untransform,
val=True)
def Demo(self):
import glob
from utils import run_fromfile
img_files = sorted(glob.glob('imgs/*.jpg'))
for img in img_files:
run_fromfile(self.model, img, self.opts.cuda,
self.val_loader.dataset.transforms)