-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathfreeze.py
58 lines (47 loc) · 1.6 KB
/
freeze.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
import os
import tensorflow as tf
import numpy as np
import helpers
# Get flags
FLAGS = helpers.get_flags()
# Initialize data loader
data_loader_dict = helpers.setup_batch_loaders(
data_type=FLAGS.data,
mix_data_loaders=FLAGS.mix_data,
max_enc_seq_length=FLAGS.max_enc_seq_length,
max_dec_seq_length=FLAGS.max_dec_seq_length,
batch_size=FLAGS.batch_size,
random_seed=FLAGS.random_seed,
model=FLAGS.model.lower()
)
# Setup model
model = helpers.setup_model(
flags=FLAGS,
prediction_mode=True,
alphabet=data_loader_dict['alphabet'],
vocabulary=data_loader_dict['vocabulary'],
data_name=data_loader_dict['name'],
num_classes=data_loader_dict['num_classes']
)
config = tf.ConfigProto(log_device_placement=False)
config.gpu_options.allow_growth = True
with tf.Session(config=config) as sess:
# Initialize model
model.init(sess)
# We retrieve the protobuf graph definition
graph = tf.get_default_graph()
input_graph_def = graph.as_graph_def()
for name in [n.name for n in input_graph_def.node]:
print(name)
# We use a built-in TF helper to export variables to constants
output_graph_def = tf.graph_util.convert_variables_to_constants(
sess,
input_graph_def=input_graph_def,
output_node_names=model.output_node_names
)
# Finally we serialize and dump the output graph to the filesystem
with tf.gfile.GFile(model.config.frozen_model_path, 'wb') as f:
f.write(output_graph_def.SerializeToString())
print('%d ops in the final graph.' % (len(output_graph_def.node)))
del sess
tf.reset_default_graph()