diff --git a/.DS_Store b/.DS_Store index 940d418..e25cadc 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/code/.DS_Store b/code/.DS_Store index d5f3573..446c860 100644 Binary files a/code/.DS_Store and b/code/.DS_Store differ diff --git a/code/tensorflow-tools/.DS_Store b/code/tensorflow-tools/.DS_Store index 6205b31..bb11c74 100644 Binary files a/code/tensorflow-tools/.DS_Store and b/code/tensorflow-tools/.DS_Store differ diff --git a/code/tensorflow-tools/tensorflow_embeddings.py b/code/tensorflow-tools/tensorflow_embeddings.py index 0963c96..d9f054f 100644 --- a/code/tensorflow-tools/tensorflow_embeddings.py +++ b/code/tensorflow-tools/tensorflow_embeddings.py @@ -23,7 +23,7 @@ '''可视化embedding, 3个步骤''' with tf.Session() as sess: - '''1、 将2D矩阵放入Variable中国''' + '''1、 将2D矩阵放入Variable中''' embeddings_var = tf.Variable(plot_array, name='embedding') tf.global_variables_initializer().run() diff --git a/code/triplet-loss/.DS_Store b/code/triplet-loss/.DS_Store index 4e4a71d..d2ca5e4 100644 Binary files a/code/triplet-loss/.DS_Store and b/code/triplet-loss/.DS_Store differ diff --git a/code/triplet-loss/experiment/mnist_10k_sprite.png b/code/triplet-loss/experiment/mnist_10k_sprite.png new file mode 100644 index 0000000..2bbdec9 Binary files /dev/null and b/code/triplet-loss/experiment/mnist_10k_sprite.png differ diff --git a/code/triplet-loss/experiment/model/metadata.tsv b/code/triplet-loss/experiment/model/metadata.tsv new file mode 100644 index 0000000..b6141b4 --- /dev/null +++ b/code/triplet-loss/experiment/model/metadata.tsv @@ -0,0 +1,1000 @@ +7 +2 +1 +0 +4 +1 +4 +9 +5 +9 +0 +6 +9 +0 +1 +5 +9 +7 +3 +4 +9 +6 +6 +5 +4 +0 +7 +4 +0 +1 +3 +1 +3 +4 +7 +2 +7 +1 +2 +1 +1 +7 +4 +2 +3 +5 +1 +2 +4 +4 +6 +3 +5 +5 +6 +0 +4 +1 +9 +5 +7 +8 +9 +3 +7 +4 +6 +4 +3 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 +4 +3 +1 +1 +7 +4 +9 +9 +4 +8 +4 +0 +2 +4 +5 +1 +1 +6 +4 +7 +1 +9 +4 +2 +4 +1 +5 +5 +3 +8 +3 +1 +4 +5 +6 +8 +9 +4 +1 +5 +3 +8 +0 +3 +2 +5 +1 +2 +8 +3 +4 +4 +0 +8 +8 +3 +3 +1 +7 +3 +5 +9 +6 +3 +2 +6 +1 +3 +6 +0 +7 +2 +1 +7 +1 +4 +2 +4 +2 +1 +7 +9 +6 +1 +1 +2 +4 +8 +1 +7 +7 +4 +8 +0 +7 +3 +1 +3 +1 +0 +7 +7 +0 +3 +5 +5 +2 +7 +6 +6 +9 +2 +8 +3 +5 +2 +2 +5 +6 +0 +8 +2 +9 +2 +8 +8 +8 +8 +7 +4 +9 +3 +0 +6 +6 +3 +2 +1 +3 +2 +2 +9 +3 +0 +0 +5 +7 +8 +1 +4 +4 +6 +0 +2 +9 +1 +4 +7 +4 +7 +3 +9 +8 +8 +4 +7 +1 +2 +1 +2 +2 +3 +2 +3 +2 +3 +9 +1 +7 +4 +0 +3 +5 +5 +8 +6 +3 +2 +6 +7 +6 +6 +3 +2 +7 +8 +1 +1 +7 +5 +6 +4 +9 +5 +1 +3 +3 +4 +7 +8 +9 +1 +1 +6 +9 +1 +4 +4 +5 +4 +0 +6 +2 +2 +3 +1 +5 +1 +2 +0 +3 +8 +1 +2 +6 +7 +1 +6 +2 +3 +9 +0 +1 +2 +2 +0 +8 +9 diff --git a/code/triplet-loss/visualize_embeddings.py b/code/triplet-loss/visualize_embeddings.py new file mode 100644 index 0000000..b2569c9 --- /dev/null +++ b/code/triplet-loss/visualize_embeddings.py @@ -0,0 +1,89 @@ +# -*- coding: utf-8 -*- +# Author: Lawlite +# Date: 2018/10/20 +# Associate Blog: http://lawlite.me/2018/10/16/Triplet-Loss原理及其实现/#more +# License: MIT + +import os +import numpy as np +import tensorflow as tf +import argparse +from triplet_loss import batch_all_triplet_loss +from triplet_loss import batch_hard_triplet_loss +import mnist_dataset +from train_with_triplet_loss import my_model +from train_with_triplet_loss import test_input_fn +from tensorflow.contrib.tensorboard.plugins import projector + +parser = argparse.ArgumentParser() +parser.add_argument('--data_dir', default='data',type=str, help="数据地址") +parser.add_argument('--model_dir', default='experiment/model', type=str, help="模型地址") +parser.add_argument('--sprite_filename', default='experiments/mnist_10k_sprite.png', + help="Sprite image for the projector") + +def main(argv): + args = parser.parse_args(argv[1:]) + params = { + "learning_rate": 1e-3, + "batch_size": 64, + "num_epochs": 20, + + "num_channels": 32, + "use_batch_norm": False, + "bn_momentum": 0.9, + "margin": 0.5, + "embedding_size": 64, + "triplet_strategy": "batch_hard", + "squared": False, + + "image_size": 28, + "num_labels": 10, + "train_size": 50000, + "eval_size": 1000, + + "num_parallel_calls": 4 + } + tf.logging.info("创建模型....") + config = tf.estimator.RunConfig(model_dir=args.model_dir, tf_random_seed=100) # config + cls = tf.estimator.Estimator(model_fn=my_model, config=config, params=params) # 建立模型 + + tf.logging.info("预测....") + + predictions = cls.predict(input_fn=lambda: test_input_fn(args.data_dir, params)) + embeddings = np.zeros((params['eval_size'], params['embedding_size'])) + for i, p in enumerate(predictions): + if i>=params['eval_size']: + break + embeddings[i] = p['embeddings'] + tf.logging.info("embeddings shape: {}".format(embeddings.shape)) + + with tf.Session() as sess: + # Obtain the test labels + dataset = mnist_dataset.test(args.data_dir) + dataset = dataset.map(lambda img, lab: lab) + dataset = dataset.batch(params['eval_size']) + labels_tensor = dataset.make_one_shot_iterator().get_next() + labels = sess.run(labels_tensor) + np.savetxt(os.path.join(args.model_dir, 'metadata.tsv'), labels, fmt='%d') + + with tf.Session() as sess: + embedding_var = tf.Variable(embeddings, name="mnist_embeddings") + tf.global_variables_initializer().run() + + saver = tf.train.Saver([embedding_var]) + sess.run(embedding_var.initializer) + saver.save(sess, os.path.join(args.model_dir, 'embeddings.ckpt'), global_step=0) + + summary_writer = tf.summary.FileWriter(args.model_dir) + config = projector.ProjectorConfig() + embedding = config.embeddings.add() + embedding.tensor_name = embeddings_var.name + embedding.metadata_path = 'mnist_10k_sprite.png' + embedding.sprite.image_path = 'metadata.tsv' + embedding.sprite.single_image_dim.extend([28, 28]) + projector.visualize_embeddings(summary_writer, config) + + +if __name__ == '__main__': + tf.app.run(main) +