-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathutils.py
78 lines (68 loc) · 2.51 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
import math
import torch
import shutil
import time
import os
import random
from easydict import EasyDict as edict
import yaml
import numpy as np
class AverageMeter(object):
""" Computes ans 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 adjust_learning_rate(optimizer, iters, base_lr, policy_parameter, policy='step', multiple=[1]):
if policy == 'fixed':
lr = base_lr
elif policy == 'step':
lr = base_lr * (policy_parameter['gamma'] ** (iters // policy_parameter['step_size']))
elif policy == 'exp':
lr = base_lr * (policy_parameter['gamma'] ** iters)
elif policy == 'inv':
lr = base_lr * ((1 + policy_parameter['gamma'] * iters) ** (-policy_parameter['power']))
elif policy == 'multistep':
lr = base_lr
for stepvalue in policy_parameter['stepvalue']:
if iters >= stepvalue:
lr *= policy_parameter['gamma']
else:
break
elif policy == 'poly':
lr = base_lr * ((1 - iters * 1.0 / policy_parameter['max_iter']) ** policy_parameter['power'])
elif policy == 'sigmoid':
lr = base_lr * (1.0 / (1 + math.exp(-policy_parameter['gamma'] * (iters - policy_parameter['stepsize']))))
elif policy == 'multistep-poly':
lr = base_lr
stepstart = 0
stepend = policy_parameter['max_iter']
for stepvalue in policy_parameter['stepvalue']:
if iters >= stepvalue:
lr *= policy_parameter['gamma']
stepstart = stepvalue
else:
stepend = stepvalue
break
lr = max(lr * policy_parameter['gamma'], lr * (1 - (iters - stepstart) * 1.0 / (stepend - stepstart)) ** policy_parameter['power'])
for i, param_group in enumerate(optimizer.param_groups):
param_group['lr'] = lr * multiple[i]
return lr
def save_checkpoint(state, is_best, filename='checkpoint.pth.tar'):
torch.save(state, filename + '_latest.pth.tar')
if is_best:
shutil.copyfile(filename + '_latest.pth.tar', filename + '_best.pth.tar')
def Config(filename):
with open(filename, 'r') as f:
parser = edict(yaml.load(f))
for x in parser:
print '{}: {}'.format(x, parser[x])
return parser