Path: blob/master/extensions-builtin/LDSR/scripts/ldsr_model.py
2448 views
import os12from modules.modelloader import load_file_from_url3from modules.upscaler import Upscaler, UpscalerData4from ldsr_model_arch import LDSR5from modules import shared, script_callbacks, errors6import sd_hijack_autoencoder # noqa: F4017import sd_hijack_ddpm_v1 # noqa: F4018910class UpscalerLDSR(Upscaler):11def __init__(self, user_path):12self.name = "LDSR"13self.user_path = user_path14self.model_url = "https://heibox.uni-heidelberg.de/f/578df07c8fc04ffbadf3/?dl=1"15self.yaml_url = "https://heibox.uni-heidelberg.de/f/31a76b13ea27482981b4/?dl=1"16super().__init__()17scaler_data = UpscalerData("LDSR", None, self)18self.scalers = [scaler_data]1920def load_model(self, path: str):21# Remove incorrect project.yaml file if too big22yaml_path = os.path.join(self.model_path, "project.yaml")23old_model_path = os.path.join(self.model_path, "model.pth")24new_model_path = os.path.join(self.model_path, "model.ckpt")2526local_model_paths = self.find_models(ext_filter=[".ckpt", ".safetensors"])27local_ckpt_path = next(iter([local_model for local_model in local_model_paths if local_model.endswith("model.ckpt")]), None)28local_safetensors_path = next(iter([local_model for local_model in local_model_paths if local_model.endswith("model.safetensors")]), None)29local_yaml_path = next(iter([local_model for local_model in local_model_paths if local_model.endswith("project.yaml")]), None)3031if os.path.exists(yaml_path):32statinfo = os.stat(yaml_path)33if statinfo.st_size >= 10485760:34print("Removing invalid LDSR YAML file.")35os.remove(yaml_path)3637if os.path.exists(old_model_path):38print("Renaming model from model.pth to model.ckpt")39os.rename(old_model_path, new_model_path)4041if local_safetensors_path is not None and os.path.exists(local_safetensors_path):42model = local_safetensors_path43else:44model = local_ckpt_path or load_file_from_url(self.model_url, model_dir=self.model_download_path, file_name="model.ckpt")4546yaml = local_yaml_path or load_file_from_url(self.yaml_url, model_dir=self.model_download_path, file_name="project.yaml")4748return LDSR(model, yaml)4950def do_upscale(self, img, path):51try:52ldsr = self.load_model(path)53except Exception:54errors.report(f"Failed loading LDSR model {path}", exc_info=True)55return img56ddim_steps = shared.opts.ldsr_steps57return ldsr.super_resolution(img, ddim_steps, self.scale)585960def on_ui_settings():61import gradio as gr6263shared.opts.add_option("ldsr_steps", shared.OptionInfo(100, "LDSR processing steps. Lower = faster", gr.Slider, {"minimum": 1, "maximum": 200, "step": 1}, section=('upscaling', "Upscaling")))64shared.opts.add_option("ldsr_cached", shared.OptionInfo(False, "Cache LDSR model in memory", gr.Checkbox, {"interactive": True}, section=('upscaling', "Upscaling")))656667script_callbacks.on_ui_settings(on_ui_settings)686970