-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
46 lines (39 loc) · 1.59 KB
/
Copy pathutils.py
File metadata and controls
46 lines (39 loc) · 1.59 KB
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
import numpy as np
import matplotlib.pyplot as plt
import sklearn
from scipy.optimize import linear_sum_assignment as linear_assignment
def imshow(img):
img = img / 2 + 0.5 # unnormalize
plt.imshow(np.transpose(img.numpy(), (1, 2, 0)))
plt.show()
def calculate_predictions(model, dataloader, device):
output_array = None
label_array = None
model.eval()
for data in dataloader:
inputs, labels = data
inputs = inputs.to(device)
labels = labels.to(device)
_, outputs, _ = model(inputs)
if output_array is not None:
output_array = np.concatenate((output_array, outputs.cpu().detach().numpy()), 0)
label_array = np.concatenate((label_array, labels.cpu().detach().numpy()), 0)
else:
output_array = outputs.cpu().detach().numpy()
label_array = labels.cpu().detach().numpy()
preds = np.argmax(output_array.data, axis=1)
# print(output_array.shape)
return output_array, label_array, preds
class metrics:
nmi = sklearn.metrics.normalized_mutual_info_score
ari = sklearn.metrics.adjusted_rand_score
@staticmethod
def acc(labels_true, labels_pred):
labels_true = labels_true.astype(np.int64)
assert labels_pred.size == labels_true.size
D = max(labels_pred.max(), labels_true.max()) + 1
w = np.zeros((D, D), dtype=np.int64)
for i in range(labels_pred.size):
w[labels_pred[i], labels_true[i]] += 1
ind = linear_assignment(w.max() - w)
return sum([w[i, j] for i, j in zip(ind[0], ind[1])]) * 1.0 / labels_pred.size