示例#1
0
def build(cfg, registry, default_args=None):
    """Build a module.

    Args:
        cfg (dict, list[dict]): The config of modules, is 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.
            Defaults to None.

    Returns:
        nn.Module: A built nn module.
    """
    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)
示例#2
0
def build_sampler(cfg, default_args: Optional[Dict] = None):
    world_size = dist_info.get_world_size()
    if world_size > 1:
        rank = dist_info.get_local_rank()
        default_args.update({'world_size': world_size, 'rank': rank})
        sampler_type = getattr(cfg, 'type', None)
        if sampler_type != 'DistributedSampler':
            logger = logging.getLogger(__name__)
            logger.warning(
                f'because world_size equal {world_size},expected DistributedSampler type,but got {sampler_type}'
            )
            cfg.type = 'DistributedSampler'
    elif world_size == 1:
        sampler_type = getattr(cfg, 'type', None)
        if sampler_type == 'DistributedSampler':
            logger = logging.getLogger(__name__)
            logger.warning(
                f'because world_size equal {world_size},and the preset sampling is {sampler_type}, '
                f'here it will set sampler is None')
            return None

    SamplerAdaptor.adaptor(cfg=cfg, default_args=default_args)
    return build_from_cfg(cfg, SAMPLER, default_args)
示例#3
0
def build_dataset(dataset_cfg, default_args=None):
    return build_from_cfg(dataset_cfg, DATASETS, default_args)
示例#4
0
def build_processor(cfg, defualt_args=None):
    return build_from_cfg(cfg, PROCESSOR, defualt_args)
示例#5
0
def build_preprocessor(cfg):
    """Build preprocessor."""
    return build_from_cfg(cfg, PREPROCESSOR)
示例#6
0
def build_vocab(cfg):
    """Build vocab."""
    return build_from_cfg(cfg, VOCAB)
示例#7
0
def build_feature_reader(cfg, default_args=None):
    return build_from_cfg(cfg=cfg,
                          registry=FEATURE_READERS,
                          default_args=default_args)
示例#8
0
def build_hook(cfg, default_args: Optional[Dict] = None):
    return build_from_cfg(cfg=cfg, registry=HOOKS, default_args=default_args)
示例#9
0
def build_batch_sampler(cfg, default_args: Optional[Dict] = None):
    SamplerAdaptor.adaptor(cfg=cfg, default_args=default_args)
    return build_from_cfg(cfg, BATCH_SAMPLER, default_args)