-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
312 lines (247 loc) · 9.65 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
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
import argparse
from datetime import datetime
import os
import logging
import yaml
import math
import sys
import numpy as np
import torch
import torch.optim as optim
from torch.optim.lr_scheduler import _LRScheduler, LambdaLR
class TwoCropTransform:
"""Create two crops of the same image"""
def __init__(self, transform):
self.transform = transform
def __call__(self, x):
return [self.transform(x), self.transform(x)]
def str2bool(v):
if isinstance(v, bool):
return v
if v.lower() in ('yes', 'true', 't', 'y', '1'):
return True
elif v.lower() in ('no', 'false', 'f', 'n', '0'):
return False
else:
raise argparse.ArgumentTypeError('Boolean value expected.')
class AverageMeter(object):
"""Computes and stores the average and current value"""
def __init__(self):
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 time_str(fmt=None):
if fmt is None:
fmt = '%Y-%m-%d_%H:%M:%S'
# time.strftime(format[, t])
return datetime.today().strftime(fmt)
def setup_default_logging(args, default_level=logging.INFO,
format="%(asctime)s - %(levelname)s - %(name)s - %(message)s"):
if 'CIFAR' in args.dataset:
output_dir = os.path.join(args.dataset, f'x{args.num_labels}_seed{args.seed}', args.exp_dir)
else:
output_dir = os.path.join(args.dataset,f'x{args.num_labels}_seed{args.seed}', args.exp_dir)
os.makedirs(output_dir, exist_ok=True)
logger = logging.getLogger('train')
try:
logging.basicConfig( # unlike the root logger, a custom logger can’t be configured using basicConfig()
filename=os.path.join(output_dir, f'{time_str()}.log'),
format=format,
datefmt="%m/%d/%Y %H:%M:%S",
level=default_level)
except:
pass
# print
# file_handler = logging.FileHandler()
console_handler = logging.StreamHandler(sys.stdout)
console_handler.setLevel(default_level)
console_handler.setFormatter(logging.Formatter(format))
logger.addHandler(console_handler)
return logger, output_dir
def accuracy(output, target, topk=(1,)):
"""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.contiguous().view(1, -1).expand_as(pred))
res = []
for k in topk:
correct_k = correct[:k].contiguous().view(-1).float().sum(0, keepdim=True)
res.append(correct_k.mul_(100.0 / batch_size))
return res
def adjust_learning_rate(args, optimizer, epoch):
lr = args.learning_rate
if args.cosine:
eta_min = lr * (args.lr_decay_rate ** 3)
lr = eta_min + (lr - eta_min) * (
1 + math.cos(math.pi * epoch / args.epochs)) / 2
else:
steps = np.sum(epoch > np.asarray(args.lr_decay_epochs))
if steps > 0:
lr = lr * (args.lr_decay_rate ** steps)
for param_group in optimizer.param_groups:
param_group['lr'] = lr
def warmup_learning_rate(args, epoch, batch_id, total_batches, optimizer):
if args.warm and epoch <= args.warm_epochs:
p = (batch_id + (epoch - 1) * total_batches) / \
(args.warm_epochs * total_batches)
lr = args.warmup_from + p * (args.warmup_to - args.warmup_from)
for param_group in optimizer.param_groups:
param_group['lr'] = lr
class WarmupCosineLrScheduler(_LRScheduler):
def __init__(
self,
optimizer,
max_iter,
warmup_iter,
warmup_ratio=5e-4,
warmup='exp',
last_epoch=-1,
):
self.max_iter = max_iter
self.warmup_iter = warmup_iter
self.warmup_ratio = warmup_ratio
self.warmup = warmup
super(WarmupCosineLrScheduler, self).__init__(optimizer, last_epoch)
def get_lr(self):
ratio = self.get_lr_ratio()
lrs = [ratio * lr for lr in self.base_lrs]
return lrs
def get_lr_ratio(self):
if self.last_epoch < self.warmup_iter:
ratio = self.get_warmup_ratio()
else:
real_iter = self.last_epoch - self.warmup_iter
real_max_iter = self.max_iter - self.warmup_iter
ratio = np.cos((7 * np.pi * real_iter) / (16 * real_max_iter))
# ratio = 0.5 * (1. + np.cos(np.pi * real_iter / real_max_iter))
return ratio
def get_warmup_ratio(self):
assert self.warmup in ('linear', 'exp')
alpha = self.last_epoch / self.warmup_iter
if self.warmup == 'linear':
ratio = self.warmup_ratio + (1 - self.warmup_ratio) * alpha
elif self.warmup == 'exp':
ratio = self.warmup_ratio ** (1. - alpha)
return ratio
def set_optimizer(args, model):
optimizer = optim.SGD(model.parameters(),
lr=args.learning_rate,
momentum=args.momentum,
weight_decay=args.weight_decay)
return optimizer
def print_args(args):
print(f"args: {args}")
for k in args.__dict__:
print(f"{k}: {getattr(args, k)}")
def save_model(model, optimizer, opt, epoch, save_file):
print('==> Saving...')
state = {
'opt': opt,
'model': model.state_dict(),
'optimizer': optimizer.state_dict(),
'epoch': epoch,
}
torch.save(state, save_file)
del state
def over_write_args_from_file(args, yml):
if yml == '':
return
with open(yml, 'r', encoding='utf-8') as f:
dic = yaml.load(f.read(), Loader=yaml.Loader)
for k in dic:
setattr(args, k, dic[k])
def marge_args_from_file(args, yml):
if yml == '':
return
with open(yml, 'r', encoding='utf-8') as f:
dic = yaml.load(f.read(), Loader=yaml.Loader)
for k in dic:
if k not in args.__dict__:
setattr(args, k, dic[k])
def setattr_cls_from_kwargs(cls, kwargs):
# if default values are in the cls,
# overlap the value by kwargs
for key in kwargs.keys():
if hasattr(cls, key):
print(f"{key} in {cls} is overlapped by kwargs: {getattr(cls, key)} -> {kwargs[key]}")
setattr(cls, key, kwargs[key])
def test_setattr_cls_from_kwargs():
class _test_cls:
def __init__(self):
self.a = 1
self.b = 'hello'
test_cls = _test_cls()
config = {'a': 3, 'b': 'change_hello', 'c': 5}
setattr_cls_from_kwargs(test_cls, config)
for key in config.keys():
print(f"{key}:\t {getattr(test_cls, key)}")
def net_builder(net_name, from_name: bool, net_conf=None, is_remix=False):
"""
return **class** of backbone network (not instance).
Args
net_name: 'WideResNet' or network names in torchvision.models
from_name: If True, net_buidler takes models in torch.vision models. Then, net_conf is ignored.
net_conf: When from_name is False, net_conf is the configuration of backbone network (now, only WRN is supported).
"""
if from_name:
import torchvision.models as models
model_name_list = sorted(name for name in models.__dict__
if name.islower() and not name.startswith("__")
and callable(models.__dict__[name]))
if net_name not in model_name_list:
if net_name == 'r2plus1d_18':
from torchvision.models.video import r2plus1d_18
return r2plus1d_18
else:
assert Exception(f"[!] Networks\' Name is wrong, check net config, \
expected: {model_name_list} \
received: {net_name}")
else:
return models.__dict__[net_name]
else:
if net_name == 'WideResNet':
import models.nets.wrn as net
builder = getattr(net, 'build_WideResNet')()
elif net_name == 'WideResNetVar':
import models.nets.wrn_var as net
builder = getattr(net, 'build_WideResNetVar')()
elif net_name == 'ResNet50':
import models.nets.resnet as net
builder = getattr(net, 'build_ResNet50')(is_remix)
elif net_name == 'ResNet18':
import models.nets.resnet as net
builder = getattr(net, 'build_ResNet18')(is_remix)
else:
assert Exception("Not Implemented Error")
setattr_cls_from_kwargs(builder, net_conf)
return builder.build
def test_net_builder(net_name, from_name, net_conf=None):
builder = net_builder(net_name, from_name, net_conf)
print(f"net_name: {net_name}, from_name: {from_name}, net_conf: {net_conf}")
print(builder)
def get_logger(name, save_path=None, level='INFO'):
logger = logging.getLogger(name)
logger.setLevel(getattr(logging, level))
log_format = logging.Formatter('[%(asctime)s %(levelname)s] %(message)s')
streamHandler = logging.StreamHandler()
streamHandler.setFormatter(log_format)
logger.addHandler(streamHandler)
if not save_path is None:
os.makedirs(save_path, exist_ok=True)
fileHandler = logging.FileHandler(os.path.join(save_path, 'log.txt'))
fileHandler.setFormatter(log_format)
logger.addHandler(fileHandler)
return logger
def count_parameters(model):
return sum(p.numel() for p in model.parameters() if p.requires_grad)