Path: blob/master/modules/esrgan_model.py
3058 views
from modules import modelloader, devices, errors1from modules.shared import opts2from modules.upscaler import Upscaler, UpscalerData3from modules.upscaler_utils import upscale_with_model456class UpscalerESRGAN(Upscaler):7def __init__(self, dirname):8self.name = "ESRGAN"9self.model_url = "https://github.com/cszn/KAIR/releases/download/v1.0/ESRGAN.pth"10self.model_name = "ESRGAN_4x"11self.scalers = []12self.user_path = dirname13super().__init__()14model_paths = self.find_models(ext_filter=[".pt", ".pth"])15scalers = []16if len(model_paths) == 0:17scaler_data = UpscalerData(self.model_name, self.model_url, self, 4)18scalers.append(scaler_data)19for file in model_paths:20if file.startswith("http"):21name = self.model_name22else:23name = modelloader.friendly_name(file)2425scaler_data = UpscalerData(name, file, self, 4)26self.scalers.append(scaler_data)2728def do_upscale(self, img, selected_model):29try:30model = self.load_model(selected_model)31except Exception:32errors.report(f"Unable to load ESRGAN model {selected_model}", exc_info=True)33return img34model.to(devices.device_esrgan)35return esrgan_upscale(model, img)3637def load_model(self, path: str):38if path.startswith("http"):39# TODO: this doesn't use `path` at all?40filename = modelloader.load_file_from_url(41url=self.model_url,42model_dir=self.model_download_path,43file_name=f"{self.model_name}.pth",44)45else:46filename = path4748return modelloader.load_spandrel_model(49filename,50device=('cpu' if devices.device_esrgan.type == 'mps' else None),51expected_architecture='ESRGAN',52)535455def esrgan_upscale(model, img):56return upscale_with_model(57model,58img,59tile_size=opts.ESRGAN_tile,60tile_overlap=opts.ESRGAN_tile_overlap,61)626364