Created
January 24, 2023 23:34
-
-
Save dmarx/ef2bfc1fab8665d19ed17b4e2f4aa4e6 to your computer and use it in GitHub Desktop.
instantiate_from_config implementation used by CompVis
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# via https://github.com/Stability-AI/stablediffusion/blob/main/ldm/util.py | |
def instantiate_from_config(config): | |
if not "target" in config: | |
if config == '__is_first_stage__': | |
return None | |
elif config == "__is_unconditional__": | |
return None | |
raise KeyError("Expected key `target` to instantiate.") | |
return get_obj_from_str(config["target"])(**config.get("params", dict())) | |
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment