ldsr_model.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import os
  2. from basicsr.utils.download_util 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 if local_ckpt_path is not None else load_file_from_url(url=self.model_url, model_dir=self.model_download_path, file_name="model.ckpt", progress=True)
  38. yaml = local_yaml_path if local_yaml_path is not None else load_file_from_url(url=self.yaml_url, model_dir=self.model_download_path, file_name="project.yaml", progress=True)
  39. try:
  40. return LDSR(model, yaml)
  41. except Exception:
  42. errors.report("Error importing LDSR", exc_info=True)
  43. return None
  44. def do_upscale(self, img, path):
  45. ldsr = self.load_model(path)
  46. if ldsr is None:
  47. print("NO LDSR!")
  48. return img
  49. ddim_steps = shared.opts.ldsr_steps
  50. return ldsr.super_resolution(img, ddim_steps, self.scale)
  51. def on_ui_settings():
  52. import gradio as gr
  53. 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")))
  54. shared.opts.add_option("ldsr_cached", shared.OptionInfo(False, "Cache LDSR model in memory", gr.Checkbox, {"interactive": True}, section=('upscaling', "Upscaling")))
  55. script_callbacks.on_ui_settings(on_ui_settings)