<jupyter_start><jupyter_text>Embeddings Visualization with Fashion MNIST<jupyter_code>import os
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow.contrib.tensorboard.plugins import projector<jupyter_output><empty_output><jupyter_text>Importing the data from pandas and converting it into numpy array<jupyter_code>test_data = np.array(pd.read_csv(r'../data/FMNIST/fashion-mnist_test.csv'), dtype='float32')
embed_count = 1600 #number of images on embedding
x_test = test_data[:embed_count, 1:] / 255
y_test = test_data[:embed_count, 0]

logdir = r'D:\Anaconda36\py36\DL-CV-TF-Keras\data\logs\Embeddings'<jupyter_output><empty_output><jupyter_text>Setup the write and embedding tensor<jupyter_code>summary_writer = tf.summary.FileWriter(logdir)

embedding_var = tf.Variable(x_test, name='fmnist_embedding')

config = projector.ProjectorConfig()
embedding = config.embeddings.add()
embedding.tensor_name = embedding_var.name

embedding.metadata_path = os.path.join(logdir, 'metadata.tsv')
embedding.sprite.image_path = os.path.join(logdir, 'sprite.png')
embedding.sprite.single_image_dim.extend([28, 28])

projector.visualize_embeddings(summary_writer, config)<jupyter_output><empty_output><jupyter_text>Run the sesion to create the model check point<jupyter_code>with tf.Session() as sess:  
    sess.run(tf.global_variables_initializer())
    saver = tf.train.Saver()
    saver.save(sess, os.path.join(logdir, 'model.ckpt'))<jupyter_output><empty_output><jupyter_text>Create the sprite image and the metadata file:<jupyter_code>rows = 28
cols = 28

label = ['t_shirt', 'trouser', 'pullover', 'dress', 'coat',
          'sandal', 'shirt', 'sneaker', 'bag', 'ankle_boot']

sprite_dim = int(np.sqrt(x_test.shape[0]))

sprite_image = np.ones((cols * sprite_dim, rows * sprite_dim))

index = 0
labels = []
for i in range(sprite_dim):
    for j in range(sprite_dim):
        
        labels.append(label[int(y_test[index])])
        
        sprite_image[
            i * cols: (i + 1) * cols,
            j * rows: (j + 1) * rows
        ] = x_test[index].reshape(28, 28) * -1 + 1
        
        index += 1
        
with open(embedding.metadata_path, 'w') as meta:
    meta.write('Index\tLabel\n')
    for index, label in enumerate(labels):
        meta.write('{}\t{}\n'.format(index, label))

plt.figure(figsize=(12,12))        
plt.imsave(embedding.sprite.image_path, sprite_image, cmap='gray')
plt.imshow(sprite_image, cmap='gray')
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13309636650868878, 'fraction_numerical': 0.015007898894154818, 'mean_word_length': 4.8229885057471265, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '205205', 'license': 'MIT', 'n_tokens_mistral': 955, 'n_tokens_neox': 899, 'n_words': 173, 'path': 'Computer vision/Embeddings Visualization with Fashion MNIST.ipynb', 'repo_name': 'zbs881314/Deep-Learning-and-Computer-Vision', 'size': '2532'}