2022-09-26 14:29:50 +00:00
|
|
|
import os
|
2022-09-21 12:37:54 +00:00
|
|
|
|
2023-05-31 16:56:37 +00:00
|
|
|
from modules import modelloader, errors
|
2023-12-30 14:37:03 +00:00
|
|
|
from modules.shared import cmd_opts, opts
|
|
|
|
from modules.upscaler import Upscaler, UpscalerData
|
|
|
|
from modules.upscaler_utils import upscale_with_model
|
2023-05-31 16:56:37 +00:00
|
|
|
|
2022-09-22 04:59:27 +00:00
|
|
|
|
2022-09-29 22:46:23 +00:00
|
|
|
class UpscalerRealESRGAN(Upscaler):
|
|
|
|
def __init__(self, path):
|
|
|
|
self.name = "RealESRGAN"
|
|
|
|
self.user_path = path
|
|
|
|
super().__init__()
|
2023-12-25 12:43:51 +00:00
|
|
|
self.enable = True
|
|
|
|
self.scalers = []
|
|
|
|
scalers = get_realesrgan_models(self)
|
2023-03-25 20:58:53 +00:00
|
|
|
|
2023-12-25 12:43:51 +00:00
|
|
|
local_model_paths = self.find_models(ext_filter=[".pth"])
|
|
|
|
for scaler in scalers:
|
|
|
|
if scaler.local_data_path.startswith("http"):
|
|
|
|
filename = modelloader.friendly_name(scaler.local_data_path)
|
|
|
|
local_model_candidates = [local_model for local_model in local_model_paths if local_model.endswith(f"{filename}.pth")]
|
|
|
|
if local_model_candidates:
|
|
|
|
scaler.local_data_path = local_model_candidates[0]
|
2023-03-25 20:58:53 +00:00
|
|
|
|
2023-12-25 12:43:51 +00:00
|
|
|
if scaler.name in opts.realesrgan_enabled_models:
|
|
|
|
self.scalers.append(scaler)
|
2022-09-29 22:46:23 +00:00
|
|
|
|
|
|
|
def do_upscale(self, img, path):
|
|
|
|
if not self.enable:
|
|
|
|
return img
|
|
|
|
|
2023-05-29 07:38:51 +00:00
|
|
|
try:
|
|
|
|
info = self.load_model(path)
|
|
|
|
except Exception:
|
|
|
|
errors.report(f"Unable to load RealESRGAN model {path}", exc_info=True)
|
2022-09-29 22:46:23 +00:00
|
|
|
return img
|
|
|
|
|
2023-12-30 22:09:51 +00:00
|
|
|
model_descriptor = modelloader.load_spandrel_model(
|
2023-12-25 12:43:51 +00:00
|
|
|
info.local_data_path,
|
2023-08-23 06:31:38 +00:00
|
|
|
device=self.device,
|
2023-12-25 12:43:51 +00:00
|
|
|
half=(not cmd_opts.no_half and not cmd_opts.upcast_sampling),
|
2023-12-30 19:12:32 +00:00
|
|
|
expected_architecture="ESRGAN", # "RealESRGAN" isn't a specific thing for Spandrel
|
2023-12-25 12:43:51 +00:00
|
|
|
)
|
|
|
|
return upscale_with_model(
|
2023-12-30 22:09:51 +00:00
|
|
|
model_descriptor,
|
2023-12-25 12:43:51 +00:00
|
|
|
img,
|
|
|
|
tile_size=opts.ESRGAN_tile,
|
|
|
|
tile_overlap=opts.ESRGAN_tile_overlap,
|
|
|
|
# TODO: `outscale`?
|
2022-09-29 22:46:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
def load_model(self, path):
|
2023-05-29 07:38:51 +00:00
|
|
|
for scaler in self.scalers:
|
|
|
|
if scaler.data_path == path:
|
|
|
|
if scaler.local_data_path.startswith("http"):
|
|
|
|
scaler.local_data_path = modelloader.load_file_from_url(
|
|
|
|
scaler.data_path,
|
|
|
|
model_dir=self.model_download_path,
|
|
|
|
)
|
|
|
|
if not os.path.exists(scaler.local_data_path):
|
|
|
|
raise FileNotFoundError(f"RealESRGAN data missing: {scaler.local_data_path}")
|
|
|
|
return scaler
|
|
|
|
raise ValueError(f"Unable to find model info: {path}")
|
2022-09-22 04:59:27 +00:00
|
|
|
|
2022-09-29 22:46:23 +00:00
|
|
|
|
2023-12-25 12:43:51 +00:00
|
|
|
def get_realesrgan_models(scaler: UpscalerRealESRGAN):
|
|
|
|
return [
|
|
|
|
UpscalerData(
|
|
|
|
name="R-ESRGAN General 4xV3",
|
|
|
|
path="https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.5.0/realesr-general-x4v3.pth",
|
|
|
|
scale=4,
|
|
|
|
upscaler=scaler,
|
|
|
|
),
|
|
|
|
UpscalerData(
|
|
|
|
name="R-ESRGAN General WDN 4xV3",
|
|
|
|
path="https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.5.0/realesr-general-wdn-x4v3.pth",
|
|
|
|
scale=4,
|
|
|
|
upscaler=scaler,
|
|
|
|
),
|
|
|
|
UpscalerData(
|
|
|
|
name="R-ESRGAN AnimeVideo",
|
|
|
|
path="https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.5.0/realesr-animevideov3.pth",
|
|
|
|
scale=4,
|
|
|
|
upscaler=scaler,
|
|
|
|
),
|
|
|
|
UpscalerData(
|
|
|
|
name="R-ESRGAN 4x+",
|
|
|
|
path="https://github.com/xinntao/Real-ESRGAN/releases/download/v0.1.0/RealESRGAN_x4plus.pth",
|
|
|
|
scale=4,
|
|
|
|
upscaler=scaler,
|
|
|
|
),
|
|
|
|
UpscalerData(
|
|
|
|
name="R-ESRGAN 4x+ Anime6B",
|
|
|
|
path="https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.2.4/RealESRGAN_x4plus_anime_6B.pth",
|
|
|
|
scale=4,
|
|
|
|
upscaler=scaler,
|
|
|
|
),
|
|
|
|
UpscalerData(
|
|
|
|
name="R-ESRGAN 2x+",
|
|
|
|
path="https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.1/RealESRGAN_x2plus.pth",
|
|
|
|
scale=2,
|
|
|
|
upscaler=scaler,
|
|
|
|
),
|
|
|
|
]
|