-
Notifications
You must be signed in to change notification settings - Fork 95
/
trainer.py
331 lines (277 loc) · 11.6 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
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
import atexit
import os
import os.path as osp
import shutil
import signal
import subprocess
import threading
import time
from timeit import default_timer as timer
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import torch
import torch.nn.functional as F
from skimage import io
from tensorboardX import SummaryWriter
from lcnn.config import C, M
from lcnn.utils import recursive_to
class Trainer(object):
def __init__(self, device, model, optimizer, train_loader, val_loader, out):
self.device = device
self.model = model
self.optim = optimizer
self.train_loader = train_loader
self.val_loader = val_loader
self.batch_size = C.model.batch_size
self.validation_interval = C.io.validation_interval
self.out = out
if not osp.exists(self.out):
os.makedirs(self.out)
self.run_tensorboard()
time.sleep(1)
self.epoch = 0
self.iteration = 0
self.max_epoch = C.optim.max_epoch
self.lr_decay_epoch = C.optim.lr_decay_epoch
self.num_stacks = C.model.num_stacks
self.mean_loss = self.best_mean_loss = 1e1000
self.loss_labels = None
self.avg_metrics = None
self.metrics = np.zeros(0)
def run_tensorboard(self):
board_out = osp.join(self.out, "tensorboard")
if not osp.exists(board_out):
os.makedirs(board_out)
self.writer = SummaryWriter(board_out)
os.environ["CUDA_VISIBLE_DEVICES"] = ""
p = subprocess.Popen(
["tensorboard", f"--logdir={board_out}", f"--port={C.io.tensorboard_port}"]
)
def killme():
os.kill(p.pid, signal.SIGTERM)
atexit.register(killme)
def _loss(self, result):
losses = result["losses"]
# Don't move loss label to other place.
# If I want to change the loss, I just need to change this function.
if self.loss_labels is None:
self.loss_labels = ["sum"] + list(losses[0].keys())
self.metrics = np.zeros([self.num_stacks, len(self.loss_labels)])
print()
print(
"| ".join(
["progress "]
+ list(map("{:7}".format, self.loss_labels))
+ ["speed"]
)
)
with open(f"{self.out}/loss.csv", "a") as fout:
print(",".join(["progress"] + self.loss_labels), file=fout)
total_loss = 0
for i in range(self.num_stacks):
for j, name in enumerate(self.loss_labels):
if name == "sum":
continue
if name not in losses[i]:
assert i != 0
continue
loss = losses[i][name].mean()
self.metrics[i, 0] += loss.item()
self.metrics[i, j] += loss.item()
total_loss += loss
return total_loss
def validate(self):
tprint("Running validation...", " " * 75)
training = self.model.training
self.model.eval()
viz = osp.join(self.out, "viz", f"{self.iteration * M.batch_size_eval:09d}")
npz = osp.join(self.out, "npz", f"{self.iteration * M.batch_size_eval:09d}")
osp.exists(viz) or os.makedirs(viz)
osp.exists(npz) or os.makedirs(npz)
total_loss = 0
self.metrics[...] = 0
with torch.no_grad():
for batch_idx, (image, meta, target) in enumerate(self.val_loader):
input_dict = {
"image": recursive_to(image, self.device),
"meta": recursive_to(meta, self.device),
"target": recursive_to(target, self.device),
"mode": "validation",
}
result = self.model(input_dict)
total_loss += self._loss(result)
H = result["preds"]
for i in range(H["jmap"].shape[0]):
index = batch_idx * M.batch_size_eval + i
np.savez(
f"{npz}/{index:06}.npz",
**{k: v[i].cpu().numpy() for k, v in H.items()},
)
if index >= 20:
continue
self._plot_samples(i, index, H, meta, target, f"{viz}/{index:06}")
self._write_metrics(len(self.val_loader), total_loss, "validation", True)
self.mean_loss = total_loss / len(self.val_loader)
torch.save(
{
"iteration": self.iteration,
"arch": self.model.__class__.__name__,
"optim_state_dict": self.optim.state_dict(),
"model_state_dict": self.model.state_dict(),
"best_mean_loss": self.best_mean_loss,
},
osp.join(self.out, "checkpoint_latest.pth"),
)
shutil.copy(
osp.join(self.out, "checkpoint_latest.pth"),
osp.join(npz, "checkpoint.pth"),
)
if self.mean_loss < self.best_mean_loss:
self.best_mean_loss = self.mean_loss
shutil.copy(
osp.join(self.out, "checkpoint_latest.pth"),
osp.join(self.out, "checkpoint_best.pth"),
)
if training:
self.model.train()
def train_epoch(self):
self.model.train()
time = timer()
for batch_idx, (image, meta, target) in enumerate(self.train_loader):
self.optim.zero_grad()
self.metrics[...] = 0
input_dict = {
"image": recursive_to(image, self.device),
"meta": recursive_to(meta, self.device),
"target": recursive_to(target, self.device),
"mode": "training",
}
result = self.model(input_dict)
loss = self._loss(result)
if np.isnan(loss.item()):
raise ValueError("loss is nan while training")
loss.backward()
self.optim.step()
if self.avg_metrics is None:
self.avg_metrics = self.metrics
else:
self.avg_metrics = self.avg_metrics * 0.9 + self.metrics * 0.1
self.iteration += 1
self._write_metrics(1, loss.item(), "training", do_print=False)
if self.iteration % 4 == 0:
tprint(
f"{self.epoch:03}/{self.iteration * self.batch_size // 1000:04}k| "
+ "| ".join(map("{:.5f}".format, self.avg_metrics[0]))
+ f"| {4 * self.batch_size / (timer() - time):04.1f} "
)
time = timer()
num_images = self.batch_size * self.iteration
if num_images % self.validation_interval == 0 or num_images == 600:
self.validate()
time = timer()
def _write_metrics(self, size, total_loss, prefix, do_print=False):
for i, metrics in enumerate(self.metrics):
for label, metric in zip(self.loss_labels, metrics):
self.writer.add_scalar(
f"{prefix}/{i}/{label}", metric / size, self.iteration
)
if i == 0 and do_print:
csv_str = (
f"{self.epoch:03}/{self.iteration * self.batch_size:07},"
+ ",".join(map("{:.11f}".format, metrics / size))
)
prt_str = (
f"{self.epoch:03}/{self.iteration * self.batch_size // 1000:04}k| "
+ "| ".join(map("{:.5f}".format, metrics / size))
)
with open(f"{self.out}/loss.csv", "a") as fout:
print(csv_str, file=fout)
pprint(prt_str, " " * 7)
self.writer.add_scalar(
f"{prefix}/total_loss", total_loss / size, self.iteration
)
return total_loss
def _plot_samples(self, i, index, result, meta, target, prefix):
fn = self.val_loader.dataset.filelist[index][:-10].replace("_a0", "") + ".png"
img = io.imread(fn)
imshow(img), plt.savefig(f"{prefix}_img.jpg"), plt.close()
mask_result = result["jmap"][i].cpu().numpy()
mask_target = target["jmap"][i].cpu().numpy()
for ch, (ia, ib) in enumerate(zip(mask_target, mask_result)):
imshow(ia), plt.savefig(f"{prefix}_mask_{ch}a.jpg"), plt.close()
imshow(ib), plt.savefig(f"{prefix}_mask_{ch}b.jpg"), plt.close()
line_result = result["lmap"][i].cpu().numpy()
line_target = target["lmap"][i].cpu().numpy()
imshow(line_target), plt.savefig(f"{prefix}_line_a.jpg"), plt.close()
imshow(line_result), plt.savefig(f"{prefix}_line_b.jpg"), plt.close()
def draw_vecl(lines, sline, juncs, junts, fn):
imshow(img)
if len(lines) > 0 and not (lines[0] == 0).all():
for i, ((a, b), s) in enumerate(zip(lines, sline)):
if i > 0 and (lines[i] == lines[0]).all():
break
plt.plot([a[1], b[1]], [a[0], b[0]], c=c(s), linewidth=4)
if not (juncs[0] == 0).all():
for i, j in enumerate(juncs):
if i > 0 and (i == juncs[0]).all():
break
plt.scatter(j[1], j[0], c="red", s=64, zorder=100)
if junts is not None and len(junts) > 0 and not (junts[0] == 0).all():
for i, j in enumerate(junts):
if i > 0 and (i == junts[0]).all():
break
plt.scatter(j[1], j[0], c="blue", s=64, zorder=100)
plt.savefig(fn), plt.close()
junc = meta[i]["junc"].cpu().numpy() * 4
jtyp = meta[i]["jtyp"].cpu().numpy()
juncs = junc[jtyp == 0]
junts = junc[jtyp == 1]
rjuncs = result["juncs"][i].cpu().numpy() * 4
rjunts = None
if "junts" in result:
rjunts = result["junts"][i].cpu().numpy() * 4
lpre = meta[i]["lpre"].cpu().numpy() * 4
vecl_target = meta[i]["lpre_label"].cpu().numpy()
vecl_result = result["lines"][i].cpu().numpy() * 4
score = result["score"][i].cpu().numpy()
lpre = lpre[vecl_target == 1]
draw_vecl(lpre, np.ones(lpre.shape[0]), juncs, junts, f"{prefix}_vecl_a.jpg")
draw_vecl(vecl_result, score, rjuncs, rjunts, f"{prefix}_vecl_b.jpg")
def train(self):
plt.rcParams["figure.figsize"] = (24, 24)
# if self.iteration == 0:
# self.validate()
epoch_size = len(self.train_loader)
start_epoch = self.iteration // epoch_size
for self.epoch in range(start_epoch, self.max_epoch):
if self.epoch == self.lr_decay_epoch:
self.optim.param_groups[0]["lr"] /= 10
self.train_epoch()
cmap = plt.get_cmap("jet")
norm = mpl.colors.Normalize(vmin=0.4, vmax=1.0)
sm = plt.cm.ScalarMappable(cmap=cmap, norm=norm)
sm.set_array([])
def c(x):
return sm.to_rgba(x)
def imshow(im):
plt.close()
plt.tight_layout()
plt.imshow(im)
plt.colorbar(sm, fraction=0.046)
plt.xlim([0, im.shape[0]])
plt.ylim([im.shape[0], 0])
def tprint(*args):
"""Temporarily prints things on the screen"""
print("\r", end="")
print(*args, end="")
def pprint(*args):
"""Permanently prints things on the screen"""
print("\r", end="")
print(*args)
def _launch_tensorboard(board_out, port, out):
os.environ["CUDA_VISIBLE_DEVICES"] = ""
p = subprocess.Popen(["tensorboard", f"--logdir={board_out}", f"--port={port}"])
def kill():
os.kill(p.pid, signal.SIGTERM)
atexit.register(kill)