ldsr_model.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import os
  2. from modules.modelloader import load_file_from_url
  3. from modules.upscaler import Upscaler, UpscalerData
  4. from ldsr_model_arch import LDSR
  5. from modules import shared, script_callbacks, errors
  6. import sd_hijack_autoencoder # noqa: F401
  7. import sd_hijack_ddpm_v1 # noqa: F401
  8. class UpscalerLDSR(Upscaler):
  9. def __init__(self, user_path):
  10. self.name = "LDSR"
  11. self.user_path = user_path
  12. self.model_url = "https://heibox.uni-heidelberg.de/f/578df07c8fc04ffbadf3/?dl=1"
  13. self.yaml_url = "https://heibox.uni-heidelberg.de/f/31a76b13ea27482981b4/?dl=1"
  14. super().__init__()
  15. scaler_data = UpscalerData("LDSR", None, self)
  16. self.scalers = [scaler_data]
  17. def load_model(self, path: str):
  18. # Remove incorrect project.yaml file if too big
  19. yaml_path = os.path.join(self.model_path, "project.yaml")
  20. old_model_path = os.path.join(self.model_path, "model.pth")
  21. new_model_path = os.path.join(self.model_path, "model.ckpt")
  22. local_model_paths = self.find_models(ext_filter=[".ckpt", ".safetensors"])
  23. local_ckpt_path = next(iter([local_model for local_model in local_model_paths if local_model.endswith("model.ckpt")]), None)
  24. local_safetensors_path = next(iter([local_model for local_model in local_model_paths if local_model.endswith("model.safetensors")]), None)
  25. local_yaml_path = next(iter([local_model for local_model in local_model_paths if local_model.endswith("project.yaml")]), None)
  26. if os.path.exists(yaml_path):
  27. statinfo = os.stat(yaml_path)
  28. if statinfo.st_size >= 10485760:
  29. print("Removing invalid LDSR YAML file.")
  30. os.remove(yaml_path)
  31. if os.path.exists(old_model_path):
  32. print("Renaming model from model.pth to model.ckpt")
  33. os.rename(old_model_path, new_model_path)
  34. if local_safetensors_path is not None and os.path.exists(local_safetensors_path):
  35. model = local_safetensors_path
  36. else:
  37. model = local_ckpt_path or load_file_from_url(self.model_url, model_dir=self.model_download_path, file_name="model.ckpt")
  38. yaml = local_yaml_path or load_file_from_url(self.yaml_url, model_dir=self.model_download_path, file_name="project.yaml")
  39. return LDSR(model, yaml)
  40. def do_upscale(self, img, path):
  41. try:
  42. ldsr = self.load_model(path)
  43. except Exception:
  44. errors.report(f"Failed loading LDSR model {path}", exc_info=True)
  45. return img
  46. ddim_steps = shared.opts.ldsr_steps
  47. return ldsr.super_resolution(img, ddim_steps, self.scale)
  48. def on_ui_settings():
  49. import gradio as gr
  50. shared.opts.add_option("ldsr_steps", shared.OptionInfo(100, "LDSR processing steps. Lower = faster", gr.Slider, {"minimum": 1, "maximum": 200, "step": 1}, section=('upscaling', "Upscaling")))
  51. shared.opts.add_option("ldsr_cached", shared.OptionInfo(False, "Cache LDSR model in memory", gr.Checkbox, {"interactive": True}, section=('upscaling', "Upscaling")))
  52. script_callbacks.on_ui_settings(on_ui_settings)