-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathconvert2onnx.py
658 lines (562 loc) · 22.6 KB
/
convert2onnx.py
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
# Copyright (c) OpenMMLab. All rights reserved.
import argparse
import numpy as np
import torch
import torch.nn.functional as F
from torch import nn
import mmcv
from mmcv.cnn import build_norm_layer, ConvModule
from mmcv.runner import load_checkpoint, BaseModule
from mmseg.ops import resize
from mmseg.apis.inference import LoadImage
from mmseg.datasets.pipelines import Compose
torch.manual_seed(3)
def _convert_batchnorm(module):
module_output = module
if isinstance(module, torch.nn.SyncBatchNorm):
module_output = torch.nn.BatchNorm2d(module.num_features, module.eps,
module.momentum, module.affine,
module.track_running_stats)
if module.affine:
module_output.weight.data = module.weight.data.clone().detach()
module_output.bias.data = module.bias.data.clone().detach()
# keep requires_grad unchanged
module_output.weight.requires_grad = module.weight.requires_grad
module_output.bias.requires_grad = module.bias.requires_grad
module_output.running_mean = module.running_mean
module_output.running_var = module.running_var
module_output.num_batches_tracked = module.num_batches_tracked
for name, child in module.named_children():
module_output.add_module(name, _convert_batchnorm(child))
del module
return module_output
def _demo_mm_inputs(input_shape, num_classes):
"""Create a superset of inputs needed to run test or train batches.
Args:
input_shape (tuple):
input batch dimensions
num_classes (int):
number of semantic classes
"""
(N, C, H, W) = input_shape
rng = np.random.RandomState(0)
imgs = rng.rand(*input_shape)
segs = rng.randint(
low=0, high=num_classes - 1, size=(N, 1, H, W)).astype(np.uint8)
img_metas = [{
'img_shape': (H, W, C),
'ori_shape': (H, W, C),
'pad_shape': (H, W, C),
'filename': '<demo>.png',
'scale_factor': 1.0,
'flip': False,
} for _ in range(N)]
mm_inputs = {
'imgs': torch.FloatTensor(imgs).requires_grad_(True),
'img_metas': img_metas,
'gt_semantic_seg': torch.LongTensor(segs)
}
return mm_inputs
def _make_divisible(v, divisor, min_value=None):
"""
This function is taken from the original tf repo.
It ensures that all layers have a channel number that is divisible by 8
It can be seen here:
https://github.com/tensorflow/models/blob/master/research/slim/nets/mobilenet/mobilenet.py
:param v:
:param divisor:
:param min_value:
:return:
"""
if min_value is None:
min_value = divisor
new_v = max(min_value, int(v + divisor / 2) // divisor * divisor)
# Make sure that round down does not go down by more than 10%.
if new_v < 0.9 * v:
new_v += divisor
return new_v
def get_shape(tensor):
shape = tensor.shape
if torch.onnx.is_in_onnx_export():
shape = [i.cpu().numpy() for i in shape]
return shape
class Conv2d_BN(nn.Sequential):
def __init__(self, a, b, ks=1, stride=1, pad=0, dilation=1,
groups=1, bn_weight_init=1,
norm_cfg=dict(type='BN', requires_grad=True)):
super().__init__()
self.inp_channel = a
self.out_channel = b
self.ks = ks
self.pad = pad
self.stride = stride
self.dilation = dilation
self.groups = groups
self.add_module('c', nn.Conv2d(
a, b, ks, stride, pad, dilation, groups, bias=False))
bn = build_norm_layer(norm_cfg, b)[1]
nn.init.constant_(bn.weight, bn_weight_init)
nn.init.constant_(bn.bias, 0)
self.add_module('bn', bn)
class Mlp(nn.Module):
def __init__(self, in_features, hidden_features=None, out_features=None, act_layer=nn.ReLU, drop=0.,
norm_cfg=dict(type='BN', requires_grad=True)):
super().__init__()
out_features = out_features or in_features
hidden_features = hidden_features or in_features
self.fc1 = Conv2d_BN(in_features, hidden_features, norm_cfg=norm_cfg)
self.dwconv = nn.Conv2d(hidden_features, hidden_features, 3, 1, 1, bias=True, groups=hidden_features)
self.act = act_layer()
self.fc2 = Conv2d_BN(hidden_features, out_features, norm_cfg=norm_cfg)
self.drop = nn.Dropout(drop)
def forward(self, x):
x = self.fc1(x)
x = self.dwconv(x)
x = self.act(x)
x = self.drop(x)
x = self.fc2(x)
x = self.drop(x)
return x
class InvertedResidual(nn.Module):
def __init__(
self,
inp: int,
oup: int,
ks: int,
stride: int,
expand_ratio: int,
activations = None,
norm_cfg=dict(type='BN', requires_grad=True)
) -> None:
super(InvertedResidual, self).__init__()
self.stride = stride
self.expand_ratio = expand_ratio
assert stride in [1, 2]
if activations is None:
activations = nn.ReLU
hidden_dim = int(round(inp * expand_ratio))
self.use_res_connect = self.stride == 1 and inp == oup
layers = []
if expand_ratio != 1:
# pw
layers.append(Conv2d_BN(inp, hidden_dim, ks=1, norm_cfg=norm_cfg))
layers.append(activations())
layers.extend([
# dw
Conv2d_BN(hidden_dim, hidden_dim, ks=ks, stride=stride, pad=ks//2, groups=hidden_dim, norm_cfg=norm_cfg),
activations(),
# pw-linear
Conv2d_BN(hidden_dim, oup, ks=1, norm_cfg=norm_cfg)
])
self.conv = nn.Sequential(*layers)
self.out_channels = oup
self._is_cn = stride > 1
def forward(self, x):
if self.use_res_connect:
return x + self.conv(x)
else:
return self.conv(x)
class StackedMV2Block(nn.Module):
def __init__(
self,
cfgs,
stem,
inp_channel=16,
activation=nn.ReLU,
norm_cfg=dict(type='BN', requires_grad=True),
width_mult=1.):
super().__init__()
self.stem = stem
if stem:
self.stem_block = nn.Sequential(
Conv2d_BN(3, inp_channel, 3, 2, 1, norm_cfg=norm_cfg),
activation()
)
self.cfgs = cfgs
self.layers = []
for i, (k, t, c, s) in enumerate(cfgs):
output_channel = _make_divisible(c * width_mult, 8)
exp_size = t * inp_channel
exp_size = _make_divisible(exp_size * width_mult, 8)
layer_name = 'layer{}'.format(i + 1)
layer = InvertedResidual(inp_channel, output_channel, ks=k, stride=s, expand_ratio=t, norm_cfg=norm_cfg,
activations=activation)
self.add_module(layer_name, layer)
inp_channel = output_channel
self.layers.append(layer_name)
def forward(self, x):
if self.stem:
x = self.stem_block(x)
for i, layer_name in enumerate(self.layers):
layer = getattr(self, layer_name)
x = layer(x)
return x
class SqueezeAxialPositionalEmbedding(nn.Module):
def __init__(self, dim, shape):
super().__init__()
self.pos_embed = nn.Parameter(torch.randn([1, dim, shape]), requires_grad=True)
def forward(self, x):
B, C, N = x.shape
x = x + F.interpolate(self.pos_embed, size=(N), mode='linear', align_corners=False)
return x
class Sea_Attention(torch.nn.Module):
def __init__(self, dim, key_dim, num_heads,
attn_ratio=4,
activation=None,
norm_cfg=dict(type='BN', requires_grad=True), ):
super().__init__()
self.num_heads = num_heads
self.scale = key_dim ** -0.5
self.key_dim = key_dim
self.nh_kd = nh_kd = key_dim * num_heads # num_head key_dim
self.d = int(attn_ratio * key_dim)
self.dh = int(attn_ratio * key_dim) * num_heads
self.attn_ratio = attn_ratio
self.to_q = Conv2d_BN(dim, nh_kd, 1, norm_cfg=norm_cfg)
self.to_k = Conv2d_BN(dim, nh_kd, 1, norm_cfg=norm_cfg)
self.to_v = Conv2d_BN(dim, self.dh, 1, norm_cfg=norm_cfg)
self.proj = torch.nn.Sequential(activation(), Conv2d_BN(
self.dh, dim, bn_weight_init=0, norm_cfg=norm_cfg))
self.proj_encode_row = torch.nn.Sequential(activation(), Conv2d_BN(
self.dh, self.dh, bn_weight_init=0, norm_cfg=norm_cfg))
self.pos_emb_rowq = SqueezeAxialPositionalEmbedding(nh_kd, 16)
self.pos_emb_rowk = SqueezeAxialPositionalEmbedding(nh_kd, 16)
self.proj_encode_column = torch.nn.Sequential(activation(), Conv2d_BN(
self.dh, self.dh, bn_weight_init=0, norm_cfg=norm_cfg))
self.pos_emb_columnq = SqueezeAxialPositionalEmbedding(nh_kd, 16)
self.pos_emb_columnk = SqueezeAxialPositionalEmbedding(nh_kd, 16)
self.dwconv = Conv2d_BN(2 * self.dh, 2 * self.dh, ks=3, stride=1, pad=1, dilation=1,
groups=2 * self.dh, norm_cfg=norm_cfg)
self.act = activation()
self.pwconv = Conv2d_BN(2 * self.dh, dim, ks=1, norm_cfg=norm_cfg)
self.sigmoid = h_sigmoid()
def forward(self, x): # x (B,N,C)
B, C, H, W = x.shape
q = self.to_q(x)
k = self.to_k(x)
v = self.to_v(x)
# detail enhance
qkv = torch.cat([q, k, v], dim=1)
qkv = self.act(self.dwconv(qkv))
qkv = self.pwconv(qkv)
# squeeze axial attention
## squeeze row
qrow = self.pos_emb_rowq(q.mean(-1)).reshape(B, self.num_heads, -1, H).permute(0, 1, 3, 2)
krow = self.pos_emb_rowk(k.mean(-1)).reshape(B, self.num_heads, -1, H)
vrow = v.mean(-1).reshape(B, self.num_heads, -1, H).permute(0, 1, 3, 2)
attn_row = torch.matmul(qrow, krow) * self.scale
attn_row = attn_row.softmax(dim=-1)
xx_row = torch.matmul(attn_row, vrow) # B nH H C
xx_row = self.proj_encode_row(xx_row.permute(0, 1, 3, 2).reshape(B, self.dh, H, 1))
## squeeze column
qcolumn = self.pos_emb_columnq(q.mean(-2)).reshape(B, self.num_heads, -1, W).permute(0, 1, 3, 2)
kcolumn = self.pos_emb_columnk(k.mean(-2)).reshape(B, self.num_heads, -1, W)
vcolumn = v.mean(-2).reshape(B, self.num_heads, -1, W).permute(0, 1, 3, 2)
attn_column = torch.matmul(qcolumn, kcolumn) * self.scale
attn_column = attn_column.softmax(dim=-1)
xx_column = torch.matmul(attn_column, vcolumn) # B nH W C
xx_column = self.proj_encode_column(xx_column.permute(0, 1, 3, 2).reshape(B, self.dh, 1, W))
xx = xx_row.add(xx_column)
xx = v.add(xx)
xx = self.proj(xx)
xx = self.sigmoid(xx) * qkv
return xx
class Block(nn.Module):
def __init__(self, dim, key_dim, num_heads, mlp_ratio=4., attn_ratio=2., drop=0.,
drop_path=0., act_layer=nn.ReLU, norm_cfg=dict(type='BN2d', requires_grad=True)):
super().__init__()
self.dim = dim
self.num_heads = num_heads
self.mlp_ratio = mlp_ratio
self.attn = Sea_Attention(dim, key_dim=key_dim, num_heads=num_heads, attn_ratio=attn_ratio,
activation=act_layer, norm_cfg=norm_cfg)
mlp_hidden_dim = int(dim * mlp_ratio)
self.mlp = Mlp(in_features=dim, hidden_features=mlp_hidden_dim, act_layer=act_layer, drop=drop,
norm_cfg=norm_cfg)
def forward(self, x1):
x1 = x1 + self.attn(x1)
x1 = x1 + self.mlp(x1)
return x1
class BasicLayer(nn.Module):
def __init__(self, block_num, embedding_dim, key_dim, num_heads,
mlp_ratio=4., attn_ratio=2., drop=0., attn_drop=0., drop_path=0.,
norm_cfg=dict(type='BN2d', requires_grad=True),
act_layer=None):
super().__init__()
self.block_num = block_num
self.transformer_blocks = nn.ModuleList()
for i in range(self.block_num):
self.transformer_blocks.append(Block(
embedding_dim, key_dim=key_dim, num_heads=num_heads,
mlp_ratio=mlp_ratio, attn_ratio=attn_ratio,
drop=drop, drop_path=drop_path[i] if isinstance(drop_path, list) else drop_path,
norm_cfg=norm_cfg,
act_layer=act_layer))
def forward(self, x):
# token * N
for i in range(self.block_num):
x = self.transformer_blocks[i](x)
return x
class h_sigmoid(nn.Module):
def __init__(self, inplace=True):
super(h_sigmoid, self).__init__()
self.relu = nn.ReLU6(inplace=inplace)
def forward(self, x):
return self.relu(x + 3) / 6
class Fusion_block(nn.Module):
def __init__(
self,
inp: int,
oup: int,
embed_dim: int,
norm_cfg=dict(type='BN', requires_grad=True),
activations=None,
) -> None:
super(Fusion_block, self).__init__()
self.norm_cfg = norm_cfg
self.local_embedding = ConvModule(inp, embed_dim, kernel_size=1, norm_cfg=self.norm_cfg, act_cfg=None)
self.global_act = ConvModule(oup, embed_dim, kernel_size=1, norm_cfg=self.norm_cfg, act_cfg=None)
self.act = h_sigmoid()
def forward(self, x_l, x_g):
'''
x_g: global features
x_l: local features
'''
B, C, H, W = x_l.shape
B, C_c, H_c, W_c = x_g.shape
local_feat = self.local_embedding(x_l)
global_act = self.global_act(x_g)
sig_act = F.interpolate(self.act(global_act), size=(H, W), mode='bilinear', align_corners=False)
out = local_feat * sig_act
return out
class SeaFormer(nn.Module):
def __init__(self,
cfgs,
channels,
emb_dims,
key_dims,
depths=[2, 2],
num_heads=8,
attn_ratios=2,
mlp_ratios=[2, 4],
drop_path_rate=0.,
norm_cfg=dict(type='BN', requires_grad=True),
act_layer=nn.ReLU6,
num_classes=1000,
init_cfg=None,
**args):
super().__init__()
self.num_classes = num_classes
self.channels = channels
self.depths = depths
self.cfgs = cfgs
self.norm_cfg = norm_cfg
self.init_cfg = init_cfg
if self.init_cfg is not None:
self.pretrained = self.init_cfg['checkpoint']
for i in range(len(cfgs)):
smb = StackedMV2Block(cfgs=cfgs[i], stem=True if i == 0 else False, inp_channel=channels[i],
norm_cfg=norm_cfg)
setattr(self, f"smb{i + 1}", smb)
for i in range(len(depths)):
dpr = [x.item() for x in torch.linspace(0, drop_path_rate, depths[i])] # stochastic depth decay rule
trans = BasicLayer(
block_num=depths[i],
embedding_dim=emb_dims[i],
key_dim=key_dims[i],
num_heads=num_heads,
mlp_ratio=mlp_ratios[i],
attn_ratio=attn_ratios,
drop=0, attn_drop=0,
drop_path=dpr,
norm_cfg=norm_cfg,
act_layer=act_layer)
setattr(self, f"trans{i + 1}", trans)
def init_weights(self):
for m in self.modules():
if isinstance(m, nn.Conv2d):
n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
n //= m.groups
m.weight.data.normal_(0, math.sqrt(2. / n))
if m.bias is not None:
m.bias.data.zero_()
elif isinstance(m, nn.BatchNorm2d):
m.weight.data.fill_(1)
m.bias.data.zero_()
elif isinstance(m, nn.Linear):
m.weight.data.normal_(0, 0.01)
if m.bias is not None:
m.bias.data.zero_()
if isinstance(self.pretrained, str):
logger = get_root_logger()
checkpoint = _load_checkpoint(self.pretrained, logger=logger, map_location='cpu')
if 'state_dict_ema' in checkpoint:
state_dict = checkpoint['state_dict_ema']
elif 'state_dict' in checkpoint:
state_dict = checkpoint['state_dict']
elif 'model' in checkpoint:
state_dict = checkpoint['model']
else:
state_dict = checkpoint
self.load_state_dict(state_dict, False)
def forward(self, x):
outputs = []
num_smb_stage = len(self.cfgs)
num_trans_stage = len(self.depths)
for i in range(num_smb_stage):
smb = getattr(self, f"smb{i + 1}")
x = smb(x)
# 1/8 shared feat
if i == 1:
outputs.append(x)
if num_trans_stage + i >= num_smb_stage:
trans = getattr(self, f"trans{i + num_trans_stage - num_smb_stage + 1}")
x = trans(x)
outputs.append(x)
return outputs
class LightHead(BaseModule):
"""
SEA-Former: Squeeze-enhanced Axial Transformer for Mobile Semantic Segmentation
"""
def __init__(self, embed_dims, channels, in_index, in_channels, num_classes, norm_cfg=None, act_cfg=dict(type='ReLU'), is_dw=False, **kwargs):
super(LightHead, self).__init__()
head_channels = channels
in_channels = in_channels
self.in_index = in_index
self.linear_fuse = ConvModule(
in_channels=head_channels,
out_channels=head_channels,
kernel_size=1,
stride=1,
groups=head_channels if is_dw else 1,
norm_cfg=norm_cfg,
act_cfg=act_cfg
)
for i in range(len(embed_dims)):
fuse = Fusion_block(in_channels[0] if i == 0 else embed_dims[i - 1], in_channels[i + 1],
embed_dim=embed_dims[i], norm_cfg=norm_cfg)
setattr(self, f"fuse{i + 1}", fuse)
self.embed_dims = embed_dims
self.conv_seg = nn.Conv2d(head_channels, num_classes, kernel_size=1)
def forward(self, inputs):
xx = [inputs[i] for i in self.in_index]
x_detail = xx[0]
for i in range(len(self.embed_dims)):
fuse = getattr(self, f"fuse{i + 1}")
x_detail = fuse(x_detail, xx[i + 1])
_c = self.linear_fuse(x_detail)
x = self.conv_seg(_c)
return x
class Segmentor(nn.Module):
def __init__(self,
backbone,
decode_head):
super(Segmentor, self).__init__()
self.backbone = backbone
self.decode_head = decode_head
def forward(self, img):
x = self.backbone(img)
out = self.decode_head(x)
return out
def _prepare_input_img(img_path,
test_pipeline,
shape=None,
rescale_shape=None):
# build the data pipeline
if shape is not None:
test_pipeline[1]['img_scale'] = (shape[1], shape[0])
test_pipeline[1]['transforms'][0]['keep_ratio'] = False
test_pipeline = [LoadImage()] + test_pipeline[1:]
test_pipeline = Compose(test_pipeline)
# prepare data
data = dict(img=img_path)
data = test_pipeline(data)
imgs = data['img']
img_metas = [i.data for i in data['img_metas']]
if rescale_shape is not None:
for img_meta in img_metas:
img_meta['ori_shape'] = tuple(rescale_shape) + (3,)
mm_inputs = {'imgs': imgs, 'img_metas': img_metas}
return mm_inputs
def pytorch2onnx(model,
mm_inputs,
show=False,
output_file='tmp.onnx',
num_classes=150):
model.cpu().eval()
imgs = mm_inputs.pop('imgs')
img_list = [img[None, :] for img in imgs]
with torch.no_grad():
torch.onnx.export(
model, (img_list[0],),
output_file,
input_names=['input'],
output_names=['output'],
export_params=True,
opset_version=11,
do_constant_folding=True,
keep_initializers_as_inputs=False,
verbose=show)
print(f'Successfully exported ONNX model: {output_file}')
def parse_args():
parser = argparse.ArgumentParser(description='Convert MMSeg to ONNX')
parser.add_argument('config', help='test config file path')
parser.add_argument('--checkpoint', help='checkpoint file', default=None)
parser.add_argument(
'--input-img', type=str, help='Images for input', default=None)
parser.add_argument(
'--show',
action='store_true',
help='show onnx graph and segmentation results')
parser.add_argument('--output-file', type=str, default='tmp.onnx')
parser.add_argument('--opset-version', type=int, default=11)
parser.add_argument(
'--shape',
type=int,
nargs='+',
default=None,
help='input image height and width.')
args = parser.parse_args()
return args
if __name__ == '__main__':
args = parse_args()
cfg = mmcv.Config.fromfile(args.config)
cfg.model.pretrained = None
if args.shape is None:
img_scale = cfg.test_pipeline[1]['img_scale']
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:
raise ValueError('invalid input shape')
# build the model and load checkpoint
cfg.model.train_cfg = None
backbone = SeaFormer(**cfg.model.backbone)
head = LightHead(**cfg.model.decode_head)
segmentor = Segmentor(backbone, head)
# convert SyncBN to BN
segmentor = _convert_batchnorm(segmentor)
if args.checkpoint:
checkpoint = load_checkpoint(
segmentor, args.checkpoint, map_location='cpu')
# read input or create dummpy input
if args.input_img is not None:
preprocess_shape = (input_shape[2], input_shape[3])
rescale_shape = None
mm_inputs = _prepare_input_img(
args.input_img,
cfg.data.test.pipeline,
shape=preprocess_shape,
rescale_shape=rescale_shape)
else:
mm_inputs = _demo_mm_inputs(input_shape, num_classes=150)
input = torch.rand((1, 3, 512, 512))
print(segmentor)
from fvcore.nn import FlopCountAnalysis, flop_count_table
segmentor.eval()
flops = FlopCountAnalysis(segmentor, input)
print(flop_count_table(flops))
# convert model to onnx file
pytorch2onnx(segmentor, mm_inputs, show=args.show, output_file=args.output_file)