Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@AndrewBMartin
Last active June 21, 2019 17:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AndrewBMartin/53f4633f1ac0697d950183629534053d to your computer and use it in GitHub Desktop.
Save AndrewBMartin/53f4633f1ac0697d950183629534053d to your computer and use it in GitHub Desktop.
Save feature vectors for projection with Tensorboard
import tensorflow as tf
from tensorflow.contrib.tensorboard.plugins import projector
LOG_DIR = "your-tensorboard-log-dir-here"
FEATURE_VECTORS = "your-feature-vectors-as-npy"
MAX_NUMBER_SAMPLES = 8191
METADATA_FILE = os.path.join(LOG_DIR, 'metadata.tsv')
CHECKPOINT_FILE = os.path.join(LOG_DIR, 'features.ckpt')
# Create metadata
# Can include class data in here if interested / have available
with open(METADATA_FILE, 'w+') as wrf:
wrf.write("\n".join([str(a) for a,i in enumerate(image_files[:MAX_NUMBER_SAMPLES])]))
feature_vectors = np.load(FEATURE_VECTORS)
features = tf.Variable(feature_vectors[:MAX_NUMBER_SAMPLES], name='features')
# Write summaries for tensorboard
with tf.Session() as sess:
saver = tf.train.Saver([features])
sess.run(features.initializer)
saver.save(sess, CHECKPOINT_FILE)
config = projector.ProjectorConfig()
embedding = config.embeddings.add()
embedding.tensor_name = features.name
embedding.metadata_path = METADATA_FILE
# This adds the sprite images
embedding.sprite.image_path = SPRITES_FILE
embedding.sprite.single_image_dim.extend(IMAGE_SIZE)
projector.visualize_embeddings(tf.summary.FileWriter(LOG_DIR), config)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment