-
Notifications
You must be signed in to change notification settings - Fork 0
/
output_summary_from_pb.py
57 lines (41 loc) · 1.58 KB
/
output_summary_from_pb.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
import os.path
import argparse
import tensorflow as tf
from tensorflow.core.framework.graph_pb2 import GraphDef
def load_graphdef_from_pb(pb_file):
graph = GraphDef()
with open(pb_file, 'rb') as f:
content = f.read()
try:
graph.ParseFromString(content)
except Exception as e:
raise IOError("Can't parse file {}: {}".format(pb_file, str(e)))
return graph
def load_graphdef_from_pbtxt(pbtxt_file):
graph = GraphDef()
with open(pbtxt_file, 'rb') as f:
content = f.read()
from google.protobuf import text_format
try:
text_format.Parse(content.decode('UTF-8'), graph,
allow_unknown_extension=True)
except Exception as e:
raise IOError("Can't parse file {}: {}".format(pbtxt_file, str(e)))
return graph
parser = argparse.ArgumentParser()
parser.add_argument("--input", nargs=1, type=str, action="store")
parser.add_argument("--output_dir", nargs=1, type=str, action="store")
args = parser.parse_args()
if (args.input is None) or (len(args.input) == 0):
raise RuntimeError("option '--input' must be specified")
if (args.output_dir is None) or (len(args.output_dir) == 0):
raise RuntimeError("option '--output_dir' must be specified")
_, ext = os.path.splitext(args.input[0])
graph = None
if ext == ".pbtxt":
graph = load_graphdef_from_pbtxt(args.input[0])
elif ext == ".pb":
graph = load_graphdef_from_pb(args.input[0])
else:
raise RuntimeError("Input file must be .pb or .pbtxt")
tf.summary.FileWriter(args.output_dir[0], graph)