Can some please help me to do t-SNE on my image data?. I am using this step to know how well my image data is clustered to know the probabilities of each class. Here is my code but I am getting some not able plot I am not able to understand what's wrong with my code.
def scale_to_01_range(x, images):
# compute the distribution range
value_range = (np.max(images) - np.min(images))
# move the distribution so that it starts from zero
# by extracting the minimal value from all its values
starts_from_zero = images - np.min(images)
# make the distribution fit [0; 1] by dividing by its ran
return x, starts_from_zero / value_range
def plot_tsne(all_tsne_images, all_tsne_classes):
labels = np.unique(all_tsne_classes)
print("labels of class", labels)
temp_total_images = []
temp_classes = []
class_index = 0
for i in all_tsne_images:
if all_tsne_classes[class_index] == 0:
temp_classes.append(0)
else:
temp_classes.append(1)
# Resizing the images from 640*480 to 128*128 without losing any data. To improve the the speed of the model
temp = cv2.resize(i, (128, 128))
temp = cv2.cvtColor(temp, cv2.COLOR_BGR2GRAY) # converting image RGB to luminance mode(grey scale)
temp_total_images.append(np.array(temp))
# Expand the dimension of the image
class_index += 1
print(np.asarray(temp_total_images[0]).shape)
temp_total_images = np.expand_dims(temp_total_images, axis=3)
temp_classes = np.asarray(temp_classes)
temp_total_images = (np.asarray(temp_total_images, np.float32)) / 255.0
tsne = TSNE(n_components=2)
tsne_obj = tsne.fit_transform(temp_total_images)
print(tsne_obj)
tx = tsne[:, 0]
ty = tsne[:, 1]
tx = scale_to_01_range(tx, temp_total_images)
ty = scale_to_01_range(ty, temp_classes)
# initialize a matplotlib plot
fig = plt.figure()
ax = fig.add_subplot(111)
# for every class, we'll add a scatter plot separately
for label in all_tsne_classes:
# find the samples of the current class in the data
indices = [i for i, l in enumerate(labels) if l == label]
# extract the coordinates of the points of this class only
current_tx = np.take(tx, indices)
current_ty = np.take(ty, indices)
# convert the class color to matplotlib format
color = np.array(all_tsne_classes[label], dtype=np.float32) / 255
# add a scatter plot with the corresponding color and label
ax.scatter(current_tx, current_ty, c=color, label=label)
# build a legend using the labels we set previously
ax.legend(loc='best')
# finally, show the plot
plt.show()
Please help me.
Thank you
Jyo
question from:
https://stackoverflow.com/questions/65950163/t-sne-for-image-data 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…