使用预加载网络的输出可视化Tensorboard上的嵌入

时间:2017-04-04 14:27:59

标签: tensorflow tensorboard

我想知道如何在Tensorboard中可视化来自预加载网络的嵌入。我正在使用FaceNet为面孔创建嵌入,我已经创建了sprite.pnglabels.tsv文件。至于加载网络和设置Tensorboard,这是我到目前为止所做的:

1。加载嵌入图层

meta_file, ckpt_file = facenet.get_model_filenames(MODEL_DIR)
with tf.Graph().as_default():
    with tf.Session().as_default() as sess:
        # load the network 
        model_dir_exp = os.path.expanduser(MODEL_DIR)
        saver = tf.train.import_meta_graph(os.path.join(model_dir_exp, meta_file))
        saver.restore(tf.get_default_session(), os.path.join(model_dir_exp, ckpt_file))

        # setup the lambda function needed to get the embeddings
        images_placeholder = tf.get_default_graph().get_tensor_by_name("input:0")
        embeddings = tf.get_default_graph().get_tensor_by_name("embeddings:0")
        phase_train_placeholder = tf.get_default_graph().get_tensor_by_name("phase_train:0")
        find_embeddings = lambda img : sess.run(embeddings, feed_dict = {images_placeholder : img, phase_train_placeholder : False})

2。找到嵌入

face_embeddings = np.zeros((n_images,128))
face_embeddings = []
for i in range(n_batches):
    start = i * batch_size
    end = min((i + 1) * batch_size, n_images)

    # Get the embeddings
    face_embeddings[start:end, :] = find_embeddings(face_images[start:end])

第3。设置Tensorboard

from tensorflow.contrib.tensorboard.plugins import projector

embedding = tf.Variable(tf.zeros([33, 128]), name = "embedding")
config = projector.ProjectorConfig()
embedding_config = config.embeddings.add()
embedding_config.tensor_name = embedding.name
embedding_config.metadata_path = os.path.join(MODEL_DIR, 'labels.tsv')
embedding_config.sprite.image_path = os.path.join(MODEL_DIR,'sprite.png')
embedding_config.sprite.single_image_dim.extend([160, 160])

writer = tf.summary.FileWriter(MODEL_DIR)
projector.visualize_embeddings(writer, config)

虽然当我在Tensorboard中加载它时,它说它无法找到数据。我查看了FAQ,当我跑find MODEL_DIR | grep tfevents时,没有任何显示,所以我猜这是问题所在。我看了MNIST Tutorial,看起来他们在训练过程中有检查点,虽然我没有,因为我正在使用预训练模型。在这种情况下,我会如何使Tensorboard显示嵌入?

0 个答案:

没有答案