-
Notifications
You must be signed in to change notification settings - Fork 643
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Enhancement]: Support fcn_unet deployment with dynamic shape (#251)
* support mmseg fcn+unet dynamic shape * add test * fix ci * fix units * resolve comments
- Loading branch information
1 parent
d9eeaba
commit ed2ec9d
Showing
7 changed files
with
131 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
# Copyright (c) OpenMMLab. All rights reserved. | ||
from .decode_heads import * # noqa: F401,F403 | ||
from .segmentors import * # noqa: F401,F403 | ||
from .utils import * # noqa: F401,F403 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Copyright (c) OpenMMLab. All rights reserved. | ||
from .up_conv_block import up_conv_block__forward | ||
|
||
__all__ = ['up_conv_block__forward'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# Copyright (c) OpenMMLab. All rights reserved. | ||
|
||
import torch | ||
|
||
from mmdeploy.core import FUNCTION_REWRITER | ||
from mmdeploy.utils import is_dynamic_shape | ||
|
||
|
||
@FUNCTION_REWRITER.register_rewriter( | ||
func_name='mmseg.models.utils.UpConvBlock.forward') | ||
def up_conv_block__forward(ctx, self, skip, x): | ||
"""Rewrite `forward` for default backend. | ||
To support dynamic shape for UNet backbone, | ||
upsample feature maps with `size` instead of `scale_factor` | ||
Args: | ||
ctx (ContextCaller): The context with additional information. | ||
self: The instance of the original class. | ||
skip (Tensor): Skip branch feature. | ||
x (Tensor): Input feature to be upsampled. | ||
Returns: | ||
Tensor: Upsampled output feature map. | ||
""" | ||
from mmcv.cnn import ConvModule | ||
|
||
# only valid when self.upsample is from build_upsample_layer | ||
if is_dynamic_shape(ctx.cfg) and not isinstance(self.upsample, ConvModule): | ||
# upsample with `size` instead of `scale_factor` | ||
from mmseg.ops import Upsample | ||
for c in self.upsample.interp_upsample: | ||
if isinstance(c, Upsample): | ||
c.size = skip.shape[-2:] | ||
c.scale_factor = None | ||
|
||
x = self.upsample(x) | ||
out = torch.cat([skip, x], dim=1) | ||
out = self.conv_block(out) | ||
return out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters