-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
220 lines (188 loc) · 6.11 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
import os
import torch
import torch.nn as nn
import numpy as np
import copy
from collections import OrderedDict
import torch.distributed as dist
def to_tensor(arr, device):
if isinstance(arr, torch.Tensor):
return arr.to(device)
else:
return torch.FloatTensor(arr).to(device)
def to_ndarray(arr):
if isinstance(arr, np.ndarray):
return arr
else:
return arr.data.cpu().numpy()
def to_8bit(x):
return (255 * np.clip(to_ndarray(x), 0, 1)).astype(np.uint8)
def img2mse(x, y):
return torch.mean((x - y)**2)
def mse2psnr(x):
return -10. * torch.log(x) / torch.log(to_tensor([10.], get_rank()))
def save_onnx(model, onnx_path, dummy_input):
model = copy.deepcopy(model)
model.eval()
if hasattr(model, 'module'):
model = model.module
model.eval()
torch.onnx.export(
model.cpu(),
dummy_input.cpu(),
onnx_path,
export_params=True,
opset_version=11,
do_constant_folding=True,
keep_initializers_as_inputs=False,
input_names=['input'],
output_names=['output'],
dynamic_axes={
'input': {0: 'batch_size'},
'output': {0: 'batch_size'}
}
)
del model
def save_ml(model, onnx_path, dummy_input):
import coremltools as ct
model = copy.deepcopy(model)
if hasattr(model, 'module'):
model = model.module
model.eval()
traced_model = torch.jit.trace(model, dummy_input)
out = traced_model(dummy_input)
model = ct.convert(
traced_model,
convert_to="mlprogram",
inputs=[ct.TensorType(shape=dummy_input.shape)]
)
model.save(onnx_path)
del model
def save_tflite(model, onnx_path, dummy_input):
from tinynn.converter import TFLiteConverter
model = copy.deepcopy(model)
model.cpu()
if hasattr(model, 'module'):
model = model.module
model.eval()
converter = TFLiteConverter(model, dummy_input.cpu(), onnx_path)
converter.convert()
del model
def check_onnx(model, onnx_path, dummy_input):
r"""Refer to https://pytorch.org/tutorials/advanced/super_resolution_with_onnxruntime.html
"""
import onnx, onnxruntime
model = copy.deepcopy(model)
model.eval()
if hasattr(model, 'module'):
model = model.module
model.eval()
model, dummy_input = model.cpu(), dummy_input.cpu()
torch_out = model(dummy_input)[0]
onnx_model = onnx.load(onnx_path)
onnx.checker.check_model(onnx_model)
ort_session = onnxruntime.InferenceSession(
onnx_path,
providers=['TensorrtExecutionProvider', 'CUDAExecutionProvider', 'CPUExecutionProvider']
)
# compute ONNX Runtime output prediction
ort_inputs = {ort_session.get_inputs()[0].name: to_ndarray(dummy_input)}
ort_outs = ort_session.run(None, ort_inputs)
# compare ONNX Runtime and PyTorch results
np.testing.assert_allclose(
to_ndarray(torch_out),
ort_outs[0],
rtol=1e-03,
atol=1e-05
)
print("Exported model has been tested with ONNXRuntime, and the result looks good!")
def undataparallel(input):
'''remove the module. prefix caused by nn.DataParallel'''
if isinstance(input, nn.Module):
model = input
if hasattr(model, 'module'):
model = model.module
return model
elif isinstance(input, OrderedDict):
state_dict = input
w = OrderedDict()
for k, v in state_dict.items():
if k.startswith('module.'):
assert len(k.split('module.')) == 2
k = k.split('module.')[1]
w[k] = v
return w
else:
raise NotImplementedError
def mkdirs(*dirs, exist_ok=True):
for d in dirs:
os.makedirs(d, mode=0o777, exist_ok=exist_ok)
def cache_code(root_dir, dest_dir):
cmd = f"rsync -az --exclude='logs/*' --exclude='model/teacher/*' --exclude='dataset/*' {root_dir} {dest_dir}"
os.system(cmd)
#------------------ ddp utils ------------------#
def setup_for_distributed(is_master):
"""
This function disables printing when not in master process
"""
import builtins as __builtin__
builtin_print = __builtin__.print
def print(*args, **kwargs):
force = kwargs.pop('force', False)
if is_master or force:
builtin_print(*args, **kwargs)
__builtin__.print = print
def is_dist_avail_and_initialized():
if not dist.is_available():
return False
if not dist.is_initialized():
return False
return True
def get_world_size():
if not is_dist_avail_and_initialized():
return 1
return dist.get_world_size()
def get_rank():
if not is_dist_avail_and_initialized():
return 0
return dist.get_rank()
def is_main_process():
return get_rank() == 0
def init_distributed_mode(args):
if 'RANK' in os.environ and 'WORLD_SIZE' in os.environ:
args.rank = int(os.environ["RANK"])
args.world_size = int(os.environ['WORLD_SIZE'])
args.gpu = int(os.environ['LOCAL_RANK'])
print( args.gpu, '.....')
elif 'SLURM_PROCID' in os.environ:
args.rank = int(os.environ['SLURM_PROCID'])
args.gpu = args.rank % torch.cuda.device_count()
else:
print('Not using distributed mode')
args.distributed = False
return
args.distributed = True
torch.cuda.set_device(args.gpu)
args.dist_backend = 'nccl'
print(
'| distributed init (rank {})'.format(args.rank), flush=True
)
torch.distributed.init_process_group(
backend=args.dist_backend,
world_size=args.world_size,
rank=args.rank
)
torch.distributed.barrier()
setup_for_distributed(args.rank == 0)
def main_process(func):
def wrapper(*args, **kwargs):
if is_main_process():
return func(*args, **kwargs)
else :
pass
return wrapper
def set_epoch_num(global_step, num_iters, batch_size, num_pseudo, world_size):
steps_per_epoch = num_pseudo // (world_size * batch_size)
iters_to_run = num_iters - global_step
num_epochs = iters_to_run // steps_per_epoch
return num_epochs