-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathanalyze_kmeans_clustering.py
More file actions
331 lines (263 loc) · 11.1 KB
/
analyze_kmeans_clustering.py
File metadata and controls
331 lines (263 loc) · 11.1 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
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#!/usr/bin/env python
"""
K-means Clustering Analysis Script
Analyzes K-means clustering on semantic features extracted from anomaly detection datasets.
Computes silhouette score, purity, and NMI for different numbers of clusters (k).
Reads experiment config from results folder and saves analysis results there.
Usage:
python analyze_kmeans_clustering.py --config results/MVTec/HierarchicalPatchCore/greedy0.1-layer4/all/config.yaml
python analyze_kmeans_clustering.py --config results/MPDD/HierarchicalPatchCore/greedy0.1-layer4/all/config.yaml
"""
import argparse
import os
import json
import numpy as np
import torch
import torch.nn.functional as F
from torch.utils.data import DataLoader
from tqdm import tqdm
from sklearn.metrics import silhouette_score, silhouette_samples, normalized_mutual_info_score
from sklearn.cluster import KMeans
import yaml
import backbones
from dataset import create_dataset, DatasetSplit
def load_config(config_path):
"""Load experiment config from yaml file."""
with open(config_path, 'r') as f:
config = yaml.safe_load(f)
return config
def extract_semantic_features(dataloader, backbone, device, semantic_layer='layer4'):
"""Extract semantic features using specified backbone layer."""
features_buffer = {}
def hook_fn(module, input, output):
features_buffer['feat'] = output
layer = getattr(backbone, semantic_layer)
handle = layer.register_forward_hook(hook_fn)
all_features = []
all_classes = []
with torch.no_grad():
for batch in tqdm(dataloader, desc='Extracting features'):
images = batch['image'].to(device)
classes = batch['classname']
_ = backbone(images)
feat = features_buffer['feat']
feat = F.adaptive_avg_pool2d(feat, (1, 1))
feat = feat.view(feat.size(0), -1)
all_features.append(feat.cpu().numpy())
all_classes.extend(classes)
handle.remove()
return np.vstack(all_features), all_classes
def compute_purity(clusters, labels, per_sample_sil=None):
"""Compute clustering purity.
If per_sample_sil (array of silhouette values per sample) is provided, compute
the mean silhouette per cluster and include it in the returned cluster_info.
"""
unique_labels = sorted(set(labels))
label_to_int = {l: i for i, l in enumerate(unique_labels)}
labels_int = np.array([label_to_int[l] for l in labels])
total_correct = 0
cluster_info = []
for cluster_id in np.unique(clusters):
mask = clusters == cluster_id
cluster_labels = labels_int[mask]
if len(cluster_labels) == 0:
continue
counts = np.bincount(cluster_labels, minlength=len(unique_labels))
dominant_count = counts.max()
dominant_label = unique_labels[counts.argmax()]
total_correct += dominant_count
info = {
'cluster_id': int(cluster_id),
'size': int(mask.sum()),
'dominant_class': dominant_label,
'purity': float(dominant_count / mask.sum() * 100)
}
if per_sample_sil is not None:
# average silhouette of samples in this cluster
sil_vals = per_sample_sil[mask]
# If cluster has only one sample, silhouette is not defined; set to NaN
if len(sil_vals) == 0:
info['silhouette'] = None
else:
info['silhouette'] = float(np.nanmean(sil_vals))
else:
info['silhouette'] = None
cluster_info.append(info)
overall_purity = total_correct / len(labels) * 100
return overall_purity, cluster_info
def compute_nmi(clusters, labels):
"""Compute Normalized Mutual Information."""
unique_labels = sorted(set(labels))
label_to_int = {l: i for i, l in enumerate(unique_labels)}
labels_int = np.array([label_to_int[l] for l in labels])
return normalized_mutual_info_score(labels_int, clusters)
def analyze_kmeans(features, labels, k_range=range(2, 16)):
"""Run K-means clustering for different k values and analyze results."""
print('\nRunning K-means clustering...')
results = []
best_silhouette = -1
best_k = -1
print('\n' + '=' * 80)
print(f'{"k":^10} | {"Silhouette":^12} | {"Purity":^10} | {"NMI":^10}')
print('-' * 80)
for k in k_range:
kmeans = KMeans(n_clusters=k, random_state=42, n_init=10)
clusters = kmeans.fit_predict(features)
silhouette = silhouette_score(features, clusters)
purity, _ = compute_purity(clusters, labels)
nmi = compute_nmi(clusters, labels)
results.append({
'k': k,
'silhouette': silhouette,
'purity': purity,
'nmi': nmi
})
marker = ''
if silhouette > best_silhouette:
best_silhouette = silhouette
best_k = k
marker = ' <-- BEST'
print(f'{k:^10} | {silhouette:^12.4f} | {purity:^10.2f} | {nmi:^10.4f}{marker}')
print('=' * 80)
# Best k details
best_result = next(r for r in results if r['k'] == best_k)
print(f'\nBest k (max silhouette): {best_k}')
print(f' - Silhouette Score: {best_result["silhouette"]:.4f}')
print(f' - Purity: {best_result["purity"]:.2f}%')
print(f' - NMI: {best_result["nmi"]:.4f}')
# Cluster distribution for best k (include per-sample silhouette -> per-cluster mean)
kmeans = KMeans(n_clusters=best_k, random_state=42, n_init=10)
best_clusters = kmeans.fit_predict(features)
# compute per-sample silhouette for the best k
try:
per_sample_sil = silhouette_samples(features, best_clusters)
except Exception:
# fallback: if silhouette_samples fails (e.g., single cluster), set to None
per_sample_sil = None
_, cluster_info = compute_purity(best_clusters, labels, per_sample_sil)
print(f'\nCluster Distribution (k={best_k}):')
print('-' * 60)
for info in sorted(cluster_info, key=lambda x: x['cluster_id']):
print(f" Cluster {info['cluster_id']:3d}: {info['size']:4d} samples, "
f"dominant: {info['dominant_class']:15s}, purity: {info['purity']:.1f}%")
return results, best_clusters, cluster_info
def save_results(save_path, results, cluster_info, labels):
"""Save analysis results to JSON file."""
unique_labels = sorted(set(labels))
# Find best k (max silhouette)
best_idx = max(range(len(results)), key=lambda i: results[i]['silhouette'])
best_result = results[best_idx]
output = {
'num_samples': len(labels),
'num_ground_truth_classes': len(unique_labels),
'ground_truth_classes': unique_labels,
'k_values': [r['k'] for r in results],
'results': [
{
'k': r['k'],
'silhouette': float(r['silhouette']),
'purity': float(r['purity']),
'nmi': float(r['nmi'])
}
for r in results
],
'best_k': {
'k': int(best_result['k']),
'silhouette': float(best_result['silhouette']),
'purity': float(best_result['purity']),
'nmi': float(best_result['nmi']),
'cluster_distribution': [
{
'cluster_id': int(info['cluster_id']),
'size': int(info['size']),
'dominant_class': info['dominant_class'],
'purity': float(info['purity']),
'silhouette': (float(info['silhouette']) if ('silhouette' in info and info['silhouette'] is not None) else None)
}
for info in sorted(cluster_info, key=lambda x: x['cluster_id'])
]
}
}
with open(save_path, 'w') as f:
json.dump(output, f, indent=2)
return output
def main():
parser = argparse.ArgumentParser(description='Analyze K-means clustering on semantic features')
parser.add_argument('--config', type=str, required=True,
help='Path to experiment config.yaml file')
parser.add_argument('--device', type=str, default='cuda',
help='Device (cuda or cpu)')
parser.add_argument('--k_min', type=int, default=2,
help='Minimum number of clusters')
parser.add_argument('--k_max', type=int, default=15,
help='Maximum number of clusters')
args = parser.parse_args()
# Load config
config = load_config(args.config)
config_dir = os.path.dirname(args.config)
# Extract parameters from config
dataset_name = config['DATASET']['name']
data_path = config['DATASET']['datadir']
resize = config['DATASET']['resize']
imagesize = config['DATASET']['imagesize']
classname = config['DATASET']['classname']
backbone_name = config['MODEL']['backbone']
semantic_layer = config['MODEL'].get('semantic_layer', 'layer4')
batch_size = config['TRAIN'].get('test_batch_size', 64)
num_workers = config['TRAIN'].get('num_workers', 8)
device = torch.device(args.device if torch.cuda.is_available() else 'cpu')
print('=' * 80)
print('K-means Clustering Analysis')
print('=' * 80)
print(f'Config: {args.config}')
print(f'Dataset: {dataset_name}')
print(f'Data path: {data_path}')
print(f'Classname: {classname}')
print(f'Resize: {resize}')
print(f'Image size: {imagesize}')
print(f'Backbone: {backbone_name}')
print(f'Semantic layer: {semantic_layer}')
print(f'Device: {device}')
print(f'K range: {args.k_min} to {args.k_max}')
print()
# Load dataset
print('Loading dataset...')
dataset = create_dataset(
dataname=dataset_name,
source=data_path,
classname=classname,
resize=resize,
imagesize=imagesize,
split=DatasetSplit.TRAIN
)
dataloader = DataLoader(
dataset,
batch_size=batch_size,
shuffle=False,
num_workers=num_workers,
pin_memory=True
)
print(f'Total samples: {len(dataset)}')
# Load backbone
print(f'\nLoading backbone ({backbone_name})...')
backbone = backbones.load(backbone_name)
backbone = backbone.to(device)
backbone.eval()
# Extract features
print(f'\nExtracting semantic features from {semantic_layer}...')
features, labels = extract_semantic_features(
dataloader, backbone, device, semantic_layer
)
print(f'Features shape: {features.shape}')
print(f'Unique classes: {len(set(labels))}')
# Analyze K-means clustering
k_range = range(args.k_min, args.k_max + 1)
results, best_clusters, cluster_info = analyze_kmeans(features, labels, k_range)
# Save results
save_path = os.path.join(config_dir, 'kmeans_analysis.json')
output = save_results(save_path, results, cluster_info, labels)
print('\n' + '=' * 80)
print(f'Results saved to: {save_path}')
print('=' * 80)
if __name__ == '__main__':
main()