随笔 - 93  文章 - 0  评论 - 3  阅读 - 46388

export_onnx 单个 多个 输入,包含动态输入

复制代码
"""
Export ONNX model of MODNet with:
    input shape: (batch_size, 3, height, width)
    output shape: (batch_size, 1, height, width)  

Arguments:
    --ckpt-path: path of the checkpoint that will be converted
    --output-path: path for saving the ONNX model

Example:
    python export_onnx.py \
        --ckpt-path=modnet_photographic_portrait_matting.ckpt \
        --output-path=modnet_photographic_portrait_matting.onnx
"""

import os
import argparse

import torch
import torch.nn as nn
from torch.autograd import Variable

from onnx import modnet_onnx


if __name__ == '__main__':
    # define cmd arguments
    parser = argparse.ArgumentParser()
    parser.add_argument('--ckpt-path', type=str, required=True, help='path of the checkpoint that will be converted')
    parser.add_argument('--output-path', type=str, required=True, help='path for saving the ONNX model')
    args = parser.parse_args()

    # check input arguments
    if not os.path.exists(args.ckpt_path):
        print('Cannot find checkpoint path: {0}'.format(args.ckpt_path))
        exit()

    # define model & load checkpoint
    modnet = modnet_onnx.MODNet(backbone_pretrained=False,hr_channels=16)
    modnet = nn.DataParallel(modnet).cuda()
    state_dict = torch.load(args.ckpt_path)
    modnet.load_state_dict(state_dict)
    modnet.eval()

    # prepare dummy_input
    batch_size = 1
    height = 256
    width = 256
    
    ##############单个输入######################
    # dummy_inputImg = Variable(torch.randn(batch_size, 3, height, width)).cuda()
    # 
    # # export to onnx model
    # torch.onnx.export(
    #     modnet.module, dummy_inputImg, args.output_path, export_params=True,
    #     input_names=['inputImg'], output_names=['output'],
    #     # dynamic_axes = {'input': {0:'batch_size', 2:'height', 3:'width'}, 'output': {0: 'batch_size', 2: 'height', 3: 'width'}},opset_version=11)
    #     dynamic_axes={'inputImg': {0: 'batch_size', 2: 'height', 3: 'width'}, 'output': {0: 'batch_size', 2: 'height', 3: 'width'}}, opset_version=11)

    ##############多个输入######################
    dummy_inputImg = Variable(torch.randn(batch_size, 3, height, width)).cuda()
    dummy_inputMatte = Variable(torch.randn(batch_size, 1, height, width)).cuda() 

    # export to onnx model
    torch.onnx.export(
        modnet.module, (dummy_inputImg,dummy_inputMatte),args.output_path, export_params = True,
        input_names = ['inputImg','inputMatte'], output_names = ['output'],
        # dynamic_axes = {'input': {0:'batch_size', 2:'height', 3:'width'}, 'output': {0: 'batch_size', 2: 'height', 3: 'width'}},opset_version=11)
        dynamic_axes = {'inputImg': {0: 'batch_size',2: 'height', 3: 'width'},'inputMatte': {0: 'batch_size',2: 'height', 3: 'width'},'output': {0: 'batch_size', 2: 'height', 3: 'width'}}, opset_version = 11)
复制代码

 

posted on   WenJXUST  阅读(1986)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示