-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutils.py
248 lines (198 loc) · 8.15 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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import torch
import torch.nn as nn
import numpy as np
from scipy.optimize import linear_sum_assignment
import os
import sys
import os.path
import torch.nn.functional as F
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt
from sklearn.manifold import TSNE
from sklearn import metrics
from sklearn.cluster import SpectralClustering, AffinityPropagation
from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import connected_components
import networkx as nx
import community
plt.rcParams.update({'figure.max_open_warning': 0})
class TransformTwice:
# two different random transform with one image
def __init__(self, transform):
self.transform = transform
def __call__(self, inp):
out1 = self.transform(inp)
out2 = self.transform(inp)
return out1, out2
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__)
class Logger(object):
def __init__(self, filename, stream=sys.stdout):
self.terminal = stream
self.log = open(filename, 'w')
def write(self, message):
self.terminal.write(message)
self.log.write(message)
def flush(self):
pass
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 setup_seed(seed):
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
np.random.seed(seed)
# random.seed(seed)
torch.backends.cudnn.deterministic = True
class Logger(object):
def __init__(self, filename, stream=sys.stdout):
self.terminal = stream
self.log = open(filename, 'w')
def write(self, message):
self.terminal.write(message)
self.log.write(message)
def flush(self):
pass
def extract_features(model, data_loader):
# extract features from dataloader to numpy array
features = []
targets = []
model.eval()
with torch.no_grad():
for (image, image2), label in data_loader:
image= image.cuda()
feature = model.encoder(image)
features.append(feature)
targets.append(label)
features = torch.cat(features, 0).detach().cpu().numpy()
targets = torch.cat(targets, 0).detach().numpy()
return features, targets
def topK(self, matrix, k, axis=-1):
if k > 0:
topK_ind = np.argpartition(matrix, kth=-k, axis=axis)[:,-k:]
else: # bottom K
topK_ind = np.argpartition(matrix, kth=-k, axis=axis)[:,:-k]
topK_elements = np.take_along_axis(matrix, topK_ind, axis=axis)
return topK_elements
def purity_score(y_true, y_pred):
# compute contingency matrix (also called confusion matrix)
contingency_matrix = metrics.cluster.contingency_matrix(y_true, y_pred)
# return purity
purity_pred = np.amax(contingency_matrix, axis=0) / np.sum(contingency_matrix, axis=0)
purity_score = np.mean(purity_pred)
return purity_pred, purity_score
def proto_graph(dist_matrix, n_nearest_neighbor):
n_topk = n_nearest_neighbor + 1
n_protos = dist_matrix.shape[1]
_, topk_inds = dist_matrix.topk(n_topk, dim=1, largest=True, sorted=True)
edges_pairs = [topk_inds[:,[0,k]] for k in range(n_topk)]
edges = torch.vstack(edges_pairs)
weights = torch.ones_like(edges[:,0])
graph = torch.sparse_coo_tensor(edges.t(), weights, size=(n_protos, n_protos))
graph = graph.to_dense().type(torch.Tensor)
return graph
def graph_cluster(edge_graph, prototypes, lamda=0.5, method='spectral', seed=0, n_cls=10, eps=0.7):
edge_graph = edge_graph / torch.clamp(edge_graph.diag().reshape(-1,1), 1)
edge_graph = edge_graph.cpu().numpy()
edge_graph = (edge_graph + edge_graph.T) / 2 - np.diag(edge_graph.diagonal())
attr_graph = ((torch.mm(prototypes , prototypes.T) + 1) / 2).cpu().numpy()
graph = lamda * edge_graph + (1-lamda) * attr_graph
if method == 'spectral': # for known cluster number
clu_spec = SpectralClustering(n_clusters=n_cls,
assign_labels='kmeans',
random_state=seed, affinity='precomputed').fit(graph)
label = clu_spec.labels_
elif method == 'propagation': # for unknown cluster number
clu_affi = AffinityPropagation(affinity='precomputed',
damping=eps,
verbose=False).fit(graph)
label = clu_affi.labels_
elif method == 'connected': # for unknown cluster number
select_graph = csr_matrix((graph>eps))
n_components, label = connected_components(csgraph=select_graph, directed=False, return_labels=True)
elif method == 'louvain': # for unknown cluster number
partition = community.best_partition(nx.from_numpy_array(graph), resolution=eps)
label = np.fromiter(partition.values(), dtype=int)
mask = graph.copy()
for i in range(len(mask)):
for j in range(len(mask)):
if label[i] == label[j]:
mask[i,j] = 1
else:
mask[i,j] = 0
return label, mask
def reknn_graph(dist_matrix, n_nearest_neighbor, mode='harmonic_mean'):
# n_topk = n_nearest_neighbor
_, top_indices = torch.topk(dist_matrix, n_nearest_neighbor)
onehot = torch.zeros_like(dist_matrix).scatter(1, top_indices, 1).bool()
jaccard = torch.zeros(onehot.shape[1], onehot.shape[1])
ind_reknn = onehot.t()
num_reknn = onehot.t().sum(1)
for i in range(onehot.shape[1]):
for j in range(onehot.shape[1]):
n_intersection = (onehot.t()[i] & onehot.t()[j]).sum()
if n_intersection == 0:
jaccard[i][j] = 0
continue
if mode == 'jaccard':
n_union = (onehot.t()[i] | onehot.t()[j]).sum()
jaccard[i][j] = n_intersection / n_union
if mode == 'harmonic_mean':
jaccard[i][j] = n_intersection / 2 * num_reknn[i] * num_reknn[j] / (num_reknn[i] + num_reknn[j])
elif mode == 'geometric_mean':
jaccard[i][j] = n_intersection / torch.sqrt(num_reknn[i] * num_reknn[j])
elif mode == 'min':
jaccard[i][j] = n_intersection / torch.min(num_reknn[i], num_reknn[j])
elif mode == 'max':
jaccard[i][j] = n_intersection / torch.max(num_reknn[i], num_reknn[j])
return jaccard
# def proto_label_graph(features_l, targets_l, prototypes):
# dist_matrix_l = torch.mm(features_l, prototypes.t())
# proto_len = prototypes.shape[0]
# proto_count_l = torch.mm(F.one_hot(dist_matrix_l.max(1)[1], num_classes=proto_len).T.float(), F.one_hot(targets_l).float())
# proto_graph_l = torch.zeros(proto_len, proto_len)
# min_count = targets_l.shape[0] / proto_len
# for i in range(proto_len):
# for j in range(proto_len):
# if proto_count_l[i].sum() > min_count and proto_count_l[j].sum() > min_count:
# if proto_count_l[i].argmax() == proto_count_l[j].argmax():
# proto_graph_l[i][j] = 1
# else:
# proto_graph_l[i][j] = -1
# else:
# proto_graph_l[i][j] = 0
# return proto_graph_l