mmselfsup/openselfsup/models/builder.py

57 lines
1.2 KiB
Python
Raw Normal View History

2020-06-16 00:05:18 +08:00
from torch import nn
from openselfsup.utils import build_from_cfg
from .registry import (BACKBONES, MODELS, NECKS, HEADS, MEMORIES, LOSSES)
def build(cfg, registry, default_args=None):
2020-09-02 18:49:39 +08:00
"""Build a module.
Args:
cfg (dict, list[dict]): The config of modules, it is either a dict
or a list of configs.
registry (:obj:`Registry`): A registry the module belongs to.
default_args (dict, optional): Default arguments to build the module.
Default: None.
Returns:
nn.Module: A built nn module.
"""
2020-06-16 00:05:18 +08:00
if isinstance(cfg, list):
modules = [
build_from_cfg(cfg_, registry, default_args) for cfg_ in cfg
]
return nn.Sequential(*modules)
else:
return build_from_cfg(cfg, registry, default_args)
def build_backbone(cfg):
2020-09-02 18:49:39 +08:00
"""Build backbone."""
2020-06-16 00:05:18 +08:00
return build(cfg, BACKBONES)
def build_neck(cfg):
2020-09-02 18:49:39 +08:00
"""Build neck."""
2020-06-16 00:05:18 +08:00
return build(cfg, NECKS)
def build_memory(cfg):
2020-09-02 18:49:39 +08:00
"""Build memory."""
2020-06-16 00:05:18 +08:00
return build(cfg, MEMORIES)
def build_head(cfg):
2020-09-02 18:49:39 +08:00
"""Build head."""
2020-06-16 00:05:18 +08:00
return build(cfg, HEADS)
def build_loss(cfg):
2020-09-02 18:49:39 +08:00
"""Build loss."""
2020-06-16 00:05:18 +08:00
return build(cfg, LOSSES)
def build_model(cfg):
2020-09-02 18:49:39 +08:00
"""Build model."""
2020-06-16 00:05:18 +08:00
return build(cfg, MODELS)