Skip to content

Factory

The factory provides a simple interface for creating components from encoders and backbones saved to the registry.

build_model(cfg)

Build the model components (encoder and backbone) based on the configuration.

Parameters:

Name Type Description Default
cfg

The configuration object containing model parameters. Should include: - device (str), model.dtype (torch.dtype) - encoder.name (str), encoder.kwargs (dict) - backbone.name (str), backbone.kwargs (dict)

required

Returns:

Name Type Description
Tuple (BFModule, BFModule)

The encoder and backbone modules.

Source code in bfm/model/factory.py
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
def build_model(cfg) -> Tuple[BFModule, BFModule]:
    """
    Build the model components (encoder and backbone) based on the configuration.

    Args:
        cfg: The configuration object containing model parameters.
            Should include:
                - device (str), model.dtype (torch.dtype)
                - encoder.name (str), encoder.kwargs (dict)
                - backbone.name (str), backbone.kwargs (dict)

    Returns:
        Tuple(BFModule, BFModule): The encoder and backbone modules.
    """
    encoder = encoders.resolve(cfg.encoder.name, **cfg.encoder.kwargs)
    backbone = backbones.resolve(cfg.backbone.name, **cfg.backbone.kwargs)

    encoder.to(cfg.device, cfg.model.dtype)
    backbone.to(cfg.device, cfg.model.dtype)
    return encoder, backbone