Skip to content

Allow config items to be preconstructed instances in build_from_cfg. #915

Open
@Erotemic

Description

@Erotemic

I have a case where I'm using mmdet to build a custom detection network architecture based on HRNet. In this case, I need to build the backbone of the network before I have information about what the neck configuration should be. Specifically, my variant of HRNet will compute what the number of channels will be in each pyramid layer, but I don't know that before I build it. Thus I can't fill in the appropriate parameters for "neck.in_channels".

What I would like to be able to do is preconstruct the backbone and pass backbone.out_channels to the "neck" configuration before I build it. Then when I build the final detector, because I already have the backbone built I want to pass the preconstructed object instead of the config. Similar to the following:

        backbone_cfg = {
            'channels': channels,
            'input_stats': input_stats,
            'fuse_method': fuse_method,
            'type': CustomHRNetBackbone
        }
        backbone = build_backbone(backbone_cfg)

        mm_cfg = mmcv.Config({
            'model': {
                'backbone': backbone,
                'neck': {
                    'in_channels': backbone.out_channels, 
                    'out_channels': 256,
                    'type': HRFPN_V2,
                    'norm_cfg': {'type': 'GN', 'num_groups': 32},
                },
        ...

        from mmdet.models import build_detector
        detector = build_detector(
            mm_cfg['model'], train_cfg=default_args['train_cfg'],
            test_cfg=default_args['test_cfg'])

Currently this won't work because build_from_cfg has the line:

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

What I would like to propose is for build_from_cfg to allow cfg to be a preconstructed object or perhaps a special dictionary where there is only one key {"instance": obj} and obj is the instance of that class? If this idea sounds reasonable I can make the PR.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions