-
Notifications
You must be signed in to change notification settings - Fork 155
/
models_to_frozen_graphs.py
77 lines (62 loc) · 2.42 KB
/
models_to_frozen_graphs.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
# Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved.
# Full license terms provided in LICENSE.md file.
import tensorflow as tf
import sys
sys.path.append("third_party/models/research/")
sys.path.append("third_party/models")
sys.path.append("third_party/")
sys.path.append("third_party/models/research/slim/")
sys.path.append("scripts")
import tensorflow.contrib.slim as tf_slim
import slim.nets as nets
import slim.nets.vgg
from model_meta import NETS, CHECKPOINT_DIR, FROZEN_GRAPHS_DIR
from convert_relu6 import convertRelu6
import os
if __name__ == '__main__':
if not os.path.exists(CHECKPOINT_DIR):
print("%s does not exist. Exiting." % CHECKPOINT_DIR)
exit()
if not os.path.exists(FROZEN_GRAPHS_DIR):
print("%s does not exist. Creating it now." % FROZEN_GRAPHS_DIR)
os.makedirs(FROZEN_GRAPHS_DIR)
for net_name, net_meta in NETS.items():
if 'exclude' in net_meta.keys() and net_meta['exclude'] is True:
continue
print("Converting %s" % net_name)
print(net_meta)
tf.reset_default_graph()
tf_config = tf.ConfigProto()
tf_config.gpu_options.allow_growth = True
with tf.Session(config=tf_config) as tf_sess:
tf_sess = tf.Session(config=tf_config)
tf_input = tf.placeholder(
tf.float32,
(
None,
net_meta['input_height'],
net_meta['input_width'],
net_meta['input_channels']
),
name=net_meta['input_name']
)
with tf_slim.arg_scope(net_meta['arg_scope']()):
tf_net, tf_end_points = net_meta['model'](
tf_input,
is_training=False,
num_classes=net_meta['num_classes']
)
tf_saver = tf.train.Saver()
tf_saver.restore(
save_path=net_meta['checkpoint_filename'],
sess=tf_sess
)
frozen_graph = tf.graph_util.convert_variables_to_constants(
tf_sess,
tf_sess.graph_def,
output_node_names=net_meta['output_names']
)
frozen_graph = convertRelu6(frozen_graph)
with open(net_meta['frozen_graph_filename'], 'wb') as f:
f.write(frozen_graph.SerializeToString())
f.close()