onnx模型转换opset版本和固定动态输入尺寸

背景:之前我想把onnx模型从opset12变成opset12,太慌乱就没找着,最近找到了官网上有示例的,大爱onnx官网,分享给有需求没找着的小伙伴们。

1. onnx模型转换opset版本

官网示例:

python 复制代码
import onnx
from onnx import version_converter, helper

# Preprocessing: load the model to be converted.
model_path = "path/to/the/model.onnx"
original_model = onnx.load(model_path)

print(f"The model before conversion:\n{original_model}")

# A full list of supported adapters can be found here:
# https://github.com/onnx/onnx/blob/main/onnx/version_converter.py#L21
# Apply the version conversion on the original model
converted_model = version_converter.convert_version(original_model, <int target_version>)

print(f"The model after conversion:\n{converted_model}")

其github地址如下:

onnx/docs/PythonAPIOverview.md at main · onnx/onnx (github.com)https://github.com/onnx/onnx/blob/main/docs/PythonAPIOverview.md#converting-version-of-an-onnx-model-within-default-domain-aionnx其小伙伴拉到gitee上的地址如下(以防有的小伙伴github打不开):

docs/PythonAPIOverview.md · meiqicheng/github-onnx-onnx - Gitee.comhttps://gitee.com/meiqicheng1216/onnx/blob/master/docs/PythonAPIOverview.md#converting-version-of-an-onnx-model-within-default-domain-aionnx最后附上完整代码:

python 复制代码
import onnx
from onnx import version_converter, helper

# A full list of supported adapters can be found here:
# https://github.com/onnx/onnx/blob/main/onnx/version_converter.py#L21
# Apply the version conversion on the original model

# Preprocessing: load the model to be converted.
model_path = r"./demo.onnx"
original_model = onnx.load(model_path)
print(f"The model before conversion:\n{original_model}")


converted_model = version_converter.convert_version(original_model, 11)
print(f"The model after conversion:\n{converted_model}")

save_model = model_path[:-5] + "_opset11.onnx"
onnx.save(converted_model, save_model)

2. onnx模型转固定动态输入尺寸

python 复制代码
def change_dynamic_input_shape(model_path, shape_list: list):
    """
    将动态输入的尺寸变成固定尺寸
    Args:
        model_path: onnx model path
        shape_list: [1, 3, ...]
    Returns:

    """
    import os
    import onnx
    model_path = os.path.abspath(model_path)
    output_path = model_path[:-5] + "_fixed.onnx"
    model = onnx.load(model_path)
    # print(onnx.helper.printable_graph(model.graph))
    inputs = model.graph.input  # inputs是一个列表,可以操作多输入~
    # look_input = inputs[0].type.tensor_type.shape.dim
    # print(look_input)
    # print(type(look_input))
    # inputs[0].type.tensor_type.shape.dim[0].dim_value = 1
    for idx, i_e in enumerate(shape_list):
        inputs[0].type.tensor_type.shape.dim[idx].dim_value = i_e
    # print(onnx.helper.printable_graph(model.graph))
    onnx.save(model, output_path)


if __name__ == "__main__":
    model_path = "./demo.onnx"
    shape_list = [1]
    change_dynamic_input_shape(model_path, shape_list)
相关推荐
独行soc3 分钟前
2025年渗透测试面试题总结-某腾某讯-技术安全实习生升级(题目+回答)
java·python·安全·web安全·面试·职场和发展·红蓝攻防
白80807 分钟前
python实现代码雨
开发语言·python·pygame
小周不摆烂13 分钟前
Python爬虫:开启数据抓取的奇幻之旅(二)
python
Start_Present30 分钟前
Pytorch 第十三回:神经网络编码器——自动编解码器
pytorch·python·深度学习·神经网络
互联网杂货铺30 分钟前
黑盒测试、白盒测试、集成测试和系统测试的区别与联系
自动化测试·软件测试·python·功能测试·测试工具·单元测试·集成测试
databook31 分钟前
线性模型与多分类问题:简单高效的力量
python·机器学习·scikit-learn
阿巴阿巴拉1 小时前
Scala相关知识总结3
开发语言·python
Sapphire~1 小时前
odoo-045 ModuleNotFoundError: No module named ‘_sqlite3‘
python·ubuntu·odoo
维度攻城狮5 小时前
实现在Unity3D中仿真汽车,而且还能使用ros2控制
python·unity·docker·汽车·ros2·rviz2
简简单单做算法5 小时前
基于mediapipe深度学习和限定半径最近邻分类树算法的人体摔倒检测系统python源码
人工智能·python·深度学习·算法·分类·mediapipe·限定半径最近邻分类树