forked from HongduanTian/CoPA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
220 lines (175 loc) · 6.17 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 random
import torch
import numpy as np
from time import time
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
class ConfusionMatrix():
def __init__(self, n_classes):
self.n_classes = n_classes
self.mat = np.zeros([n_classes, n_classes])
def update_mat(self, preds, labels, idxs):
idxs = np.array(idxs)
real_pred = idxs[preds]
real_labels = idxs[labels]
self.mat[real_pred, real_labels] += 1
def get_mat(self):
return self.mat
class Accumulator():
def __init__(self, max_size=2000):
self.max_size = max_size
self.ac = np.empty(0)
def append(self, v):
self.ac = np.append(self.ac[-self.max_size:], v)
def reset(self):
self.ac = np.empty(0)
def mean(self, last=None):
last = last if last else self.max_size
return self.ac[-last:].mean()
class IterBeat():
def __init__(self, freq, length=None):
self.length = length
self.freq = freq
def step(self, i):
if i == 0:
self.t = time()
self.lastcall = 0
else:
if ((i % self.freq) == 0) or ((i + 1) == self.length):
t = time()
print('{0} / {1} ---- {2:.2f} it/sec'.format(
i, self.length, (i - self.lastcall) / (t - self.t)))
self.lastcall = i
self.t = t
class SerializableArray(object):
def __init__(self, array):
self.shape = array.shape
self.data = array.tobytes()
self.dtype = array.dtype
def get(self):
array = np.frombuffer(self.data, self.dtype)
return np.reshape(array, self.shape)
class Recorder(object):
def __init__(self, saveroot:str, datasets:list, key_wd_list:list=None) -> None:
"""
Args:
saveroot: the root path of for saving the record file.
key_wd_list: A list of strings that are key words of records.
"""
self.saveroot = saveroot
self.datasets = datasets
if key_wd_list is None:
raise ValueError("No value to be recorded.")
else:
self.key_words = key_wd_list
if self.key_words:
self.records = self.generate_record_dict(self.datasets, self.key_words)
def generate_record_dict(self, datasets, keywords):
record_dict = {}
for dataset in datasets:
record_dict[dataset] = {}
for kw in keywords:
record_dict[dataset][kw] = []
return record_dict
def update_records(self, dataset:str, valueDict:dict):
'''
Args:
valueDict: A dict of values in which the keys should be consistent with the keys of self.records
'''
for key in valueDict.keys():
self.records[dataset][key].append(valueDict[key])
def save(self, filename:str):
if not os.path.exists(self.saveroot):
os.makedirs(self.saveroot)
filepath = os.path.join(self.saveroot, filename + '.npy')
np.save(filepath, self.records)
def print_res(array, name, file=None, prec=4, mult=1):
array = np.array(array) * mult
mean, std = np.mean(array), np.std(array)
conf = 1.96 * std / np.sqrt(len(array))
stat_string = ("test {:s}: {:0.%df} +/- {:0.%df}"
% (prec, prec)).format(name, mean, conf)
print(stat_string)
if file is not None:
with open(file, 'a+') as f:
f.write(stat_string + '\n')
def process_copies(embeddings, labels, args):
n_copy = args['test.n_copy']
test_embeddings = embeddings.view(
args['data.test_query'] * args['data.test_way'],
n_copy, -1).mean(dim=1)
return test_embeddings, labels[0::n_copy]
def set_determ(seed=1234):
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
def merge_dicts(dicts, torch_stack=True):
def stack_fn(l):
if isinstance(l[0], torch.Tensor):
return torch.stack(l)
elif isinstance(l[0], str):
return l
else:
return torch.tensor(l)
keys = dicts[0].keys()
new_dict = {key: [] for key in keys}
for key in keys:
for d in dicts:
new_dict[key].append(d[key])
if torch_stack:
for key in keys:
new_dict[key] = stack_fn(new_dict[key])
return new_dict
def voting(preds, pref_ind=0):
n_models = len(preds)
n_test = len(preds[0])
final_preds = []
for i in range(n_test):
cur_preds = [preds[k][i] for k in range(n_models)]
classes, counts = np.unique(cur_preds, return_counts=True)
if (counts == max(counts)).sum() > 1:
final_preds.append(preds[pref_ind][i])
else:
final_preds.append(classes[np.argmax(counts)])
return final_preds
def agreement(preds):
n_preds = preds.shape[0]
mat = np.zeros((n_preds, n_preds))
for i in range(n_preds):
for j in range(i, n_preds):
mat[i, j] = mat[j, i] = (
preds[i] == preds[j]).astype('float').mean()
return mat
def read_textfile(filename, skip_last_line=True):
with open(filename, 'r') as f:
container = f.read().split('\n')
if skip_last_line:
container = container[:-1]
return container
def check_dir(dirname, verbose=True):
"""This function creates a directory
in case it doesn't exist"""
try:
# Create target Directory
os.makedirs(dirname)
if verbose:
print("Directory ", dirname, " was created")
except FileExistsError:
if verbose:
print("Directory ", dirname, " already exists")
return dirname
def setup_seed(seed_id:int):
torch.manual_seed(seed_id)
torch.cuda.manual_seed_all(seed_id)
np.random.seed(seed_id)
random.seed(seed_id)
torch.backends.cudnn.deterministic=True
def spectrum_norm(m:torch.tensor) -> torch.tensor:
'''calculate the spectrum norm of a parameter matrix.'''
eigvals, eigvecs = torch.eig(m)
max_val = torch.max(eigvals)
if max_val.item() < 0:
max_val = torch.tensor(1.0).type_as(max_val)
return m / max_val