当前位置:网站首页>【onnx 输入尺寸】修改pytorch生成的onnx模型的输入尺寸
【onnx 输入尺寸】修改pytorch生成的onnx模型的输入尺寸
2022-08-03 05:27:00 【寻找永不遗憾】
1 问题描述
224x224的onnx模型,想把它的输入改成520x520的,怎么办呢?
2 代码
import sys
import argparse
import onnx
from onnx import shape_inference
def conv_model_input_size(converted_model, size1, size2):
# print(converted_model.graph.input)
# exit()
for i, node in enumerate(converted_model.graph.input):
if i == 0:
print("Before changing input size: {}".format(node.type.tensor_type.shape))
print("dim:{}".format(node.type.tensor_type.shape.dim[1].dim_param))
if node.type.tensor_type.shape.dim[1].dim_value == 3 :
# NCHW
node.type.tensor_type.shape.dim[2].dim_value= size1
node.type.tensor_type.shape.dim[3].dim_value= size2
elif node.type.tensor_type.shape.dim[3].dim_value== 3 :
# NHWC
node.type.tensor_type.shape.dim[1].dim_value= size1
node.type.tensor_type.shape.dim[2].dim_value= size2
else:
print("ERROR: Not supported input shape")
return
print("After changing input size: {}".format(node.type.tensor_type.shape))
# onnx.save(converted_model, dst_fullname)
# exit()
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-i", "--input", default="./efficientnet_lite0.onnx", help="The input onnx model")
parser.add_argument("-a", "--action", default="size", choices=["size","dyn"],
help="Select command action")
parser.add_argument("-s1", "--size1", default="520", help="Input size, for action=size")
parser.add_argument("-s2", "--size2", default="520", help="Input size, for action=size")
args = parser.parse_args()
if not all([args.input, args.action]):
parser.print_help()
sys.exit(1)
src_root = args.input
print(src_root)
if args.action == "size":
if not src_root.endswith('.onnx'):
exit()
converted_model = onnx.load(src_root)
input_size1 = converted_model.graph.input[0].type.tensor_type.shape.dim[2].dim_value
input_size2 = converted_model.graph.input[0].type.tensor_type.shape.dim[3].dim_value
print("len of inputs:{}".format(len(converted_model.graph.input)))
# 修改input size
conv_model_input_size(converted_model, int(args.size1), int(args.size2))
# 疑问,函数明明没返回值,为什么converted_model模型的输入就变了呢?
# 有没有下面这一行,似乎对结果没啥影响,都只能显示输入尺寸信息,哭了!
inference_model = shape_inference.infer_shapes(converted_model)
onnx.checker.check_model(inference_model)
dst_fullname = src_root[:-5] + "_" + str(args.size1) + "x" + str(args.size2) + ".onnx"
onnx.save(inference_model, dst_fullname)
print(dst_fullname)
else:
print("ERROR, Invalid --action")
结果显示:
边栏推荐
- Unity Animation从UAS获取动画资产到编制状态机控制简单的人物动画
- PHP二维数组保留键值去重
- Use of Alibaba Cloud SMS Service (create, test notes)
- 2-php学习笔记之控制语句,函数
- ue4学习日记3(设置操控角色,地形)
- C#程序默认以管理员权限打开
- 数组与字符串12-数组拆分
- mib browser无法接收snmp trap消息解决
- Prometheus monitors container, pod, email alerts
- Makefile.am:24: error: Libtool library used but ‘LIBTOOL‘ is undefined
猜你喜欢
随机推荐
二分查找3 - 猜数字大小
CPU上下文切换详解思维导图
学习C的第一天,尝试一下写博客
ARP协议及简单路由器原理(1)
大佬!Maya+ZBrush+Substance制作泰坦野兽全流程!
Eight, the difference between the interface of the abstract class
802.1AS的SystemIdentity理解
【3D建模制作技巧分享】ZBrush快捷键如何设置
交换机access口,hybrid口,trunk口的区别
802.1AS的BMCA(最佳主时钟选举)理解
【面筋1】一些没什么标准答案的问题
Use of Alibaba Cloud SMS Service (create, test notes)
Dynamic adjustment of web theme (2) Extraction
AQS、CAS、Synchronized小理解
嘿!selenium自动下载driver版本,得知道一下
2021-06-20
看了都收藏的3D游戏建模全流程解析,角色模型就该这么做!
classpath: comparison with classpath*
Oracle 11g silent install
使用Contab调用Shell脚本执行expdp自动备份Oracle









