Advertisement
Guest User

Untitled

a guest
Aug 18th, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  1. """Imports a SavedModel as a graph in Tensorboard."""
  2.  
  3. from __future__ import absolute_import
  4. from __future__ import division
  5. from __future__ import print_function
  6.  
  7. import argparse
  8. import sys
  9.  
  10. from tensorflow.core.framework import graph_pb2
  11. from tensorflow.python.client import session
  12. from tensorflow.python.framework import importer
  13. from tensorflow.python.framework import ops
  14. from tensorflow.python.platform import app
  15. from tensorflow.python.platform import gfile
  16. from tensorflow.python.summary import summary
  17. from tensorflow import saved_model
  18.  
  19. # Try importing TensorRT ops if available
  20. # TODO(aaroey): ideally we should import everything from contrib, but currently
  21. # tensorrt module would cause build errors when being imported in
  22. # tensorflow/contrib/__init__.py. Fix it.
  23. # pylint: disable=unused-import,g-import-not-at-top,wildcard-import
  24. try:
  25. from tensorflow.contrib.tensorrt.ops.gen_trt_engine_op import *
  26. except ImportError:
  27. pass
  28. # pylint: enable=unused-import,g-import-not-at-top,wildcard-import
  29.  
  30.  
  31. def import_to_tensorboard(model_dir, log_dir):
  32. """View an imported SavedModel model as a graph in Tensorboard.
  33.  
  34. Args:
  35. model_dir: The location of the SavedModel model to visualize
  36. log_dir: The location for the Tensorboard log to begin visualization from.
  37.  
  38. Usage:
  39. Call this function with your model location and desired log directory.
  40. Launch Tensorboard by pointing it to the log directory.
  41. View your imported SavedModel model as a graph.
  42. """
  43. with session.Session(graph=ops.Graph()) as sess:
  44. # Restore model from the saved_model file, that is exported by TensorFlow estimator.
  45. saved_model.loader.load(sess, ["serve"], model_dir)
  46. pb_visual_writer = summary.FileWriter(log_dir)
  47. pb_visual_writer.add_graph(sess.graph)
  48. print("Model Imported. Visualize by running: "
  49. "tensorboard --logdir={}".format(log_dir))
  50.  
  51.  
  52. def main(unused_args):
  53. import_to_tensorboard(FLAGS.model_dir, FLAGS.log_dir)
  54.  
  55.  
  56. if __name__ == "__main__":
  57. parser = argparse.ArgumentParser()
  58. parser.register("type", "bool", lambda v: v.lower() == "true")
  59. parser.add_argument(
  60. "--model_dir",
  61. type=str,
  62. default="",
  63. required=True,
  64. help="The location of the SavedModel model to visualize.")
  65. parser.add_argument(
  66. "--log_dir",
  67. type=str,
  68. default="",
  69. required=True,
  70. help="The location for the Tensorboard log to begin visualization from.")
  71. FLAGS, unparsed = parser.parse_known_args()
  72. app.run(main=main, argv=[sys.argv[0]] + unparsed)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement