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)
相关推荐
通信.萌新32 分钟前
OpenCV边沿检测(Python版)
人工智能·python·opencv
Bran_Liu37 分钟前
【LeetCode 刷题】字符串-字符串匹配(KMP)
python·算法·leetcode
weixin_3077791340 分钟前
分析一个深度学习项目并设计算法和用PyTorch实现的方法和步骤
人工智能·pytorch·python
Channing Lewis1 小时前
flask实现重启后需要重新输入用户名而避免浏览器使用之前已经记录的用户名
后端·python·flask
Channing Lewis2 小时前
如何在 Flask 中实现用户认证?
后端·python·flask
水银嘻嘻2 小时前
【Mac】Python相关知识经验
开发语言·python·macos
汤姆和佩琦2 小时前
2025-1-20-sklearn学习(42) 使用scikit-learn计算 钿车罗帕,相逢处,自有暗尘随马。
人工智能·python·学习·机器学习·scikit-learn·sklearn
我的运维人生2 小时前
Java并发编程深度解析:从理论到实践
java·开发语言·python·运维开发·技术共享
lljss20203 小时前
python创建一个httpServer网页上传文件到httpServer
开发语言·python
Makesths3 小时前
【python基础】用Python写一个2048小游戏
python