-
Notifications
You must be signed in to change notification settings - Fork 9
/
freezer.py
78 lines (63 loc) · 2.51 KB
/
freezer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import os
import tensorflow as tf
FLAGS = tf.app.flags.FLAGS
tf.app.flags.DEFINE_string(
'ckpt_dir', '/model/archive_0710', 'path to checkpoint files')
def freeze_graph(model_dir, output_nodes):
"""Extract the sub graph defined by the output nodes and
convert all variables into constant
Args:
model_dir: string, folder containing target checkpoint files
output_nodes: list of string, name of output tensors
"""
if not tf.gfile.Exists(model_dir):
raise AssertionError('Target directory \'{}\' does not exist.'.format(
model_dir
))
if not output_nodes:
raise AssertionError('Output nodes not provided.')
checkpoint_state = tf.train.get_checkpoint_state(model_dir)
if not checkpoint_state:
raise AssertionError('No valid checkpoints found in directory' +
' \'{}\''.format(model_dir))
input_checkpoint = checkpoint_state.model_checkpoint_path
target_file = os.path.join(model_dir, 'frozen_model.pb')
clear_devices = True
with tf.Session(graph=tf.Graph()) as sess:
# restore metagraph
saver = tf.train.import_meta_graph(
input_checkpoint + '.meta', clear_devices=clear_devices
)
# restore weights
saver.restore(sess, input_checkpoint)
# for node in tf.get_default_graph().as_graph_def().node:
# print(node.name)
# convert variables to constants
output_graph_def = tf.graph_util.convert_variables_to_constants(
sess,
tf.get_default_graph().as_graph_def(),
output_nodes
)
# save graph to file
with tf.gfile.GFile(target_file, 'wb') as f:
f.write(output_graph_def.SerializeToString())
print('Write complete. {} ops in final graph'.format(
len(output_graph_def.node)))
return output_graph_def
def print_nodes(model_dir):
checkpoint_state = tf.train.get_checkpoint_state(model_dir)
input_checkpoint = checkpoint_state.model_checkpoint_path
with tf.Session(graph=tf.Graph()) as sess:
saver = tf.train.import_meta_graph(
input_checkpoint + '.meta', clear_devices=True)
[print(node.name) for node in tf.get_default_graph().as_graph_def().node]
return None
if __name__ == '__main__':
# model_dir = './model/archive_0710'
model_dir = FLAGS.ckpt_dir
output_nodes = [
'metrics/Sigmoid',
'metrics/Cast'
]
freeze_graph(model_dir, output_nodes)
# print_nodes(model_dir)