1# Copyright 2016 The TensorFlow Authors. All Rights Reserved. 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14# ============================================================================== 15"""Public API for the Embedding Projector. 16 17@@ProjectorPluginAsset 18@@ProjectorConfig 19@@EmbeddingInfo 20@@EmbeddingMetadata 21@@SpriteMetadata 22""" 23 24from __future__ import absolute_import 25from __future__ import division 26from __future__ import print_function 27 28import os 29 30from google.protobuf import text_format 31from tensorflow.contrib.tensorboard.plugins.projector import projector_config_pb2 32# pylint: disable=wildcard-import 33from tensorflow.contrib.tensorboard.plugins.projector.projector_config_pb2 import * 34# pylint: enable=wildcard-import 35from tensorflow.python.lib.io import file_io 36 37 38def visualize_embeddings(summary_writer, config): 39 """Stores a config file used by the embedding projector. 40 41 Args: 42 summary_writer: The summary writer used for writing events. 43 config: `tf.contrib.tensorboard.plugins.projector.ProjectorConfig` 44 proto that holds the configuration for the projector such as paths to 45 checkpoint files and metadata files for the embeddings. If 46 `config.model_checkpoint_path` is none, it defaults to the 47 `logdir` used by the summary_writer. 48 49 Raises: 50 ValueError: If the summary writer does not have a `logdir`. 51 """ 52 logdir = summary_writer.get_logdir() 53 54 # Sanity checks. 55 if logdir is None: 56 raise ValueError('Summary writer must have a logdir') 57 58 # Saving the config file in the logdir. 59 config_pbtxt = text_format.MessageToString(config) 60 # FYI - the 'projector_config.pbtxt' string is hardcoded in the projector 61 # plugin. 62 # TODO(dandelion): Restore this to a reference to the projector plugin 63 file_io.write_string_to_file( 64 os.path.join(logdir, 'projector_config.pbtxt'), config_pbtxt) 65