This repository has been archived by the owner on Jan 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 129
/
utils.py
162 lines (130 loc) · 4.36 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
# Copyright (c) 2020 Uber Technologies, Inc.
# Please check LICENSE for more detail
import numpy as np
import sys
import cv2
import os
import torch
from torch import optim
def index_dict(data, idcs):
returns = dict()
for key in data:
returns[key] = data[key][idcs]
return returns
def rotate(xy, theta):
st, ct = torch.sin(theta), torch.cos(theta)
rot_mat = xy.new().resize_(len(xy), 2, 2)
rot_mat[:, 0, 0] = ct
rot_mat[:, 0, 1] = -st
rot_mat[:, 1, 0] = st
rot_mat[:, 1, 1] = ct
xy = torch.matmul(rot_mat, xy.unsqueeze(2)).view(len(xy), 2)
return xy
def merge_dict(ds, dt):
for key in ds:
dt[key] = ds[key]
return
class Logger(object):
def __init__(self, log):
self.terminal = sys.stdout
self.log = open(log, "a")
def write(self, message):
self.terminal.write(message)
self.log.write(message)
self.log.flush()
def flush(self):
pass
def load_pretrain(net, pretrain_dict):
state_dict = net.state_dict()
for key in pretrain_dict.keys():
if key in state_dict and (pretrain_dict[key].size() == state_dict[key].size()):
value = pretrain_dict[key]
if not isinstance(value, torch.Tensor):
value = value.data
state_dict[key] = value
net.load_state_dict(state_dict)
def gpu(data):
"""
Transfer tensor in `data` to gpu recursively
`data` can be dict, list or tuple
"""
if isinstance(data, list) or isinstance(data, tuple):
data = [gpu(x) for x in data]
elif isinstance(data, dict):
data = {key:gpu(_data) for key,_data in data.items()}
elif isinstance(data, torch.Tensor):
data = data.contiguous().cuda(non_blocking=True)
return data
def to_long(data):
if isinstance(data, dict):
for key in data.keys():
data[key] = to_long(data[key])
if isinstance(data, list) or isinstance(data, tuple):
data = [to_long(x) for x in data]
if torch.is_tensor(data) and data.dtype == torch.int16:
data = data.long()
return data
class Optimizer(object):
def __init__(self, params, config, coef=None):
if not (isinstance(params, list) or isinstance(params, tuple)):
params = [params]
if coef is None:
coef = [1.0] * len(params)
else:
if isinstance(coef, list) or isinstance(coef, tuple):
assert len(coef) == len(params)
else:
coef = [coef] * len(params)
self.coef = coef
param_groups = []
for param in params:
param_groups.append({"params": param, "lr": 0})
opt = config["opt"]
assert opt == "sgd" or opt == "adam"
if opt == "sgd":
self.opt = optim.SGD(
param_groups, momentum=config["momentum"], weight_decay=config["wd"]
)
elif opt == "adam":
self.opt = optim.Adam(param_groups, weight_decay=0)
self.lr_func = config["lr_func"]
if "clip_grads" in config:
self.clip_grads = config["clip_grads"]
self.clip_low = config["clip_low"]
self.clip_high = config["clip_high"]
else:
self.clip_grads = False
def zero_grad(self):
self.opt.zero_grad()
def step(self, epoch):
if self.clip_grads:
self.clip()
lr = self.lr_func(epoch)
for i, param_group in enumerate(self.opt.param_groups):
param_group["lr"] = lr * self.coef[i]
self.opt.step()
return lr
def clip(self):
low, high = self.clip_low, self.clip_high
params = []
for param_group in self.opt.param_groups:
params += list(filter(lambda p: p.grad is not None, param_group["params"]))
for p in params:
mask = p.grad.data < low
p.grad.data[mask] = low
mask = p.grad.data > high
p.grad.data[mask] = high
def load_state_dict(self, opt_state):
self.opt.load_state_dict(opt_state)
class StepLR:
def __init__(self, lr, lr_epochs):
assert len(lr) - len(lr_epochs) == 1
self.lr = lr
self.lr_epochs = lr_epochs
def __call__(self, epoch):
idx = 0
for lr_epoch in self.lr_epochs:
if epoch < lr_epoch:
break
idx += 1
return self.lr[idx]