-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathload_utils.py
executable file
·31 lines (23 loc) · 954 Bytes
/
load_utils.py
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
import importlib
import torch
from omegaconf import OmegaConf
def get_obj_from_str(string, reload=False):
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):
if not "target" in config:
raise KeyError("Expected key `target` to instantiate.")
return get_obj_from_str(config["target"])(**config.get("params", dict()))
def load_model(name, ckpt):
config = OmegaConf.load(f'taming-transformers/configs/pr/{name}.yaml')
model = instantiate_from_config(config.model)
model.init_from_ckpt(ckpt) # load_state_dict(mc['state_dict'])
model.eval()
return model
def load_data(name):
config = OmegaConf.load(f'taming-transformers/configs/pr/{name}.yaml')
data = instantiate_from_config(config.data)
return data