-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsolver.py
63 lines (53 loc) · 2.13 KB
/
solver.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
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.
from torch.nn import functional as F
import flashy
class Solver(flashy.BaseSolver):
def __init__(self, cfg, model, loaders, optim):
super().__init__()
self.h = cfg
self.model = model
self.loaders = loaders
self.optim = optim
self.register_stateful('model', 'optim')
self.init_tensorboard()
def run(self):
self.logger.info('Log dir: %s', self.folder)
self.restore()
self.log_hyperparams(self.h)
for epoch in range(self.epoch, self.h.epochs + 1):
self.run_stage("train", self.do_train_valid, train=True)
self.run_stage("valid", self.do_train_valid, train=False)
self.commit()
def get_formatter(self, stage_name: str):
return flashy.Formatter({
'acc': '.1%',
'loss': '.5f',
})
def do_train_valid(self, train: bool = True):
self.logger.info('-' * 80)
self.logger.info(f'Starting {self.current_stage} stage...')
loader = self.loaders["train" if train else "valid"]
lp = self.log_progress(self.current_stage, loader, total=len(loader), updates=self.h.log_updates)
average = flashy.averager()
for idx, batch in enumerate(lp):
img, label = [x.to(self.h.device) for x in batch]
est = self.model(img)
loss = F.cross_entropy(est, label)
acc = (est.argmax(dim=-1).float() == label).float().mean()
if train:
loss.backward()
flashy.distrib.sync_model(self.model)
self.optim.step()
self.optim.zero_grad()
metrics = average({'acc': acc, 'loss': loss})
lp.update(**metrics)
if idx == 0:
self.log_image(self.current_stage, 'sample', img[0])
if idx > 20:
break
metrics = flashy.distrib.average_metrics(metrics, len(loader))
return metrics