TDM 19000: Clustering
Clustering is an unsupervised machine learning technique that groups pixels in images based on similar characteristics - this can be color, intensity, or other features in an image. The result of this is clusters (groups) of images that are all similar to each other in some overarching way.
To break up the folder 'Snails' that contains 30 images, we will:
-
Create 3 clusters for image groups
-
Label each image based on the features found in its embedding
-
Apply the label directly to each file name
from sklearn.cluster import KMeans
labels = []
kmeans = KMeans(n_clusters=3, random_state=42).fit(features.numpy())
for idx, label in enumerate(kmeans.labels_):
print(f"{filenames[idx]} -> Cluster {label}")
labels.append([filenames[idx], label])
To sort the images based on their cluster, we will create three cluster groups:
from collections import defaultdict
# Initialize a dictionary where key=cluster, value=list of filenames
cluster_groups = defaultdict(list)
for fname, cluster in labels:
cluster_groups[cluster].append(fname)
|
To view what files are in each cluster group, use 'print(cluster_groups[__])'. i.e. to view the files in cluster 0, print(cluster_groups[0]), …, switching the number accordingly. |
Looking at the names of the files is good, but it would be cool to actually see the sort of images that got sorted into each cluster.
# !!!!!!!!
# change this [0] according to if you want to show cluster 0, 1, or 2.
my_cluster = cluster_groups[0]
# number of images per row
cols = 4
# calculate number of rows
rows = (len(my_cluster) + cols - 1) // cols
plt.figure(figsize=(cols*3, rows*3))
for i, fname in enumerate(my_cluster):
path = os.path.join(folder, fname)
img = Image.open(path)
plt.subplot(rows, cols, i+1)
plt.imshow(img)
plt.axis('off')
plt.title(fname, fontsize=8)
plt.tight_layout()
plt.show()
Make sure to run the above code for each of the clusters 0, 1, and 2.