MCPcopy Create free account
hub / github.com/OpenDriveLab/ReSim / new_module

Function new_module

SwissArmyTransformer/sat/tokenization/cogview/vqvae/api.py:17–44  ·  view source on GitHub ↗

in config: "target": module type, vqvae_zc.Decoder/vqvae_diffusion.Decoder/vqvae_diffusion.Decoder2 "ckpt": path of checkpoint "ckpt_prefix": prefix to remove in ckpt state dict "device": device "params": dict of params

(config)

Source from the content-addressed store, hash-verified

15from .vqvae_zc import VQVAE
16
17def new_module(config):
18 '''
19 in config:
20 "target": module type, vqvae_zc.Decoder/vqvae_diffusion.Decoder/vqvae_diffusion.Decoder2
21 "ckpt": path of checkpoint
22 "ckpt_prefix": prefix to remove in ckpt state dict
23 "device": device
24 "params": dict of params
25 '''
26 if not "target" in config:
27 raise KeyError("Expected key `target` to instantiate.")
28 module, cls = config.get("target").rsplit(".", 1)
29 model = getattr(importlib.import_module(module, package=__package__), cls)(**config.get("params", dict()))
30
31 device = config.get("device", "cpu")
32 model = model.to(device)
33 model.eval()
34
35 if "ckpt" in config:
36 ckpt = torch.load(config.get("ckpt"), map_location='cpu')
37 prefix = config.get("ckpt_prefix", None)
38 if "state_dict" in ckpt:
39 ckpt = ckpt["state_dict"]
40 if prefix is not None:
41 ckpt = {k[len(prefix) + 1:]: v for k, v in ckpt.items() if k.startswith(prefix)}
42 model.load_state_dict(ckpt, strict=False)
43 del ckpt
44 return model
45
46def load_decoder_default(device=0, path="pretrained/vqvae/l1+ms-ssim+revd_percep.pt"):
47 # exp: load currently best decoder

Callers 3

load_decoder_defaultFunction · 0.85
load_model_defaultFunction · 0.85
test_decodeFunction · 0.85

Calls 4

getMethod · 0.80
toMethod · 0.80
loadMethod · 0.45
load_state_dictMethod · 0.45

Tested by 1

test_decodeFunction · 0.68