17
17
18
18
def freeze_session (sess , keep_var_names = None , output_names = None , clear_devices = True ):
19
19
"""Freezes the state of a session into a pruned computation graph."""
20
- output_names = [i .replace ( ":0" , "" ) for i in output_names ]
20
+ output_names = [i .split ( ':' )[: - 1 ][ 0 ] for i in output_names ]
21
21
graph = sess .graph
22
22
with graph .as_default ():
23
23
freeze_var_names = list (set (v .op .name for v in tf .global_variables ()).difference (keep_var_names or []))
@@ -42,7 +42,7 @@ def from_graphdef(model_path, input_names, output_names):
42
42
graph_def .ParseFromString (f .read ())
43
43
tf .import_graph_def (graph_def , name = '' )
44
44
frozen_graph = freeze_session (sess , output_names = output_names )
45
- # clean up after us
45
+ # clean up
46
46
tf .reset_default_graph ()
47
47
return frozen_graph , input_names , output_names
48
48
@@ -57,7 +57,7 @@ def from_checkpoint(model_path, input_names, output_names):
57
57
# restore from model_path minus the ".meta"
58
58
saver .restore (sess , model_path [:- 5 ])
59
59
frozen_graph = freeze_session (sess , output_names = output_names )
60
- # clean up after us
60
+ # clean up
61
61
tf .reset_default_graph ()
62
62
return frozen_graph , input_names , output_names
63
63
@@ -86,12 +86,7 @@ def from_saved_model(model_path, input_names, output_names):
86
86
outputs_tensor_info = get_signature_def (meta_graph_def , k ).outputs
87
87
for _ , output_tensor in sorted (outputs_tensor_info .items ()):
88
88
outputs [output_tensor .name ] = sess .graph .get_tensor_by_name (output_tensor .name )
89
- # freeze uses the node name derived from output:0 so only pass in output:0;
90
- # it will provide all outputs of that node.
91
- for o in list (outputs .keys ()):
92
- if not o .endswith (":0" ):
93
- del outputs [o ]
94
89
frozen_graph = freeze_session (sess , output_names = list (outputs .keys ()))
95
- # clean up after us
90
+ # clean up
96
91
tf .reset_default_graph ()
97
92
return frozen_graph , inputs .keys (), outputs .keys ()
0 commit comments