cymic/Waifu_Diffusion_Webui
1
1import os2import sys3import traceback4 5from basicsr.utils.download_util import load_file_from_url6 7from modules.upscaler import Upscaler, UpscalerData8from modules.ldsr_model_arch import LDSR9from modules import shared10from modules.paths import models_path11 12 13class UpscalerLDSR(Upscaler):14 def __init__(self, user_path):15 self.name = "LDSR"16 self.model_path = os.path.join(models_path, self.name)17 self.user_path = user_path18 self.model_url = "https://heibox.uni-heidelberg.de/f/578df07c8fc04ffbadf3/?dl=1"19 self.yaml_url = "https://heibox.uni-heidelberg.de/f/31a76b13ea27482981b4/?dl=1"20 super().__init__()21 scaler_data = UpscalerData("LDSR", None, self)22 self.scalers = [scaler_data]23 24 def load_model(self, path: str):25 # Remove incorrect project.yaml file if too big26 yaml_path = os.path.join(self.model_path, "project.yaml")27 old_model_path = os.path.join(self.model_path, "model.pth")28 new_model_path = os.path.join(self.model_path, "model.ckpt")29 if os.path.exists(yaml_path):30 statinfo = os.stat(yaml_path)31 if statinfo.st_size >= 10485760:32 print("Removing invalid LDSR YAML file.")33 os.remove(yaml_path)34 if os.path.exists(old_model_path):35 print("Renaming model from model.pth to model.ckpt")36 os.rename(old_model_path, new_model_path)37 model = load_file_from_url(url=self.model_url, model_dir=self.model_path,38 file_name="model.ckpt", progress=True)39 yaml = load_file_from_url(url=self.yaml_url, model_dir=self.model_path,40 file_name="project.yaml", progress=True)41 42 try:43 return LDSR(model, yaml)44 45 except Exception:46 print("Error importing LDSR:", file=sys.stderr)47 print(traceback.format_exc(), file=sys.stderr)48 return None49 50 def do_upscale(self, img, path):51 ldsr = self.load_model(path)52 if ldsr is None:53 print("NO LDSR!")54 return img55 ddim_steps = shared.opts.ldsr_steps56 return ldsr.super_resolution(img, ddim_steps, self.scale)57 