Onnx: Get the input and output node name from onnx model

Created on 13 Mar 2020  路  4Comments  路  Source: onnx/onnx

Hi all,
Is there any tool or method which can let us rapidly know the input/ output node names of onnx model?
Because I know there are some good tools which can analyze for TensorFlow pb model such as saved_model_cli or summarize graph. It can not only know the input/ output node names but also output this model uses what total operations are.

I really need to confirm the input and output node names when I get a onnx model which is not generated by myself.
BTW I already knew the netron this awesome tool. :))

If you have any idea, I will be grateful for any help you can provide.
Thank you!

Most helpful comment

@chiehpower, if you haven't resolved this already, simply extracting the name attribute of each node in input_all and input_initializer resolves this error.

Solution:

model = onnx.load('xxx.onnx')
output =[node.name for node in model.graph.output]

input_all = [node.name for node in model.graph.input]
input_initializer =  [node.name for node in model.graph.initializer]
net_feed_input = list(set(input_all)  - set(input_initializer))

print('Inputs: ', net_feed_input)
print('Outputs: ', output)

All 4 comments

model = onnx.load('xxx.onnx')
output = model.graph.output

input_all = model.graph.input
input_initializer = model.graph.initializer
net_feed_input = set(input_all)  - (input_initializer)

model = onnx.load('xxx.onnx')
output = model.graph.output

input_all = model.graph.input
input_initializer = model.graph.initializer
net_feed_input = set(input_all)  - (input_initializer)

Hi @austingg,

Thanks for your reply!!
After I used your code, I got this error message.

Traceback (most recent call last):
  File "test.py", line 7, in <module>
    net_feed_input = set(input_all)  - (input_initializer)
TypeError: unhashable type: 'ValueInfoProto'

@chiehpower, if you haven't resolved this already, simply extracting the name attribute of each node in input_all and input_initializer resolves this error.

Solution:

model = onnx.load('xxx.onnx')
output =[node.name for node in model.graph.output]

input_all = [node.name for node in model.graph.input]
input_initializer =  [node.name for node in model.graph.initializer]
net_feed_input = list(set(input_all)  - set(input_initializer))

print('Inputs: ', net_feed_input)
print('Outputs: ', output)

@chiehpower, if you haven't resolved this already, simply extracting the name attribute of each node in input_all and input_initializer resolves this error.

Solution:

model = onnx.load('xxx.onnx')
output =[node.name for node in model.graph.output]

input_all = [node.name for node in model.graph.input]
input_initializer =  [node.name for node in model.graph.initializer]
net_feed_input = list(set(input_all)  - set(input_initializer))

print('Inputs: ', net_feed_input)
print('Outputs: ', output)

Hi @gabrielibagon,

Thanks for your sharing!!
It is really useful for me. :)

Was this page helpful?
0 / 5 - 0 ratings