open-mmlab / mmselfsup

OpenMMLab Self-Supervised Learning Toolbox and Benchmark
https://mmselfsup.readthedocs.io/en/latest/
Apache License 2.0
3.14k stars 429 forks source link

[Bug] 'TwoNormDataPreprocessor is not in the model registry #749

Open krauwu opened 1 year ago

krauwu commented 1 year ago

Branch

1.x branch (1.x version, such as v1.0.0rc2, or dev-1.x branch)

Prerequisite

Environment

sys.platform: linux Python: 3.8.16 (default, Mar 2 2023, 03:21:46) [GCC 11.2.0] CUDA available: False numpy_random_seed: 2147483648 GCC: gcc (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0 PyTorch: 2.0.0 PyTorch compiling details: PyTorch built with:

TorchVision: 0.15.0 OpenCV: 4.7.0 MMEngine: 0.7.2 MMCV: 2.0.0 MMCV Compiler: GCC 7.3 MMCV CUDA Compiler: not available MMSelfSup: 1.0.0rc6+80bb3f4

Describe the bug

i just install successed as the .md in home page, but when run the test files, it shows KeyError: 'TwoNormDataPreprocessor is not in the model registry. but when I went to the init.py of data_preprocess, this module is right there, I don't know where is wrong

Reproduces the problem - code sample

No response

Reproduces the problem - command or script

run test_beitv1.py

Reproduces the problem - error message

FAILED [100%] tests/test_models/test_algorithms/test_beitv1.py:35 (test_beitv1) @pytest.mark.skipif(platform.system() == 'Windows', reason='Windows mem limit') def test_beitv1():

  model = BEiT(

backbone=backbone, neck=neck, head=head, target_generator=target_generator, data_preprocessor=data_preprocessor)

test_beitv1.py:38:


../../../mmselfsup/models/algorithms/base.py:58: in init super().init( ../../../../anaconda3/envs/self/lib/python3.8/site-packages/mmengine/model/base_model/base_model.py:78: in init self.data_preprocessor = MODELS.build(data_preprocessor) ../../../../anaconda3/envs/self/lib/python3.8/site-packages/mmengine/registry/registry.py:545: in build return self.build_func(cfg, *args, **kwargs, registry=self) ../../../../anaconda3/envs/self/lib/python3.8/site-packages/mmengine/registry/build_functions.py:241: in build_model_from_cfg return build_from_cfg(cfg, registry, default_args)


cfg = {'bgr_to_rgb': True, 'mean': (123.675, 116.28, 103.53), 'second_mean': (-20.4, -20.4, -20.4), 'second_std': (204.0, 204.0, 204.0), ...} registry = Registry of model
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━...orch.nn.modules.padding.ZeroPad2d'> │ └───────────────────────────────┴──────────────────────────────────────────────┘

default_args = None

def build_from_cfg(
        cfg: Union[dict, ConfigDict, Config],
        registry: Registry,
        default_args: Optional[Union[dict, ConfigDict, Config]] = None) -> Any:
    """Build a module from config dict when it is a class configuration, or
    call a function from config dict when it is a function configuration.

    If the global variable default scope (:obj:`DefaultScope`) exists,
    :meth:`build` will firstly get the responding registry and then call
    its own :meth:`build`.

    At least one of the ``cfg`` and ``default_args`` contains the key "type",
    which should be either str or class. If they all contain it, the key
    in ``cfg`` will be used because ``cfg`` has a high priority than
    ``default_args`` that means if a key exists in both of them, the value of
    the key will be ``cfg[key]``. They will be merged first and the key "type"
    will be popped up and the remaining keys will be used as initialization
    arguments.

    Examples:
        >>> from mmengine import Registry, build_from_cfg
        >>> MODELS = Registry('models')
        >>> @MODELS.register_module()
        >>> class ResNet:
        >>>     def __init__(self, depth, stages=4):
        >>>         self.depth = depth
        >>>         self.stages = stages
        >>> cfg = dict(type='ResNet', depth=50)
        >>> model = build_from_cfg(cfg, MODELS)
        >>> # Returns an instantiated object
        >>> @MODELS.register_module()
        >>> def resnet50():
        >>>     pass
        >>> resnet = build_from_cfg(dict(type='resnet50'), MODELS)
        >>> # Return a result of the calling function

    Args:
        cfg (dict or ConfigDict or Config): Config dict. It should at least
            contain the key "type".
        registry (:obj:`Registry`): The registry to search the type from.
        default_args (dict or ConfigDict or Config, optional): Default
            initialization arguments. Defaults to None.

    Returns:
        object: The constructed object.
    """
    # Avoid circular import
    from ..logging import print_log

    if not isinstance(cfg, (dict, ConfigDict, Config)):
        raise TypeError(
            f'cfg should be a dict, ConfigDict or Config, but got {type(cfg)}')

    if 'type' not in cfg:
        if default_args is None or 'type' not in default_args:
            raise KeyError(
                '`cfg` or `default_args` must contain the key "type", '
                f'but got {cfg}\n{default_args}')

    if not isinstance(registry, Registry):
        raise TypeError('registry must be a mmengine.Registry object, '
                        f'but got {type(registry)}')

    if not (isinstance(default_args,
                       (dict, ConfigDict, Config)) or default_args is None):
        raise TypeError(
            'default_args should be a dict, ConfigDict, Config or None, '
            f'but got {type(default_args)}')

    args = cfg.copy()
    if default_args is not None:
        for name, value in default_args.items():
            args.setdefault(name, value)

    # Instance should be built under target scope, if `_scope_` is defined
    # in cfg, current default scope should switch to specified scope
    # temporarily.
    scope = args.pop('_scope_', None)
    with registry.switch_scope_and_registry(scope) as registry:
        obj_type = args.pop('type')
        if isinstance(obj_type, str):
            obj_cls = registry.get(obj_type)
            if obj_cls is None:
              raise KeyError(

f'{obj_type} is not in the {registry.name} registry. ' f'Please check whether the value of {obj_type} is ' 'correct or it was registered as expected. More details ' 'can be found at ' 'https://mmengine.readthedocs.io/en/latest/advanced_tutorials/config.html#import-the-custom-module' # noqa: E501 E KeyError: 'TwoNormDataPreprocessor is not in the model registry. Please check whether the value of TwoNormDataPreprocessor is correct or it was registered as expected. More details can be found at https://mmengine.readthedocs.io/en/latest/advanced_tutorials/config.html#import-the-custom-module'

../../../../anaconda3/envs/self/lib/python3.8/site-packages/mmengine/registry/build_functions.py:100: KeyError

Additional information

No response

krauwu commented 1 year ago

solved, the test code should be type='mmselfsup.TwoNormDataPreprocessor', in line#12

fangyixiao18 commented 1 year ago

solved, the test code should be type='mmselfsup.TwoNormDataPreprocessor', in line#12

Thanks for your reporting, we will fix it