-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
142 lines (111 loc) · 4.3 KB
/
utils.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
import os
import logging
import csv
import torch
from torch.optim import Optimizer
from torch.utils.tensorboard import SummaryWriter
import ignite.distributed as idist
class Logger(object):
def __init__(self, logdir, prefix='', resume=None):
self.logdir = logdir
self.rank = idist.get_rank()
self.csv_msg = None
self.prefix = prefix
handlers = [logging.StreamHandler(os.sys.stdout)]
if logdir is not None and self.rank == 0:
if resume is None:
try:
os.makedirs(logdir)
except FileExistsError:
print('Warning: log file already exists!')
handlers.append(logging.FileHandler(os.path.join(logdir, f'log_{prefix}.txt')))
self.writer = SummaryWriter(log_dir=logdir)
else:
self.writer = None
logging.basicConfig(format=f"[%(asctime)s ({self.rank})] %(message)s",
level=logging.INFO,
handlers=handlers)
logging.info(' '.join(os.sys.argv))
def log_msg(self, msg):
if idist.get_rank() > 0:
return
logging.info(msg)
def log(self, engine, global_step, print_msg=True, **kwargs):
if idist.get_rank() > 0:
return
msg = f'[epoch {engine.state.epoch}] [iter {engine.state.iteration}]'
for k, v in kwargs.items():
if isinstance(v, torch.Tensor):
v = v.item()
if type(v) is float:
msg += f' [{k} {v:.3f}]'
else:
msg += f' [{k} {v}]'
if self.writer is not None:
self.writer.add_scalar(k, v, global_step)
if print_msg:
logging.info(msg)
def log_csv(self, vals):
self.csv_msg = vals
def save_csv(self, prefix=''):
csv_file = os.path.join(self.logdir, 'result.csv')
with open(csv_file, 'a', encoding='UTF8', newline='') as f:
writer = csv.writer(f)
writer.writerow([prefix] + self.csv_msg)
def save(self, engine, record_epoch=True, **kwargs):
if idist.get_rank() > 0:
return
state = {}
for k, v in kwargs.items():
if isinstance(v, torch.nn.parallel.DistributedDataParallel):
v = v.module
if hasattr(v, 'state_dict'):
state[k] = v.state_dict()
continue
if type(v) is list and hasattr(v[0], 'state_dict'):
state[k] = [x.state_dict() for x in v]
continue
state[k] = v # record other info
if record_epoch:
filename = f'ckpt-{self.prefix}-{engine.state.epoch}.pth'
else:
filename = f'ckpt-{self.prefix}-best.pth'
torch.save(state, os.path.join(self.logdir, filename))
print(f'Checkpoint saved to {os.path.join(self.logdir, filename)}')
class AverageMeter(object):
def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.cnt = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.cnt += n
self.avg = self.sum / self.cnt
def accuracy(output, target, topk=(1,5)):
with torch.no_grad():
maxk = max(topk)
batch_size = target.size(0)
_, pred = output.topk(maxk, dim=1) # top-k index: size (B, k)
pred = pred.t() # size (k, B)
correct = pred.eq(target.view(1, -1).expand_as(pred))
acc = []
for k in topk:
correct_k = correct[:k].float().sum()
acc.append(correct_k * 100.0 / batch_size)
if len(acc) == 1:
return acc[0]
else:
return acc
class LambdaLR:
"""https://github.com/eriklindernoren/PyTorch-GAN/blob/36d3c77e5ff20ebe0aeefd322326a134a279b93e/implementations/unit/models.py"""
def __init__(self, n_epochs, offset, decay_start_epoch):
assert (n_epochs - decay_start_epoch) > 0, "Decay must start before the training session ends!"
self.n_epochs = n_epochs
self.offset = offset
self.decay_start_epoch = decay_start_epoch
def step(self, epoch):
return 1.0 - max(0, epoch + self.offset - self.decay_start_epoch) / (self.n_epochs - self.decay_start_epoch)