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)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构