Spaces:
Runtime error
Runtime error
File size: 1,783 Bytes
555da6f |
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
"""
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 . 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)
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 = 512
width = 512
dummy_input = Variable(torch.randn(batch_size, 3, height, width)).cuda()
# export to onnx model
torch.onnx.export(
modnet.module, dummy_input, args.output_path, export_params = True,
input_names = ['input'], output_names = ['output'],
dynamic_axes = {'input': {0:'batch_size', 2:'height', 3:'width'}, 'output': {0: 'batch_size', 2: 'height', 3: 'width'}})
|