Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. import os
  2.  
  3. from keras import backend as K
  4. import tensorflow as tf
  5.  
  6. def freeze_session(session, keep_var_names=None, output_names=None, clear_devices=True):
  7. """
  8. Freezes the state of a session into a pruned computation graph.
  9.  
  10. Creates a new computation graph where variable nodes are replaced by
  11. constants taking their current value in the session. The new graph will be
  12. pruned so subgraphs that are not necessary to compute the requested
  13. outputs are removed.
  14. @param session The TensorFlow session to be frozen.
  15. @param keep_var_names A list of variable names that should not be frozen,
  16. or None to freeze all the variables in the graph.
  17. @param output_names Names of the relevant graph outputs.
  18. @param clear_devices Remove the device directives from the graph for better portability.
  19. @return The frozen graph definition.
  20. """
  21. graph = session.graph
  22. with graph.as_default():
  23. freeze_var_names = list(set(v.op.name for v in tf.global_variables()).difference(keep_var_names or []))
  24. output_names = output_names or []
  25. output_names += [v.op.name for v in tf.global_variables()]
  26. input_graph_def = graph.as_graph_def()
  27. if clear_devices:
  28. for node in input_graph_def.node:
  29. node.device = ""
  30. frozen_graph = tf.graph_util.convert_variables_to_constants(
  31. session, input_graph_def, output_names, freeze_var_names)
  32. return frozen_graph
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement