File size: 2,352 Bytes
bf88397
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
"""Construct versioned models from self-contained checkpoint configuration."""

from torch import nn

from src.models.refiners import ProfileCascade, ZeroAtomNet
from src.models.unet import (
    AxialBoundaryNet,
    BoundaryNet,
    GlobalBoundaryNet,
    SpatialBoundaryNet,
)


class BoundaryEnsemble(nn.Module):
    def __init__(self, components):
        super().__init__()
        self.nets = nn.ModuleList(components)

    def forward(self, *args, **kwargs):
        raise ValueError("An ensemble must combine original-coordinate predictions")


def make_model(config):
    kwargs = {
        "boundary_keys": config["boundary_keys"],
        "base": config["base"],
        "in_channels": config.get("in_channels", 1),
    }
    architecture = config.get("architecture", "boundary_unet_v1")
    if architecture == "boundary_volume_refine_v1":
        from src.models.volume import VolumeRefiner

        return VolumeRefiner(config)
    if architecture == "boundary_profile_refine_v1":
        return ProfileCascade(config)
    if architecture == "boundary_zero_atom_v1":
        return ZeroAtomNet(config)
    if architecture == "boundary_ensemble_v1":
        components = config["components"]
        if any(
            c["config"].get("architecture") == "boundary_ensemble_v1"
            for c in components
        ):
            raise ValueError("Nested ensembles are not supported")
        return BoundaryEnsemble([make_model(c["config"]) for c in components])
    if architecture == "boundary_unet_spatial_v1":
        return SpatialBoundaryNet(
            **kwargs,
            layers=config.get("axial_layers", 2),
            lateral_layers=config.get("lateral_layers", 2),
            heads=config.get("axial_attention_heads", 6),
        )
    if architecture == "boundary_unet_v1":
        return BoundaryNet(**kwargs)
    if architecture == "boundary_unet_axial_v1":
        return AxialBoundaryNet(
            **kwargs,
            layers=config.get("axial_layers", 2),
            heads=config.get("axial_attention_heads", 6),
        )
    if architecture == "boundary_unet_global_v1":
        return GlobalBoundaryNet(
            **kwargs,
            layers=config.get("axial_layers", 0),
            heads=config.get("axial_attention_heads", 6),
        )
    raise ValueError("Unsupported checkpoint architecture")