-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.py
More file actions
126 lines (99 loc) · 4.74 KB
/
Copy pathutils.py
File metadata and controls
126 lines (99 loc) · 4.74 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
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
"""
Created on Tuesday April 24 2020
@author: Ahmad Mustapha (amm90@mail.aub.edu)
"""
from deep_clustering_net import DeepClusteringNet
from deep_clustering_dataset import DeepClusteringDataset
from preprocessing import l2_normalization, sklearn_pca_whitening
from torch.utils.data import DataLoader
from sklearn.cluster import KMeans
from sklearn.mixture import GaussianMixture
from sklearn.metrics import normalized_mutual_info_score as NMI
from sklearn.metrics import silhouette_samples
from torch.utils.tensorboard import SummaryWriter
from scipy.stats import entropy
import matplotlib.pyplot as plt
import torch
import numpy as np
import random
import os
def set_seed(seed):
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
np.random.seed(seed)
random.seed(seed)
os.environ['PYTHONHASHSEED'] = str(seed)
def qualify_space(model: DeepClusteringNet, dataset: DeepClusteringDataset,
k_list: list, writer:SummaryWriter=None,
verbose=True, random_state=None,**kwargs):
# clustering algorithm:
clustering_algorithm = kwargs.get("clustering_algorithm", "kmeans")
# full feedforward
features = model.full_feed_forward(
dataloader=torch.utils.data.DataLoader(dataset,
batch_size=256,
shuffle=False,
pin_memory=True), verbose=True)
# pre-processing pca-whitening
if kwargs.get("pca_components", None) == None:
pass
else:
if verbose:
print(" - Features PCA + Whitening")
features = sklearn_pca_whitening(features, n_components=kwargs.get("pca_components"), random_state=random_state)
# pre-processing l2-normalization
if verbose:
print(" - Features L2 Normalization")
features = l2_normalization(features)
# cluster
if verbose:
print(" - Clustering")
if clustering_algorithm=="kmeans":
CMs = [ KMeans(n_clusters = k, random_state=random_state, n_init= kwargs.get("n_init", 10), verbose=verbose) for k in k_list ]
elif clustering_algorithm== "gmm":
CMs = [ GaussianMixture(n_components = k, random_state=random_state, verbose=verbose) for k in k_list ]
else:
raise Exception("Error an unsupported clustering algorithm was provided")
k_assignments = [ CM.fit_predict(features) for CM in CMs ]
k_grouped_assignments_indices = [ group_by_index(assignments) for assignments in k_assignments]
k_entropies = [[] for i in range(len(k_list))]
for i, grouped_assignments_indices in enumerate(k_grouped_assignments_indices):
grouped_original_labels = [[dataset.get_targets()[index] for index in group] for group in grouped_assignments_indices]
grouped_counts = [np.unique(group, return_counts=True)[1] for group in grouped_original_labels]
entropies = [entropy(group) for group in grouped_counts]
k_entropies[i] = entropies
k_avg_entropies = [np.average(entropies) for entropies in k_entropies ]
k_min_entropies = [np.min(entropies) for entropies in k_entropies ]
k_max_entropies = [np.max(entropies) for entropies in k_entropies ]
if writer:
for i,k in enumerate(k_list):
writer.add_histogram(clustering_algorithm+"/Space Quality k=%d"%k, np.array(k_entropies[i]), global_step=0)
if writer:
avg_entropies_vs_k = plt.figure()
plt.plot(k_list, k_avg_entropies,"-*", label="Avg")
plt.plot(k_list, k_min_entropies,"-+", label="Min")
plt.plot(k_list, k_max_entropies,"-o", label="Max")
plt.title("Space Quality")
plt.xlabel("Number of clusters")
plt.ylabel(" Cluster Entropy")
plt.legend()
writer.add_figure(clustering_algorithm+"/Space Quality Entropy", avg_entropies_vs_k, global_step=0)
k_nmis = [ NMI(assignments, dataset.get_targets()) for assignments in k_assignments ]
if writer:
nmis_vs_k = plt.figure()
plt.plot(k_list, k_nmis)
plt.title("Space Quality")
plt.xlabel("Number of clusters")
plt.ylabel("Predicted/GroundTruth NMI")
plt.legend()
writer.add_figure(clustering_algorithm+"/Space Quality NMI", nmis_vs_k, global_step=0)
k_silhouette_samples = [ silhouette_samples(features, labels) for labels in k_assignments]
return [ (k, k_entropies[i], k_nmis[i], CMs[i].inertia_, k_silhouette_samples[i]) for i,k in enumerate(k_list)]
def group_by_index(labels):
n_labels = len(np.unique(labels))
grouped_indices = [[] for i in range(n_labels)]
for i, label in enumerate(labels):
grouped_indices[label].append(i)
return grouped_indices