Skip to content

Instantly share code, notes, and snippets.

@dmarx
Created January 24, 2023 23:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmarx/ef2bfc1fab8665d19ed17b4e2f4aa4e6 to your computer and use it in GitHub Desktop.
Save dmarx/ef2bfc1fab8665d19ed17b4e2f4aa4e6 to your computer and use it in GitHub Desktop.
instantiate_from_config implementation used by CompVis
# 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