File size: 1,318 Bytes
32da3e8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Model instantiation utilities."""

import importlib

import torch

from configs import ModelConfig


def get_obj_from_str(string: str, reload: bool = False):
    """Import and return a class/function from a dotted string path."""
    module, cls = string.rsplit(".", 1)
    if reload:
        module_imp = importlib.import_module(module)
        importlib.reload(module_imp)
    return getattr(importlib.import_module(module, package=None), cls)


def instantiate_from_config(config: ModelConfig) -> object:
    """Instantiate a model from ModelConfig.

    Args:
        config: ModelConfig with target, params, and optional ckpt path

    Returns:
        Instantiated model, optionally with loaded checkpoint
    """
    if not config.target:
        raise KeyError("Expected 'target' to instantiate.")

    model = get_obj_from_str(config.target)(**config.params)

    if getattr(config, "ckpt", None) is not None:
        state_dict = torch.load(config.ckpt, map_location="cpu")
        if "ema" in state_dict:
            state_dict = state_dict["ema"]
        elif "model" in state_dict:
            raise NotImplementedError("Loading from 'model' key not implemented yet.")
        model.load_state_dict(state_dict, strict=True)
        print(f"Loaded {config.target} from {config.ckpt}")

    return model