ONE - On-device Neural Engine
Loading...
Searching...
No Matches
onnx-dump.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2
3# Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17import onnx
18import os
19import sys
20
21from onnx import AttributeProto, TensorProto
22from onnx import numpy_helper
23from onnx import helper
24
25
26def _data_type_str(data_type):
27 return TensorProto.DataType.Name(data_type)
28
29
31 if attr.type == AttributeProto.TENSOR:
32 return "{}, {}".format(_data_type_str(attr.t.data_type),
33 numpy_helper.to_array(attr.t))
34 if attr.type == AttributeProto.GRAPH:
35 # TODO revise when graph node is available
36 return "<graph>"
37 if attr.type == AttributeProto.TENSORS:
38 # TODO revise to see contents
39 return "<tensors>..."
40 if attr.type == AttributeProto.GRAPHS:
41 # TODO revise when graph node is available
42 return "<graphs>..."
43 return helper.get_attribute_value(attr)
44
45
46def _gather_value_infos(onnx_model):
47 vis = dict()
48
49 for mod_input in onnx_model.graph.input:
50 vis[mod_input.name] = mod_input.type
51
52 for mod_output in onnx_model.graph.output:
53 vis[mod_output.name] = mod_output.type
54
55 for vi in onnx_model.graph.value_info:
56 vis[vi.name] = vi.type
57
58 return vis
59
60
61def _type_format(type):
62 dtstr = _data_type_str(type.tensor_type.elem_type)
63 shape = type.tensor_type.shape
64 shape_ar = [dim.dim_value for dim in shape.dim]
65 return '{} {}'.format(dtstr, shape_ar)
66
67
68def _dump_header(onnx_model):
69 print("[General] -----------------------------")
70 print("IR version =", onnx_model.ir_version)
71 print("Producer =", onnx_model.producer_name, onnx_model.producer_version)
72 print("")
73
74
75def _dump_operators(onnx_model):
76 opcodes_dict = dict()
77 for node in onnx_model.graph.node:
78 if node.op_type in opcodes_dict:
79 opcodes_dict[node.op_type] = opcodes_dict[node.op_type] + 1
80 else:
81 opcodes_dict[node.op_type] = 1
82
83 print("[Operators] ---------------------------")
84 total_nodes = 0
85 for opcode_key in opcodes_dict:
86 print("{:>5} {}".format(opcodes_dict[opcode_key], opcode_key))
87 total_nodes = total_nodes + opcodes_dict[opcode_key]
88
89 print("----- -----")
90 print("{:>5} {}".format(total_nodes, 'Total'))
91
92 print("")
93
94
95def _dump_initializers(onnx_model):
96 print("[Initializers] ------------------------")
97 for initializer in onnx_model.graph.initializer:
98 init_name = '"{}"'.format(initializer.name)
99 dtstr = _data_type_str(initializer.data_type)
100 print('{:<15} {} {}'.format(init_name, dtstr, initializer.dims))
101
102 print("")
103
104
105def _dump_nodes(onnx_model):
106 print("[Nodes] -------------------------------")
107
108 vis = _gather_value_infos(onnx_model)
109
110 for node in onnx_model.graph.node:
111 print('{0}("{1}")'.format(node.op_type, node.name))
112
113 attribute = ''
114 for attr in node.attribute:
115 if attribute != '':
116 attribute += ', '
117 attribute += "{}: {}".format(attr.name, _get_attribute_value(attr))
118
119 if attribute != '':
120 print(' A {0}'.format(attribute))
121
122 for inp in node.input:
123 inp_vi_str = ''
124 if inp in vis:
125 inp_vi = vis[inp]
126 inp_vi_str = _type_format(inp_vi)
127 print(' I "{0}" {1}'.format(inp, inp_vi_str))
128 for out in node.output:
129 out_vi_str = ''
130 if out in vis:
131 out_vi = vis[out]
132 out_vi_str = _type_format(out_vi)
133 print(' O "{0}" {1}'.format(out, out_vi_str))
134
135 print("")
136
137
138def _dump_inputoutputs(onnx_model):
139 print("[Graph Input/Output]-------------------")
140 for mod_input in onnx_model.graph.input:
141 io_name = '"{}"'.format(mod_input.name)
142 dtstr = _data_type_str(mod_input.type.tensor_type.elem_type)
143 shape = mod_input.type.tensor_type.shape
144 input_shape = [dim.dim_value for dim in shape.dim]
145 print(' I: {:<15} {} {}'.format(io_name, dtstr, input_shape))
146
147 for mod_output in onnx_model.graph.output:
148 io_name = '"{}"'.format(mod_output.name)
149 dtstr = _data_type_str(mod_output.type.tensor_type.elem_type)
150 shape = mod_output.type.tensor_type.shape
151 output_shape = [dim.dim_value for dim in shape.dim]
152 print(' O: {:<15} {} {}'.format(io_name, dtstr, output_shape))
153
154 print("")
155
156
157def _dump_graph(onnx_model):
158 _dump_header(onnx_model)
159 _dump_operators(onnx_model)
160 _dump_initializers(onnx_model)
161 _dump_nodes(onnx_model)
162 _dump_inputoutputs(onnx_model)
163
164
165def _help_exit(cmd_name):
166 print('Dump ONNX model file Graph')
167 print('Usage: {0} [onnx_path]'.format(cmd_name))
168 print('')
169 exit()
170
171
172def main():
173 if len(sys.argv) < 2:
174 _help_exit(os.path.basename(sys.argv[0]))
175
176 onnx.checker.check_model(sys.argv[1])
177 onnx_model = onnx.load(sys.argv[1])
178
179 _dump_graph(onnx_model)
180
181
182if __name__ == "__main__":
183 main()
_type_format(type)
Definition onnx-dump.py:61
_data_type_str(data_type)
Definition onnx-dump.py:26
_dump_initializers(onnx_model)
Definition onnx-dump.py:95
_help_exit(cmd_name)
Definition onnx-dump.py:165
_dump_graph(onnx_model)
Definition onnx-dump.py:157
_get_attribute_value(attr)
Definition onnx-dump.py:30
_dump_header(onnx_model)
Definition onnx-dump.py:68
_dump_nodes(onnx_model)
Definition onnx-dump.py:105
_dump_operators(onnx_model)
Definition onnx-dump.py:75
_dump_inputoutputs(onnx_model)
Definition onnx-dump.py:138
_gather_value_infos(onnx_model)
Definition onnx-dump.py:46