-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
79 lines (64 loc) · 2.21 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
from scipy.optimize import linear_sum_assignment
import numpy as np
import torch
from tqdm import tqdm
from deepclustering import IDEC
def cluster_acc(y_true, y_pred, reassign:bool = False):
"""
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)
cluster_accuracy = w[row_ind, col_ind].sum() / y_pred.size
if reassign:
reassignment = dict(zip(row_ind, col_ind))
return (
reassignment,
cluster_accuracy
)
else:
return cluster_accuracy
@torch.inference_mode()
def predict_cluster_accuracy(model, loader, device, reassigned:bool=False):
model.eval()
model.to(device)
targets, features, predicted = [], [], []
for x, y in tqdm(loader, desc='Evaluate cluster accuracy'):
x, y = x.to(device), y.to(device)
x = x.reshape(-1, 28*28)
if type(model)==IDEC:
output = model(x)[0]
else:
output = model(x)
y_pred = output.argmax(1)
targets.append(y)
features.append(output)
predicted.append(y_pred)
targets = torch.cat(targets).cpu().numpy()
features = torch.cat(features).cpu().numpy()
predicted = torch.cat(predicted).cpu().numpy()
if reassigned:
reassignment_dict, accuracy = cluster_acc(targets, predicted, reassign=True)
return {
'accuracy': accuracy,
'predicted': np.vectorize(reassignment_dict.get)(predicted),
'features': features,
'reassignment_dict': reassignment_dict
}
else:
accuracy = cluster_acc(targets, predicted, reassign=False)
return{
'accuracy': accuracy,
'predicted': predicted,
'features': features
}