_embedding.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import math
  2. import numpy as np
  3. from ._convert_np import make_np
  4. from ._utils import make_grid
  5. from tensorboard.compat import tf
  6. from tensorboard.plugins.projector.projector_config_pb2 import EmbeddingInfo
  7. _HAS_GFILE_JOIN = hasattr(tf.io.gfile, "join")
  8. def _gfile_join(a, b):
  9. # The join API is different between tensorboard's TF stub and TF:
  10. # https://github.com/tensorflow/tensorboard/issues/6080
  11. # We need to try both because `tf` may point to either the stub or the real TF.
  12. if _HAS_GFILE_JOIN:
  13. return tf.io.gfile.join(a, b)
  14. else:
  15. fs = tf.io.gfile.get_filesystem(a)
  16. return fs.join(a, b)
  17. def make_tsv(metadata, save_path, metadata_header=None):
  18. if not metadata_header:
  19. metadata = [str(x) for x in metadata]
  20. else:
  21. assert len(metadata_header) == len(
  22. metadata[0]
  23. ), "len of header must be equal to the number of columns in metadata"
  24. metadata = ["\t".join(str(e) for e in l) for l in [metadata_header] + metadata]
  25. metadata_bytes = tf.compat.as_bytes("\n".join(metadata) + "\n")
  26. with tf.io.gfile.GFile(_gfile_join(save_path, "metadata.tsv"), "wb") as f:
  27. f.write(metadata_bytes)
  28. # https://github.com/tensorflow/tensorboard/issues/44 image label will be squared
  29. def make_sprite(label_img, save_path):
  30. from PIL import Image
  31. from io import BytesIO
  32. # this ensures the sprite image has correct dimension as described in
  33. # https://www.tensorflow.org/get_started/embedding_viz
  34. nrow = int(math.ceil((label_img.size(0)) ** 0.5))
  35. arranged_img_CHW = make_grid(make_np(label_img), ncols=nrow)
  36. # augment images so that #images equals nrow*nrow
  37. arranged_augment_square_HWC = np.zeros(
  38. (arranged_img_CHW.shape[2], arranged_img_CHW.shape[2], 3)
  39. )
  40. arranged_img_HWC = arranged_img_CHW.transpose(1, 2, 0) # chw -> hwc
  41. arranged_augment_square_HWC[: arranged_img_HWC.shape[0], :, :] = arranged_img_HWC
  42. im = Image.fromarray(np.uint8((arranged_augment_square_HWC * 255).clip(0, 255)))
  43. with BytesIO() as buf:
  44. im.save(buf, format="PNG")
  45. im_bytes = buf.getvalue()
  46. with tf.io.gfile.GFile(_gfile_join(save_path, "sprite.png"), "wb") as f:
  47. f.write(im_bytes)
  48. def get_embedding_info(metadata, label_img, subdir, global_step, tag):
  49. info = EmbeddingInfo()
  50. info.tensor_name = "{}:{}".format(tag, str(global_step).zfill(5))
  51. info.tensor_path = _gfile_join(subdir, "tensors.tsv")
  52. if metadata is not None:
  53. info.metadata_path = _gfile_join(subdir, "metadata.tsv")
  54. if label_img is not None:
  55. info.sprite.image_path = _gfile_join(subdir, "sprite.png")
  56. info.sprite.single_image_dim.extend([label_img.size(3), label_img.size(2)])
  57. return info
  58. def write_pbtxt(save_path, contents):
  59. config_path = _gfile_join(save_path, "projector_config.pbtxt")
  60. with tf.io.gfile.GFile(config_path, "wb") as f:
  61. f.write(tf.compat.as_bytes(contents))
  62. def make_mat(matlist, save_path):
  63. with tf.io.gfile.GFile(_gfile_join(save_path, "tensors.tsv"), "wb") as f:
  64. for x in matlist:
  65. x = [str(i.item()) for i in x]
  66. f.write(tf.compat.as_bytes("\t".join(x) + "\n"))