Path: blob/master/modules/realesrgan_model.py
3575 views
import os12from modules import modelloader, errors3from modules.shared import cmd_opts, opts4from modules.upscaler import Upscaler, UpscalerData5from modules.upscaler_utils import upscale_with_model678class UpscalerRealESRGAN(Upscaler):9def __init__(self, path):10self.name = "RealESRGAN"11self.user_path = path12super().__init__()13self.enable = True14self.scalers = []15scalers = get_realesrgan_models(self)1617local_model_paths = self.find_models(ext_filter=[".pth"])18for scaler in scalers:19if scaler.local_data_path.startswith("http"):20filename = modelloader.friendly_name(scaler.local_data_path)21local_model_candidates = [local_model for local_model in local_model_paths if local_model.endswith(f"{filename}.pth")]22if local_model_candidates:23scaler.local_data_path = local_model_candidates[0]2425if scaler.name in opts.realesrgan_enabled_models:26self.scalers.append(scaler)2728def do_upscale(self, img, path):29if not self.enable:30return img3132try:33info = self.load_model(path)34except Exception:35errors.report(f"Unable to load RealESRGAN model {path}", exc_info=True)36return img3738model_descriptor = modelloader.load_spandrel_model(39info.local_data_path,40device=self.device,41prefer_half=(not cmd_opts.no_half and not cmd_opts.upcast_sampling),42expected_architecture="ESRGAN", # "RealESRGAN" isn't a specific thing for Spandrel43)44return upscale_with_model(45model_descriptor,46img,47tile_size=opts.ESRGAN_tile,48tile_overlap=opts.ESRGAN_tile_overlap,49# TODO: `outscale`?50)5152def load_model(self, path):53for scaler in self.scalers:54if scaler.data_path == path:55if scaler.local_data_path.startswith("http"):56scaler.local_data_path = modelloader.load_file_from_url(57scaler.data_path,58model_dir=self.model_download_path,59)60if not os.path.exists(scaler.local_data_path):61raise FileNotFoundError(f"RealESRGAN data missing: {scaler.local_data_path}")62return scaler63raise ValueError(f"Unable to find model info: {path}")646566def get_realesrgan_models(scaler: UpscalerRealESRGAN):67return [68UpscalerData(69name="R-ESRGAN General 4xV3",70path="https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.5.0/realesr-general-x4v3.pth",71scale=4,72upscaler=scaler,73),74UpscalerData(75name="R-ESRGAN General WDN 4xV3",76path="https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.5.0/realesr-general-wdn-x4v3.pth",77scale=4,78upscaler=scaler,79),80UpscalerData(81name="R-ESRGAN AnimeVideo",82path="https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.5.0/realesr-animevideov3.pth",83scale=4,84upscaler=scaler,85),86UpscalerData(87name="R-ESRGAN 4x+",88path="https://github.com/xinntao/Real-ESRGAN/releases/download/v0.1.0/RealESRGAN_x4plus.pth",89scale=4,90upscaler=scaler,91),92UpscalerData(93name="R-ESRGAN 4x+ Anime6B",94path="https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.2.4/RealESRGAN_x4plus_anime_6B.pth",95scale=4,96upscaler=scaler,97),98UpscalerData(99name="R-ESRGAN 2x+",100path="https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.1/RealESRGAN_x2plus.pth",101scale=2,102upscaler=scaler,103),104]105106107