diff --git a/mmcv/cnn/bricks/conv.py b/mmcv/cnn/bricks/conv.py index e9f4712027a..a00b0a52cee 100644 --- a/mmcv/cnn/bricks/conv.py +++ b/mmcv/cnn/bricks/conv.py @@ -1,4 +1,5 @@ # Copyright (c) OpenMMLab. All rights reserved. +import inspect from typing import Dict, Optional from mmengine.registry import MODELS @@ -35,8 +36,8 @@ def build_conv_layer(cfg: Optional[Dict], *args, **kwargs) -> nn.Module: cfg_ = cfg.copy() layer_type = cfg_.pop('type') - if isinstance(layer_type, type): - return layer_type(*args, **kwargs, **cfg_) + if inspect.isclass(layer_type): + return layer_type(*args, **kwargs, **cfg_) # type: ignore # Switch registry to the target scope. If `conv_layer` cannot be found # in the registry, fallback to search `conv_layer` in the # mmengine.MODELS. diff --git a/mmcv/cnn/bricks/padding.py b/mmcv/cnn/bricks/padding.py index a27cb688b28..3b29996b94a 100644 --- a/mmcv/cnn/bricks/padding.py +++ b/mmcv/cnn/bricks/padding.py @@ -1,4 +1,5 @@ # Copyright (c) OpenMMLab. All rights reserved. +import inspect from typing import Dict import torch.nn as nn @@ -27,7 +28,7 @@ def build_padding_layer(cfg: Dict, *args, **kwargs) -> nn.Module: cfg_ = cfg.copy() padding_type = cfg_.pop('type') - if isinstance(padding_type, type): + if inspect.isclass(padding_type): return padding_type(*args, **kwargs, **cfg_) # Switch registry to the target scope. If `padding_layer` cannot be found # in the registry, fallback to search `padding_layer` in the diff --git a/mmcv/cnn/bricks/upsample.py b/mmcv/cnn/bricks/upsample.py index 5d0c4735882..a9ed51b6b99 100644 --- a/mmcv/cnn/bricks/upsample.py +++ b/mmcv/cnn/bricks/upsample.py @@ -1,4 +1,5 @@ # Copyright (c) OpenMMLab. All rights reserved. +import inspect from typing import Dict import torch @@ -76,7 +77,7 @@ def build_upsample_layer(cfg: Dict, *args, **kwargs) -> nn.Module: layer_type = cfg_.pop('type') - if isinstance(layer_type, type): + if inspect.isclass(layer_type): upsample = layer_type # Switch registry to the target scope. If `upsample` cannot be found # in the registry, fallback to search `upsample` in the diff --git a/tests/test_cnn/test_build_layers.py b/tests/test_cnn/test_build_layers.py index 363bcfcaa96..eefdd640ca7 100644 --- a/tests/test_cnn/test_build_layers.py +++ b/tests/test_cnn/test_build_layers.py @@ -1,4 +1,5 @@ # Copyright (c) OpenMMLab. All rights reserved. +import inspect from importlib import import_module import numpy as np @@ -190,7 +191,7 @@ def test_build_activation_layer(): for module_name in ['activation', 'hsigmoid', 'hswish', 'swish']: act_module = import_module(f'mmcv.cnn.bricks.{module_name}') for key, value in act_module.__dict__.items(): - if isinstance(value, type) and issubclass(value, nn.Module): + if inspect.isclass(value) and issubclass(value, nn.Module): act_names.append(key) with pytest.raises(TypeError): @@ -235,7 +236,7 @@ def test_build_padding_layer(): for module_name in ['padding']: pad_module = import_module(f'mmcv.cnn.bricks.{module_name}') for key, value in pad_module.__dict__.items(): - if isinstance(value, type) and issubclass(value, nn.Module): + if inspect.isclass(value) and issubclass(value, nn.Module): pad_names.append(key) with pytest.raises(TypeError):