-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclustering.py
More file actions
62 lines (48 loc) · 1.79 KB
/
clustering.py
File metadata and controls
62 lines (48 loc) · 1.79 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
import os
import shutil
from itertools import compress
import numpy as np
import pandas as pd
from scipy.cluster.vq import kmeans2
from sklearn.decomposition import PCA
from tqdm import tqdm
# Performs Principal Component Analysis (PCA) on the input embeddings to reduce their dimensionality.
def calculate_pca(embeddings, dim=16):
print("Calculating PCA")
pca = PCA(n_components=dim)
pca_embeddings = pca.fit_transform(embeddings.squeeze())
print("PCA calculating done!")
return pca_embeddings
# Performs k-means clustering on the input embeddings.
def calculate_kmeans(embeddings, k):
print("KMeans processing...")
centroid, labels = kmeans2(data=embeddings, k=k, minit="points")
counts = np.bincount(labels)
print("Kmeans done!")
return centroid, labels
def load_embeddings(file_path):
embeddings = pd.read_csv(file_path)
file_paths = embeddings["filepaths"]
embeddings = embeddings.drop("filepaths", axis=1)
return embeddings.values, file_paths
def create_dir(directory):
if not os.path.exists(directory):
os.makedirs(directory)
# Load embeddings
embeddings, image_paths = load_embeddings("embeddings/horse_embeddings.csv")
pca_dim = 16
cluster_range = 4
project_name = "horse"
# Embeddings to PCA
pca_embeddings = calculate_pca(embeddings, dim=pca_dim)
# PCA to k-means
centroids, labels = calculate_kmeans(pca_embeddings, k=cluster_range)
# Save random sample clusters
for label_number in tqdm(range(cluster_range)):
label_mask = labels == label_number
path_images = list(compress(image_paths, label_mask))
target_directory = f"./clusters/{project_name}/cluster_{label_number}"
create_dir(target_directory)
# Copy images into separate directories
for img_path in path_images:
shutil.copy2(img_path, target_directory)