gpt4 book ai didi

python - 从 onnx 文件中查找输入形状

转载 作者:行者123 更新时间:2023-12-03 18:44:53 27 4
gpt4 key购买 nike

如何找到 onnx 模型的输入大小?我最终想从 python 编写它的脚本。

使用 tensorflow 我可以恢复图定义,从中找到输入候选节点,然后获取它们的大小。我可以用 ONNX(甚至更简单)做类似的事情吗?

谢谢

最佳答案

是的,前提是输入模型具有信息。请注意,ONNX 模型的输入可能具有未知等级,或者可能具有具有固定维度(如 100)或符号(如“N”)或完全未知的已知等级。您可以按如下方式访问:

import onnx

model = onnx.load(r"model.onnx")

# The model is represented as a protobuf structure and it can be accessed
# using the standard python-for-protobuf methods

# iterate through inputs of the graph
for input in model.graph.input:
print (input.name, end=": ")
# get type of input tensor
tensor_type = input.type.tensor_type
# check if it has a shape:
if (tensor_type.HasField("shape")):
# iterate through dimensions of the shape:
for d in tensor_type.shape.dim:
# the dimension may have a definite (integer) value or a symbolic identifier or neither:
if (d.HasField("dim_value")):
print (d.dim_value, end=", ") # known dimension
elif (d.HasField("dim_param")):
print (d.dim_param, end=", ") # unknown dimension with symbolic name
else:
print ("?", end=", ") # unknown dimension with no name
else:
print ("unknown rank", end="")
print()

关于python - 从 onnx 文件中查找输入形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56734576/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com