From e0066299daed4fb7503df7348c50a037d1ba9d29 Mon Sep 17 00:00:00 2001 From: Junjun2016 Date: Sun, 15 Aug 2021 23:23:29 +0800 Subject: [PATCH 1/6] add build_func for UPSAMPLE_LAYERS --- mmcv/cnn/bricks/registry.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mmcv/cnn/bricks/registry.py b/mmcv/cnn/bricks/registry.py index c29279776d..e1bb578bea 100644 --- a/mmcv/cnn/bricks/registry.py +++ b/mmcv/cnn/bricks/registry.py @@ -1,11 +1,12 @@ # Copyright (c) OpenMMLab. All rights reserved. from mmcv.utils import Registry +from .upsample import build_upsample_layer CONV_LAYERS = Registry('conv layer') NORM_LAYERS = Registry('norm layer') ACTIVATION_LAYERS = Registry('activation layer') PADDING_LAYERS = Registry('padding layer') -UPSAMPLE_LAYERS = Registry('upsample layer') +UPSAMPLE_LAYERS = Registry('upsample layer', build_func=build_upsample_layer) PLUGIN_LAYERS = Registry('plugin layer') DROPOUT_LAYERS = Registry('drop out layers') From 8bc3d6f929c7bee13ca7ab025d6004c03478f85b Mon Sep 17 00:00:00 2001 From: Junjun2016 Date: Mon, 16 Aug 2021 00:19:01 +0800 Subject: [PATCH 2/6] add build_upsample_layer_from_cfg --- mmcv/cnn/bricks/registry.py | 47 ++++++++++++++++++++++++++++++++++--- 1 file changed, 44 insertions(+), 3 deletions(-) diff --git a/mmcv/cnn/bricks/registry.py b/mmcv/cnn/bricks/registry.py index e1bb578bea..854af7341f 100644 --- a/mmcv/cnn/bricks/registry.py +++ b/mmcv/cnn/bricks/registry.py @@ -1,17 +1,58 @@ # Copyright (c) OpenMMLab. All rights reserved. +import torch.nn as nn + from mmcv.utils import Registry -from .upsample import build_upsample_layer CONV_LAYERS = Registry('conv layer') NORM_LAYERS = Registry('norm layer') ACTIVATION_LAYERS = Registry('activation layer') PADDING_LAYERS = Registry('padding layer') -UPSAMPLE_LAYERS = Registry('upsample layer', build_func=build_upsample_layer) PLUGIN_LAYERS = Registry('plugin layer') - DROPOUT_LAYERS = Registry('drop out layers') POSITIONAL_ENCODING = Registry('position encoding') ATTENTION = Registry('attention') FEEDFORWARD_NETWORK = Registry('feed-forward Network') TRANSFORMER_LAYER = Registry('transformerLayer') TRANSFORMER_LAYER_SEQUENCE = Registry('transformer-layers sequence') + + +def build_upsample_layer_from_cfg(cfg, registry, *args, **kwargs): + """Build upsample layer. + + Args: + cfg (dict): The upsample layer config, which should contain: + + - type (str): Layer type. + - scale_factor (int): Upsample ratio, which is not applicable to + deconv. + - layer args: Args needed to instantiate a upsample layer. + registry (:obj:`Registry`): A registry the module belongs to. + args (argument list): Arguments passed to the ``__init__`` + method of the corresponding conv layer. + kwargs (keyword arguments): Keyword arguments passed to the + ``__init__`` method of the corresponding conv layer. + + Returns: + nn.Module: Created upsample layer. + """ + if not isinstance(cfg, dict): + raise TypeError(f'cfg must be a dict, but got {type(cfg)}') + if 'type' not in cfg: + raise KeyError( + f'the cfg dict must contain the key "type", but got {cfg}') + cfg_ = cfg.copy() + + layer_type = cfg_.pop('type') + if layer_type not in registry: + raise KeyError(f'Unrecognized upsample type {layer_type}') + else: + upsample = registry.get(layer_type) + + if upsample is nn.Upsample: + cfg_['mode'] = layer_type + layer = upsample(*args, **kwargs, **cfg_) + return layer + + +UPSAMPLE_LAYERS = Registry( + 'upsample layer', build_func=build_upsample_layer_from_cfg) From 3a2f218c2adac909272d93059fcfcc10041372a8 Mon Sep 17 00:00:00 2001 From: Junjun2016 Date: Mon, 16 Aug 2021 12:32:30 +0800 Subject: [PATCH 3/6] implement build_upsample_layer with build_upsample_layer_from_cfg --- mmcv/cnn/bricks/upsample.py | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/mmcv/cnn/bricks/upsample.py b/mmcv/cnn/bricks/upsample.py index a1a353767d..7339b10c11 100644 --- a/mmcv/cnn/bricks/upsample.py +++ b/mmcv/cnn/bricks/upsample.py @@ -1,4 +1,6 @@ # Copyright (c) OpenMMLab. All rights reserved. +import warnings + import torch.nn as nn import torch.nn.functional as F @@ -47,6 +49,8 @@ def forward(self, x): return x +# Avoid BC-breaking of importing build_upsample_layer. +# It's backwards compatible with the old `build_upsample_layer`. def build_upsample_layer(cfg, *args, **kwargs): """Build upsample layer. @@ -65,20 +69,12 @@ def build_upsample_layer(cfg, *args, **kwargs): Returns: nn.Module: Created upsample layer. """ - if not isinstance(cfg, dict): - raise TypeError(f'cfg must be a dict, but got {type(cfg)}') - if 'type' not in cfg: - raise KeyError( - f'the cfg dict must contain the key "type", but got {cfg}') - cfg_ = cfg.copy() - - layer_type = cfg_.pop('type') - if layer_type not in UPSAMPLE_LAYERS: - raise KeyError(f'Unrecognized upsample type {layer_type}') - else: - upsample = UPSAMPLE_LAYERS.get(layer_type) - - if upsample is nn.Upsample: - cfg_['mode'] = layer_type - layer = upsample(*args, **kwargs, **cfg_) - return layer + from .registry import build_upsample_layer_from_cfg + warnings.warn( + ImportWarning( + '``build_upsample_layer(cfg, *args, **kwargs)`` will be ' + 'deprecated. Please use ' + '``UPSAMPLE_LAYERS.build(cfg, *args, **kwargs)`` instead.')) + + return build_upsample_layer_from_cfg( + cfg, *args, **kwargs, registry=UPSAMPLE_LAYERS) From 2211ec2ff3a19712ee2237e9c73064e72a78b1b4 Mon Sep 17 00:00:00 2001 From: Junjun2016 Date: Mon, 16 Aug 2021 16:23:45 +0800 Subject: [PATCH 4/6] fix build_upsample_layer_from_cfg pass arguments order --- mmcv/cnn/bricks/upsample.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/mmcv/cnn/bricks/upsample.py b/mmcv/cnn/bricks/upsample.py index 7339b10c11..411e47ab24 100644 --- a/mmcv/cnn/bricks/upsample.py +++ b/mmcv/cnn/bricks/upsample.py @@ -76,5 +76,4 @@ def build_upsample_layer(cfg, *args, **kwargs): 'deprecated. Please use ' '``UPSAMPLE_LAYERS.build(cfg, *args, **kwargs)`` instead.')) - return build_upsample_layer_from_cfg( - cfg, *args, **kwargs, registry=UPSAMPLE_LAYERS) + return build_upsample_layer_from_cfg(cfg, UPSAMPLE_LAYERS, *args, **kwargs) From 8326488af08bdd9bb026da382584539af1f3a038 Mon Sep 17 00:00:00 2001 From: Junjun2016 Date: Thu, 2 Sep 2021 22:55:20 +0800 Subject: [PATCH 5/6] refactor --- mmcv/cnn/bricks/registry.py | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/mmcv/cnn/bricks/registry.py b/mmcv/cnn/bricks/registry.py index 854af7341f..4db67d17d4 100644 --- a/mmcv/cnn/bricks/registry.py +++ b/mmcv/cnn/bricks/registry.py @@ -3,18 +3,6 @@ from mmcv.utils import Registry -CONV_LAYERS = Registry('conv layer') -NORM_LAYERS = Registry('norm layer') -ACTIVATION_LAYERS = Registry('activation layer') -PADDING_LAYERS = Registry('padding layer') -PLUGIN_LAYERS = Registry('plugin layer') -DROPOUT_LAYERS = Registry('drop out layers') -POSITIONAL_ENCODING = Registry('position encoding') -ATTENTION = Registry('attention') -FEEDFORWARD_NETWORK = Registry('feed-forward Network') -TRANSFORMER_LAYER = Registry('transformerLayer') -TRANSFORMER_LAYER_SEQUENCE = Registry('transformer-layers sequence') - def build_upsample_layer_from_cfg(cfg, registry, *args, **kwargs): """Build upsample layer. @@ -54,5 +42,16 @@ def build_upsample_layer_from_cfg(cfg, registry, *args, **kwargs): return layer +CONV_LAYERS = Registry('conv layer') +NORM_LAYERS = Registry('norm layer') +ACTIVATION_LAYERS = Registry('activation layer') +PADDING_LAYERS = Registry('padding layer') +PLUGIN_LAYERS = Registry('plugin layer') UPSAMPLE_LAYERS = Registry( 'upsample layer', build_func=build_upsample_layer_from_cfg) +DROPOUT_LAYERS = Registry('drop out layers') +POSITIONAL_ENCODING = Registry('position encoding') +ATTENTION = Registry('attention') +FEEDFORWARD_NETWORK = Registry('feed-forward Network') +TRANSFORMER_LAYER = Registry('transformerLayer') +TRANSFORMER_LAYER_SEQUENCE = Registry('transformer-layers sequence') From 4f4e31afb8196a1310a8ea3bf12cebeb56e2134c Mon Sep 17 00:00:00 2001 From: Junjun2016 Date: Thu, 2 Sep 2021 23:01:26 +0800 Subject: [PATCH 6/6] refactor --- mmcv/cnn/bricks/upsample.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mmcv/cnn/bricks/upsample.py b/mmcv/cnn/bricks/upsample.py index 411e47ab24..a5e6bc52cb 100644 --- a/mmcv/cnn/bricks/upsample.py +++ b/mmcv/cnn/bricks/upsample.py @@ -5,7 +5,7 @@ import torch.nn.functional as F from ..utils import xavier_init -from .registry import UPSAMPLE_LAYERS +from .registry import UPSAMPLE_LAYERS, build_upsample_layer_from_cfg UPSAMPLE_LAYERS.register_module('nearest', module=nn.Upsample) UPSAMPLE_LAYERS.register_module('bilinear', module=nn.Upsample) @@ -69,7 +69,7 @@ def build_upsample_layer(cfg, *args, **kwargs): Returns: nn.Module: Created upsample layer. """ - from .registry import build_upsample_layer_from_cfg + warnings.warn( ImportWarning( '``build_upsample_layer(cfg, *args, **kwargs)`` will be '