Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] fix vsr models pytorch2onnx #1300

Merged
merged 3 commits into from
Oct 17, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 12 additions & 13 deletions tools/model_converters/pytorch2onnx.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
# Copyright (c) OpenMMLab. All rights reserved.
import argparse
import glob
import os.path as osp
import re
import warnings
from functools import reduce

import cv2
import mmcv
import numpy as np
import onnx
import onnxruntime as rt
import torch
from mmcv.onnx import register_extra_symbolics
from mmengine import Config
from mmengine.dataset import Compose
from mmengine.runner import load_checkpoint

Expand Down Expand Up @@ -63,7 +60,7 @@ def pytorch2onnx(model,
img = input['inputs'].unsqueeze(0)
data = torch.cat((img, masks), dim=1)
elif model_type == 'video_restorer':
data = input['inputs'].unsqueeze(0)
data = input['inputs'].unsqueeze(0).float()
data = data.to(device)

# pytorch has some bug in pytorch1.3, we have to fix it
Expand Down Expand Up @@ -158,6 +155,8 @@ def parse_args():
'--mask-path',
default=None,
help='path to input mask file, used in inpainting model')
parser.add_argument('--num-frames', type=int, default=None)
parser.add_argument('--sequence-length', type=int, default=None)
parser.add_argument('--device', type=int, default=0, help='CUDA device id')
parser.add_argument('--show', action='store_true', help='show onnx graph')
parser.add_argument('--output-file', type=str, default='tmp.onnx')
Expand Down Expand Up @@ -188,7 +187,7 @@ def parse_args():
else:
device = torch.device('cuda', args.device)

config = mmcv.Config.fromfile(args.config)
config = Config.fromfile(args.config)
delete_cfg(config, key='init_cfg')

# ONNX does not support spectral norm
Expand Down Expand Up @@ -217,6 +216,8 @@ def parse_args():
keys_to_remove = ['alpha', 'ori_alpha']
elif model_type == 'image_restorer':
keys_to_remove = ['gt', 'gt_path']
elif model_type == 'video_restorer':
keys_to_remove = ['gt', 'gt_path']
else:
keys_to_remove = []
for key in keys_to_remove:
Expand Down Expand Up @@ -244,17 +245,15 @@ def parse_args():
f'"GenerateSegmentIndices", but got '
f'"{test_pipeline[0]["type"]}".')
# prepare data
sequence_length = len(glob.glob(osp.join(args.img_path, '*')))
img_dir_split = re.split(r'[\\/]', args.img_path)
if img_dir_split[0] == '':
img_dir_split[0] = '/'
key = img_dir_split[-1]
lq_folder = reduce(osp.join, img_dir_split[:-1])
# sequence_length = len(glob.glob(osp.join(args.img_path, '*')))
lq_folder = osp.dirname(args.img_path)
key = osp.basename(args.img_path)
data = dict(
img_path=lq_folder,
gt_path='',
key=key,
sequence_length=sequence_length)
num_frames=args.num_frames,
sequence_length=args.sequence_length)

# build the data pipeline
test_pipeline = Compose(test_pipeline)
Expand Down