-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
130 lines (105 loc) · 4.22 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
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 LambdaLR
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 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 setattr_cls_from_kwargs(cls, kwargs):
# if default values are in the cls,
# overlap the value by kwargs
for key in kwargs.keys():
setattr(cls, key, kwargs[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 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')()
else:
assert Exception("Not Implemented Error")
setattr_cls_from_kwargs(builder, net_conf)
return builder.build
def get_optimizer(net, optim_name='SGD', lr=0.1, momentum=0.9, weight_decay=0, nesterov=True, bn_wd_skip=True):
"""
return optimizer (name) in torch.optim.
If bn_wd_skip, the optimizer does not apply
weight decay regularization on parameters in batch normalization.
"""
decay = []
no_decay = []
for name, param in net.named_parameters():
if ('bn' in name or 'bias' in name) and bn_wd_skip:
no_decay.append(param)
else:
decay.append(param)
per_param_args = [{'params': decay},
{'params': no_decay, 'weight_decay': 0.0}]
if optim_name == 'SGD':
optimizer = torch.optim.SGD(per_param_args, lr=lr, momentum=momentum, weight_decay=weight_decay,
nesterov=nesterov)
elif optim_name == 'AdamW':
optimizer = torch.optim.AdamW(per_param_args, lr=lr, weight_decay=weight_decay)
return optimizer
def get_cosine_schedule_with_warmup(optimizer,
num_training_steps,
num_cycles=7. / 16.,
num_warmup_steps=0,
last_epoch=-1):
"""
Get cosine scheduler (LambdaLR).
if warmup is needed, set num_warmup_steps (int) > 0.
"""
def _lr_lambda(current_step):
"""
_lr_lambda returns a multiplicative factor given an interger parameter epochs.
Decaying criteria: last_epoch
"""
if current_step < num_warmup_steps:
_lr = float(current_step) / float(max(1, num_warmup_steps))
else:
num_cos_steps = float(current_step - num_warmup_steps)
num_cos_steps = num_cos_steps / float(max(1, num_training_steps - num_warmup_steps))
_lr = max(0.0, math.cos(math.pi * num_cycles * num_cos_steps))
return _lr
return LambdaLR(optimizer, _lr_lambda, last_epoch)