forked from openvinotoolkit/open_model_zoo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
freeze_saved_model.py
43 lines (36 loc) · 1.5 KB
/
freeze_saved_model.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
import argparse
import tensorflow.compat.v1 as tf
from tensorflow.python.framework import graph_io
from tensorflow.python.tools import optimize_for_inference_lib
def parse_args():
"""Parse input arguments"""
parser = argparse.ArgumentParser(description='Freeze saved model')
parser.add_argument('--saved_model_dir', type=str, required=True,
help='Path to saved model directory.')
parser.add_argument('--save_file', type=str, required=True,
help='Path to resulting frozen model.')
return parser.parse_args()
def freeze(saved_model_dir, input_nodes, output_nodes, save_file):
graph_def = tf.Graph()
with tf.Session(graph=graph_def) as sess:
tf.saved_model.loader.load(sess, [tf.saved_model.tag_constants.SERVING], saved_model_dir)
frozen_graph_def = tf.graph_util.convert_variables_to_constants(
sess,
sess.graph_def,
output_nodes
)
frozen_graph_def = optimize_for_inference_lib.optimize_for_inference(
frozen_graph_def,
input_nodes,
output_nodes,
tf.float32.as_datatype_enum
)
with open(save_file, 'wb') as f:
f.write(frozen_graph_def.SerializeToString())
def main():
args = parse_args()
input_nodes = ['map/TensorArrayStack/TensorArrayGatherV3']
output_nodes = ['softmax_tensor']
freeze(args.saved_model_dir, input_nodes, output_nodes, args.save_file)
if __name__ == '__main__':
main()