-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathutil.py
executable file
·133 lines (100 loc) · 3.5 KB
/
util.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
import os
import json
import functools
import time
import socket
from pathlib import Path
from datetime import datetime
from itertools import repeat
from collections import OrderedDict
import numpy as np
import psutil
import humanize
def replace_nested_dict_item(obj, key, replace_value):
for k, v in obj.items():
if isinstance(v, dict):
obj[k] = replace_nested_dict_item(v, key, replace_value)
if key in obj:
obj[key] = replace_value
return obj
def state_dict_data_parallel_fix(load_state_dict, curr_state_dict):
load_keys = list(load_state_dict.keys())
curr_keys = list(curr_state_dict.keys())
redo_dp = False
undo_dp = False
if not curr_keys[0].startswith('module.') and load_keys[0].startswith('module.'):
undo_dp = True
elif curr_keys[0].startswith('module.') and not load_keys[0].startswith('module.'):
redo_dp = True
if undo_dp:
from collections import OrderedDict
new_state_dict = OrderedDict()
for k, v in load_state_dict.items():
name = k[7:] # remove `module.`
new_state_dict[name] = v
# load params
elif redo_dp:
from collections import OrderedDict
new_state_dict = OrderedDict()
for k, v in load_state_dict.items():
name = 'module.' + k # remove `module.`
new_state_dict[name] = v
else:
new_state_dict = load_state_dict
return new_state_dict
def read_json(fname):
with fname.open('rt') as handle:
return json.load(handle, object_hook=OrderedDict)
def write_json(content, fname):
with fname.open('wt') as handle:
json.dump(content, handle, indent=4, sort_keys=False)
def inf_loop(data_loader):
''' wrapper function for endless data loader. '''
for loader in repeat(data_loader):
yield from loader
def memory_summary():
vmem = psutil.virtual_memory()
msg = (
f">>> Currently using {vmem.percent}% of system memory "
f"{humanize.naturalsize(vmem.used)}/{humanize.naturalsize(vmem.available)}"
)
print(msg)
@functools.lru_cache(maxsize=64, typed=False)
def memcache(path):
suffix = Path(path).suffix
print(f"loading features >>>", end=" ")
tic = time.time()
if suffix == ".npy":
res = np_loader(path)
else:
raise ValueError(f"unknown suffix: {suffix} for path {path}")
print(f"[Total: {time.time() - tic:.1f}s] ({socket.gethostname() + ':' + str(path)})")
return res
def np_loader(np_path, l2norm=False):
with open(np_path, "rb") as f:
data = np.load(f, encoding="latin1", allow_pickle=True)
if isinstance(data, np.ndarray) and data.size == 1:
data = data[()] # handle numpy dict storage convnetion
if l2norm:
print("L2 normalizing features")
if isinstance(data, dict):
for key in data:
feats_ = data[key]
feats_ = feats_ / max(np.linalg.norm(feats_), 1E-6)
data[key] = feats_
elif data.ndim == 2:
data_norm = np.linalg.norm(data, axis=1)
data = data / np.maximum(data_norm.reshape(-1, 1), 1E-6)
else:
raise ValueError("unexpected data format {}".format(type(data)))
return data
class Timer:
def __init__(self):
self.cache = datetime.now()
def check(self):
now = datetime.now()
duration = now - self.cache
self.cache = now
return duration.total_seconds()
def reset(self):
self.cache = datetime.now()