Skip to content

Commit 0c04f52

Browse files
authored
Onnx upsample (open-mmlab#100)
* add customized Upsample which can convert to ONNX * support multiply decode head for hrnet * support size for Upsample
1 parent b8f42c7 commit 0c04f52

File tree

4 files changed

+45
-11
lines changed

4 files changed

+45
-11
lines changed

mmseg/models/backbones/hrnet.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from mmcv.runner import load_checkpoint
55
from mmcv.utils.parrots_wrapper import _BatchNorm
66

7-
from mmseg.ops import resize
7+
from mmseg.ops import Upsample, resize
88
from mmseg.utils import get_root_logger
99
from ..builder import BACKBONES
1010
from .resnet import BasicBlock, Bottleneck
@@ -141,7 +141,7 @@ def _make_fuse_layers(self):
141141
bias=False),
142142
build_norm_layer(self.norm_cfg, in_channels[i])[1],
143143
# we set align_corners=False for HRNet
144-
nn.Upsample(
144+
Upsample(
145145
scale_factor=2**(j - i),
146146
mode='bilinear',
147147
align_corners=False)))

mmseg/ops/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from .encoding import Encoding
22
from .separable_conv_module import DepthwiseSeparableConvModule
3-
from .wrappers import resize
3+
from .wrappers import Upsample, resize
44

5-
__all__ = ['resize', 'DepthwiseSeparableConvModule', 'Encoding']
5+
__all__ = ['Upsample', 'resize', 'DepthwiseSeparableConvModule', 'Encoding']

mmseg/ops/wrappers.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import warnings
22

3+
import torch
4+
import torch.nn as nn
35
import torch.nn.functional as F
46

57

@@ -11,8 +13,8 @@ def resize(input,
1113
warning=True):
1214
if warning:
1315
if size is not None and align_corners:
14-
input_h, input_w = input.shape[2:]
15-
output_h, output_w = size
16+
input_h, input_w = tuple(int(x) for x in input.shape[2:])
17+
output_h, output_w = tuple(int(x) for x in size)
1618
if output_h > input_h or output_w > output_h:
1719
if ((output_h > 1 and output_w > 1 and input_h > 1
1820
and input_w > 1) and (output_h - 1) % (input_h - 1)
@@ -22,4 +24,30 @@ def resize(input,
2224
'the output would more aligned if '
2325
f'input size {(input_h, input_w)} is `x+1` and '
2426
f'out size {(output_h, output_w)} is `nx+1`')
27+
if isinstance(size, torch.Size):
28+
size = tuple(int(x) for x in size)
2529
return F.interpolate(input, size, scale_factor, mode, align_corners)
30+
31+
32+
class Upsample(nn.Module):
33+
34+
def __init__(self,
35+
size=None,
36+
scale_factor=None,
37+
mode='nearest',
38+
align_corners=None):
39+
super(Upsample, self).__init__()
40+
self.size = size
41+
if isinstance(scale_factor, tuple):
42+
self.scale_factor = tuple(float(factor) for factor in scale_factor)
43+
else:
44+
self.scale_factor = float(scale_factor) if scale_factor else None
45+
self.mode = mode
46+
self.align_corners = align_corners
47+
48+
def forward(self, x):
49+
if not self.size:
50+
size = [int(t * self.scale_factor) for t in x.shape[-2:]]
51+
else:
52+
size = self.size
53+
return resize(x, size, None, self.mode, self.align_corners)

tools/pytorch2onnx.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import numpy as np
66
import onnxruntime as rt
77
import torch
8+
from torch import nn
89
import torch._C
910
import torch.serialization
1011
from mmcv.onnx import register_extra_symbolics
@@ -88,7 +89,10 @@ def pytorch2onnx(model,
8889
"""
8990
model.cpu().eval()
9091

91-
num_classes = model.decode_head.num_classes
92+
if isinstance(model.decode_head, nn.ModuleList):
93+
num_classes = model.decode_head[-1].num_classes
94+
else:
95+
num_classes = model.decode_head.num_classes
9296

9397
mm_inputs = _demo_mm_inputs(input_shape, num_classes)
9498

@@ -142,7 +146,7 @@ def pytorch2onnx(model,
142146

143147

144148
def parse_args():
145-
parser = argparse.ArgumentParser(description='Convert MMDet to ONNX')
149+
parser = argparse.ArgumentParser(description='Convert MMSeg to ONNX')
146150
parser.add_argument('config', help='test config file path')
147151
parser.add_argument('--checkpoint', help='checkpoint file', default=None)
148152
parser.add_argument('--show', action='store_true', help='show onnx graph')
@@ -182,11 +186,13 @@ def parse_args():
182186
# convert SyncBN to BN
183187
segmentor = _convert_batchnorm(segmentor)
184188

185-
num_classes = segmentor.decode_head.num_classes
189+
if isinstance(segmentor.decode_head, nn.ModuleList):
190+
num_classes = segmentor.decode_head[-1].num_classes
191+
else:
192+
num_classes = segmentor.decode_head.num_classes
186193

187194
if args.checkpoint:
188-
checkpoint = load_checkpoint(
189-
segmentor, args.checkpoint, map_location='cpu')
195+
load_checkpoint(segmentor, args.checkpoint, map_location='cpu')
190196

191197
# conver model to onnx file
192198
pytorch2onnx(

0 commit comments

Comments
 (0)