-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathutils.py
215 lines (177 loc) · 6.8 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
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
import torch
import csv
import shutil
import pathlib
from copy import deepcopy
from os import remove
from os.path import isfile
from collections import OrderedDict
import models
def load_model(model, ckpt_file, main_gpu, use_cuda: bool=True, strict=True):
r"""Load model for training, resume training, evaluation,
quantization and finding similar kernels for new methods
"""
if use_cuda:
checkpoint = torch.load(ckpt_file, map_location=lambda storage, loc: storage.cuda(main_gpu))
try:
model.load_state_dict(checkpoint, strict)
except:
model.module.load_state_dict(checkpoint, strict)
else:
checkpoint = torch.load(ckpt_file, map_location=lambda storage, loc: storage)
try:
model.load_state_dict(checkpoint)
except:
# create new OrderedDict that does not contain `module.`
new_state_dict = OrderedDict()
for k, v in checkpoint.items():
if k[:7] == 'module.':
name = k[7:] # remove `module.`
else:
name = k[:]
new_state_dict[name] = v
model.load_state_dict(new_state_dict)
return checkpoint
def save_model(arch_name, dataset, state, ckpt_name='ckpt_best.pth'):
r"""Save the model (checkpoint) at the training time
"""
dir_ckpt = pathlib.Path('checkpoint')
dir_path = dir_ckpt / arch_name / dataset
dir_path.mkdir(parents=True, exist_ok=True)
if ckpt_name is None:
ckpt_name = 'ckpt_best.pth'
model_file = dir_path / ckpt_name
torch.save(state, model_file)
def save_summary(arch_name, dataset, name, summary):
r"""Save summary i.e. top-1/5 validation accuracy in each epoch
under `summary` directory
"""
dir_summary = pathlib.Path('summary')
dir_path = dir_summary / 'csv'
dir_path.mkdir(parents=True, exist_ok=True)
file_name = '{}_{}_{}.csv'.format(arch_name, dataset, name)
file_summ = dir_path / file_name
if summary[0] == 0:
with open(file_summ, 'w', newline='') as csv_out:
writer = csv.writer(csv_out)
header_list = ['Epoch', 'Acc@1_train', 'Acc@5_train', 'Acc@1_valid', 'Acc@5_valid']
writer.writerow(header_list)
writer.writerow(summary)
else:
file_temp = dir_path / 'temp.csv'
shutil.copyfile(file_summ, file_temp)
with open(file_temp, 'r', newline='') as csv_in:
with open(file_summ, 'w', newline='') as csv_out:
reader = csv.reader(csv_in)
writer = csv.writer(csv_out)
for row_list in reader:
writer.writerow(row_list)
writer.writerow(summary)
remove(file_temp)
def save_eval(summary):
r"""Save evaluation results i.e. top-1/5 test accuracy in the `eval.csv` file
"""
dir_summary = pathlib.Path('summary')
dir_path = dir_summary / 'csv'
dir_path.mkdir(parents=True, exist_ok=True)
file_summ = dir_path / 'eval.csv'
if not isfile(file_summ):
with open(file_summ, 'w', newline='') as csv_out:
writer = csv.writer(csv_out)
header_list = ['ckpt', 'Acc@1', 'Acc@5']
writer.writerow(header_list)
writer.writerow(summary)
else:
file_temp = 'temp.csv'
shutil.copyfile(file_summ, file_temp)
with open(file_temp, 'r', newline='') as csv_in:
with open(file_summ, 'w', newline='') as csv_out:
reader = csv.reader(csv_in)
writer = csv.writer(csv_out)
for row_list in reader:
writer.writerow(row_list)
writer.writerow(summary)
remove(file_temp)
class AverageMeter(object):
r"""Computes and stores the average and current value
"""
def __init__(self, name, fmt=':f'):
self.name = name
self.fmt = fmt
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
def __str__(self):
fmtstr = '{name} {val' + self.fmt + '} ({avg' + self.fmt + '})'
return fmtstr.format(**self.__dict__)
class ProgressMeter(object):
def __init__(self, num_batches, *meters, prefix=""):
self.batch_fmtstr = self._get_batch_fmtstr(num_batches)
self.meters = meters
self.prefix = prefix
def print(self, batch):
entries = [self.prefix + self.batch_fmtstr.format(batch)]
entries += [str(meter) for meter in self.meters]
print('\t'.join(entries))
def _get_batch_fmtstr(self, num_batches):
num_digits = len(str(num_batches // 1))
fmt = '{:' + str(num_digits) + 'd}'
return '[' + fmt + '/' + fmt.format(num_batches) + ']'
class ScoreMeter(object):
r"""Stores the ground truth and prediction labels
to compute the f1-score (macro)
"""
def __init__(self):
self.label = []
self.prediction = []
self.score = None
def update(self, output, target):
pred = torch.argmax(output, dim=-1)
self.prediction += pred.detach().cpu().tolist()
self.label += target.detach().cpu().tolist()
def set_scheduler(optimizer, args):
r"""Sets the learning rate scheduler
"""
if args.scheduler == 'step':
scheduler = torch.optim.lr_scheduler.StepLR(optimizer, args.step_size, args.gamma)
elif args.scheduler == 'multistep':
scheduler = torch.optim.lr_scheduler.MultiStepLR(optimizer, args.milestones, args.gamma)
elif args.scheduler == 'exp':
scheduler = torch.optim.lr_scheduler.ExponentialLR(optimizer, args.gamma)
elif args.scheduler == 'cosine':
scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, args.step_size)
else:
print('==> unavailable scheduler!! exit...\n')
exit()
return scheduler
def accuracy(output, target, topk=(1,)):
r"""Computes the accuracy over the $k$ top predictions for the specified values of k
"""
with torch.no_grad():
maxk = max(topk)
batch_size = target.size(0)
_, pred = output.topk(maxk, 1, True, True)
pred = pred.t()
correct = pred.eq(target.view(1, -1).expand_as(pred))
res = []
for k in topk:
correct_k = correct[:k].view(-1).float().sum(0, keepdim=True)
res.append(correct_k.mul_(100.0 / batch_size))
return res
def set_arch_name(args):
r"""Set architecture name
"""
arch_name = deepcopy(args.arch)
if args.arch in ['resnet']:
arch_name += str(args.layers)
elif args.arch in ['wideresnet']:
arch_name += '{}_{}'.format(args.layers, int(args.width_mult))
return arch_name