fix: shape assignment for deploy_test_kneron and pytorch2onnx_kneron

This commit is contained in:
chingning.chen 2022-03-29 17:43:55 +08:00
parent 4d7356fda7
commit dcac233a60
3 changed files with 53 additions and 10 deletions

View File

@ -1,6 +1,11 @@
# Copyright (c) OpenMMLab. All rights reserved. # Copyright (c) OpenMMLab. All rights reserved.
from .base import BaseSegmentor from .base import BaseSegmentor, ONNXRuntimeSegmentorKN
from .cascade_encoder_decoder import CascadeEncoderDecoder from .cascade_encoder_decoder import CascadeEncoderDecoder
from .encoder_decoder import EncoderDecoder from .encoder_decoder import EncoderDecoder
__all__ = ['BaseSegmentor', 'EncoderDecoder', 'CascadeEncoderDecoder'] __all__ = [
'BaseSegmentor',
'ONNXRuntimeSegmentorKN',
'EncoderDecoder',
'CascadeEncoderDecoder'
]

View File

@ -66,6 +66,12 @@ def parse_args() -> argparse.Namespace:
type=float, type=float,
default=0.5, default=0.5,
help='Opacity of painted segmentation map. In (0, 1] range.') help='Opacity of painted segmentation map. In (0, 1] range.')
parser.add_argument(
'--shape',
type=int,
nargs='+',
default=None,
help='input image height and width.')
parser.add_argument('--local_rank', type=int, default=0) parser.add_argument('--local_rank', type=int, default=0)
args = parser.parse_args() args = parser.parse_args()
if 'LOCAL_RANK' not in os.environ: if 'LOCAL_RANK' not in os.environ:
@ -104,6 +110,28 @@ def main():
cfg.merge_from_dict(args.cfg_options) cfg.merge_from_dict(args.cfg_options)
cfg.model.pretrained = None cfg.model.pretrained = None
cfg.data.test.test_mode = True cfg.data.test.test_mode = True
if args.shape is not None:
if len(args.shape) == 1:
shape = (args.shape[0], args.shape[0])
elif len(args.shape) == 2:
shape = (args.shape[1], args.shape[0])
else:
raise ValueError('invalid input shape')
test_mode = cfg.model.test_cfg.mode
if test_mode == 'slide':
warnings.warn(
"We suggest you NOT assigning shape when exporting "
"slide-mode models. Assigning shape to slide-mode models "
"may result in unexpected results. To see which mode the "
"model is using, check cfg.model.test_cfg.mode, which "
"should be either 'whole' or 'slide'."
)
cfg.model.test_cfg['crop_size'] = shape
else:
cfg.test_pipeline[1]['img_scale'] = shape
cfg.data.test['pipeline'][1]['img_scale'] = shape
# init distributed env first, since logger depends on the dist info. # init distributed env first, since logger depends on the dist info.
distributed = False distributed = False

View File

@ -2,6 +2,7 @@
# Original: tools/pytorch2onnx.py, modified by Kneron # Original: tools/pytorch2onnx.py, modified by Kneron
import argparse import argparse
import warnings
import os import os
import onnx import onnx
import mmcv import mmcv
@ -293,15 +294,24 @@ if __name__ == '__main__':
else: else:
img_scale = cfg.test_pipeline[1]['img_scale'] img_scale = cfg.test_pipeline[1]['img_scale']
input_shape = (1, 3, img_scale[1], img_scale[0]) input_shape = (1, 3, img_scale[1], img_scale[0])
elif len(args.shape) == 1:
input_shape = (1, 3, args.shape[0], args.shape[0])
elif len(args.shape) == 2:
input_shape = (
1,
3,
) + tuple(args.shape)
else: else:
raise ValueError('invalid input shape') if test_mode == 'slide':
warnings.warn(
"We suggest you NOT assigning shape when exporting "
"slide-mode models. Assigning shape to slide-mode models "
"may result in unexpected results. To see which mode the "
"model is using, check cfg.model.test_cfg.mode, which "
"should be either 'whole' or 'slide'."
)
if len(args.shape) == 1:
input_shape = (1, 3, args.shape[0], args.shape[0])
elif len(args.shape) == 2:
input_shape = (
1,
3,
) + tuple(args.shape)
else:
raise ValueError('invalid input shape')
# build the model and load checkpoint # build the model and load checkpoint
cfg.model.train_cfg = None cfg.model.train_cfg = None