I am using this notebook to cluster the images, this is working fine.But I need to pull out images from the created clusters into separate folders.
I don't know how group them separately.
The folder naming can be anything like Numerical sequence.
%matplotlib inline
import os
import random
import numpy as np
import json
import matplotlib.pyplot
import pickle
from matplotlib.pyplot import imshow
from PIL import Image
from sklearn.manifold import TSNE
images, pca_features, pca = pickle.load(open('../data/features_caltech101.p', 'rb'))
for img, f in list(zip(images, pca_features))[0:5]:
print("image: %s, features: %0.2f,%0.2f,%0.2f,%0.2f... "%(img, f[0], f[1], f[2], f[3]))
num_images_to_plot = 1000
if len(images) > num_images_to_plot:
sort_order = sorted(random.sample(range(len(images)), num_images_to_plot))
images = [images[i] for i in sort_order]
pca_features = [pca_features[i] for i in sort_order]
X = np.array(pca_features)
tsne = TSNE(n_components=2, learning_rate=150, perplexity=30, angle=0.2, verbose=2).fit_transform(X)
width = 4000
height = 3000
max_dim = 100
full_image = Image.new('RGBA', (width, height))
for img, x, y in zip(images, tx, ty):
tile = Image.open(img)
rs = max(1, tile.width/max_dim, tile.height/max_dim)
tile = tile.resize((int(tile.width/rs), int(tile.height/rs)), Image.ANTIALIAS)
full_image.paste(tile, (int((width-max_dim)*x), int((height-max_dim)*y)), mask=tile.convert('RGBA'))
matplotlib.pyplot.figure(figsize = (16,12))
imshow(full_image)
Ref : https://github.com/ml4a/ml4a/blob/master/notebooks/image-tsne.ipynb
