forked from snap-stanford/stellar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
180 lines (149 loc) · 5.49 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
import torch
import torch.nn as nn
import numpy as np
from scipy.optimize import linear_sum_assignment
import os
import random
import pickle
import datetime
import os.path
import torch.nn.functional as F
from copy import deepcopy
class ModelEma(torch.nn.Module):
def __init__(self, model, decay=0.9997, device=None):
super(ModelEma, self).__init__()
# make a copy of the model for accumulating moving average of weights
self.module = deepcopy(model)
self.module.eval()
self.decay = decay
self.device = device # perform ema on different device from model if set
if self.device is not None:
self.module.to(device=device)
def _update(self, model, update_fn):
with torch.no_grad():
for ema_v, model_v in zip(self.module.state_dict().values(), model.state_dict().values()):
if self.device is not None:
model_v = model_v.to(device=self.device)
ema_v.copy_(update_fn(ema_v, model_v))
def update(self, model):
self._update(model, update_fn=lambda e, m: self.decay * e + (1. - self.decay) * m)
def set(self, model):
self._update(model, update_fn=lambda e, m: m)
class AverageMeter(object):
def __init__(self, name, fmt=':f'):
self.name = name
self.fmt = fmt
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 __str__(self):
fmtstr = '{name} {val' + self.fmt + '} ({avg' + self.fmt + '})'
return fmtstr.format(**self.__dict__)
def accuracy(output, target):
num_correct = np.sum(output == target)
res = num_correct / len(target)
return res
def cluster_acc(y_pred, y_true):
"""
Calculate clustering accuracy. Require scikit-learn installed
# Arguments
y: true labels, numpy.array with shape `(n_samples,)`
y_pred: predicted labels, numpy.array with shape `(n_samples,)`
# Return
accuracy, in [0,1]
"""
y_true = y_true.astype(np.int64)
assert y_pred.size == y_true.size
D = max(y_pred.max(), y_true.max()) + 1
w = np.zeros((D, D), dtype=np.int64)
for i in range(y_pred.size):
w[y_pred[i], y_true[i]] += 1
row_ind, col_ind = linear_sum_assignment(w.max() - w)
return w[row_ind, col_ind].sum() / y_pred.size
def cluster_f1(y_pred, y_true):
"""
Calculate clustering accuracy. Require scikit-learn installed
# Arguments
y: true labels, numpy.array with shape `(n_samples,)`
y_pred: predicted labels, numpy.array with shape `(n_samples,)`
# Return
accuracy, in [0,1]
"""
y_true = y_true.astype(np.int64)
assert y_pred.size == y_true.size
D = max(y_pred.max(), y_true.max()) + 1
w = np.zeros((D, D), dtype=np.int64)
for i in range(y_pred.size):
w[y_pred[i], y_true[i]] += 1
row_ind, col_ind = linear_sum_assignment(w.max() - w)
precision = w / np.sum(w[row_ind, :], axis = 1, keepdims=True)
recall = w / np.sum(w[:, col_ind], axis = 0, keepdims=True)
f1 = 2 * precision * recall / (precision + recall)
f1_max = np.nanmax(f1, axis=0)
cls_weight = np.sum(w[:, col_ind], axis = 0)
cls_weight = cls_weight / np.sum(cls_weight)
w_f1 = np.sum(cls_weight * f1_max)
return w_f1
def prepare_save_dir(args, filename):
""" Create saving directory."""
runner_name = os.path.basename(filename).split(".")[0]
model_dir = 'data/experiments/{}/{}/'.format(runner_name, args.name)
args.savedir = model_dir
if not os.path.exists(args.savedir):
os.makedirs(args.savedir)
return args
def write_txt(args, string):
""" Write the string in a text file."""
with open(args.savedir + 'out.txt', 'a') as f:
f.write(string + " \n")
def create_logger(args, metrics):
""" Create a logger."""
args.logger = {}
args.logger['time_start'] = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
i = 0
while os.path.isfile(args.savedir + str(i) + '.pkl'):
i += 1
args.logger['pkl_path'] = args.savedir + str(i) + '.pkl'
args.logger['path'] = args.savedir + str(i)
for metric in metrics:
args.logger[metric] = []
return args
def save_logger(args):
""" Save the logger."""
with open(args.logger['pkl_path'], "wb") as output_file:
pickle.dump(vars(args), output_file)
def entropy(x):
"""
Helper function to compute the entropy over the batch
input: batch w/ shape [b, num_classes]
output: entropy value [is ideally -log(num_classes)]
"""
EPS = 1e-8
x_ = torch.clamp(x, min = EPS)
b = x_ * torch.log(x_)
if len(b.size()) == 2: # Sample-wise entropy
return - b.sum(dim = 1).mean()
elif len(b.size()) == 1: # Distribution-wise entropy
return - b.sum()
else:
raise ValueError('Input tensor is %d-Dimensional' %(len(b.size())))
class MarginLoss(nn.Module):
def __init__(self, m=0.2, weight=None, s=10):
super(MarginLoss, self).__init__()
self.m = m
self.s = s
self.weight = weight
def forward(self, x, target):
index = torch.zeros_like(x, dtype=torch.bool)
index.scatter_(1, target.data.view(-1, 1), 1)
x_m = x - self.m * self.s
output = torch.where(index, x_m, x)
return F.cross_entropy(output, target, weight=self.weight)