python - 从 onnx 文件中查找输入形状
问题描述
如何找到 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()
推荐阅读
- javascript - 为什么 textarea 中的选择范围重置为 0?
- node.js - TypeError:无法读取 null 的属性“会话”
- python - 如何从 Django 服务器与外部 python 脚本通信
- amazon-eks - EKS 1.11 和 HPA
- r - 在 R 中更改晶格密度图 Y 轴上的科学记数法
- keras - 使用 keras 的多层自动编码器,指定不同的优化器
- php - 从 WordPress MySQL user_meta 表和我的自定义表中获取数据
- ios - UILabel TapGesture 未触发
- minecraft - Minecraft 中 vannila 命令块的 If 语句
- django - 如何将 Django 的翻译字符串与 Wagtailtrans 一起使用(支持多语言 Wagtail 站点的附加组件)