2022-09-26 14:29:50 +00:00
|
|
|
import os
|
2022-09-21 12:37:54 +00:00
|
|
|
|
2022-09-03 09:08:45 +00:00
|
|
|
import numpy as np
|
|
|
|
from PIL import Image
|
2022-09-26 14:29:50 +00:00
|
|
|
from basicsr.utils.download_util import load_file_from_url
|
2022-09-21 01:11:53 +00:00
|
|
|
from realesrgan import RealESRGANer
|
2022-09-03 09:08:45 +00:00
|
|
|
|
2022-09-29 22:46:23 +00:00
|
|
|
from modules.upscaler import Upscaler, UpscalerData
|
2022-09-08 12:19:36 +00:00
|
|
|
from modules.shared import cmd_opts, opts
|
2023-05-31 16:56:37 +00:00
|
|
|
from modules import modelloader, errors
|
|
|
|
|
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__()
|
|
|
|
try:
|
2023-05-10 06:02:23 +00:00
|
|
|
from basicsr.archs.rrdbnet_arch import RRDBNet # noqa: F401
|
|
|
|
from realesrgan import RealESRGANer # noqa: F401
|
|
|
|
from realesrgan.archs.srvgg_arch import SRVGGNetCompact # noqa: F401
|
2022-09-29 22:46:23 +00:00
|
|
|
self.enable = True
|
|
|
|
self.scalers = []
|
|
|
|
scalers = self.load_models(path)
|
2023-03-25 20:58:53 +00:00
|
|
|
|
|
|
|
local_model_paths = self.find_models(ext_filter=[".pth"])
|
2022-09-29 22:46:23 +00:00
|
|
|
for scaler in scalers:
|
2023-03-25 20:58:53 +00:00
|
|
|
if scaler.local_data_path.startswith("http"):
|
|
|
|
filename = modelloader.friendly_name(scaler.local_data_path)
|
2023-05-09 19:17:58 +00:00
|
|
|
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
|
|
|
|
2022-09-29 22:46:23 +00:00
|
|
|
if scaler.name in opts.realesrgan_enabled_models:
|
|
|
|
self.scalers.append(scaler)
|
|
|
|
|
|
|
|
except Exception:
|
2023-05-31 16:56:37 +00:00
|
|
|
errors.report("Error importing Real-ESRGAN", exc_info=True)
|
2022-09-29 22:46:23 +00:00
|
|
|
self.enable = False
|
|
|
|
self.scalers = []
|
|
|
|
|
|
|
|
def do_upscale(self, img, path):
|
|
|
|
if not self.enable:
|
|
|
|
return img
|
|
|
|
|
|
|
|
info = self.load_model(path)
|
2023-01-17 10:57:55 +00:00
|
|
|
if not os.path.exists(info.local_data_path):
|
2023-05-09 19:17:58 +00:00
|
|
|
print(f"Unable to load RealESRGAN model: {info.name}")
|
2022-09-29 22:46:23 +00:00
|
|
|
return img
|
|
|
|
|
|
|
|
upsampler = RealESRGANer(
|
|
|
|
scale=info.scale,
|
2023-01-17 10:57:55 +00:00
|
|
|
model_path=info.local_data_path,
|
2022-09-29 22:46:23 +00:00
|
|
|
model=info.model(),
|
2023-01-27 15:19:43 +00:00
|
|
|
half=not cmd_opts.no_half and not cmd_opts.upcast_sampling,
|
2022-09-29 22:46:23 +00:00
|
|
|
tile=opts.ESRGAN_tile,
|
|
|
|
tile_pad=opts.ESRGAN_tile_overlap,
|
|
|
|
)
|
|
|
|
|
|
|
|
upsampled = upsampler.enhance(np.array(img), outscale=info.scale)[0]
|
|
|
|
|
|
|
|
image = Image.fromarray(upsampled)
|
|
|
|
return image
|
|
|
|
|
|
|
|
def load_model(self, path):
|
|
|
|
try:
|
2023-01-17 10:57:55 +00:00
|
|
|
info = next(iter([scaler for scaler in self.scalers if scaler.data_path == path]), None)
|
2022-09-29 22:46:23 +00:00
|
|
|
|
|
|
|
if info is None:
|
|
|
|
print(f"Unable to find model info: {path}")
|
|
|
|
return None
|
|
|
|
|
2023-03-25 20:58:53 +00:00
|
|
|
if info.local_data_path.startswith("http"):
|
2023-05-19 06:09:00 +00:00
|
|
|
info.local_data_path = load_file_from_url(url=info.data_path, model_dir=self.model_download_path, progress=True)
|
2023-03-25 20:58:53 +00:00
|
|
|
|
2022-09-29 22:46:23 +00:00
|
|
|
return info
|
2023-05-29 05:54:13 +00:00
|
|
|
except Exception:
|
2023-05-31 16:56:37 +00:00
|
|
|
errors.report("Error making Real-ESRGAN models list", exc_info=True)
|
2022-09-29 22:46:23 +00:00
|
|
|
return None
|
2022-09-22 04:59:27 +00:00
|
|
|
|
2022-09-29 22:46:23 +00:00
|
|
|
def load_models(self, _):
|
|
|
|
return get_realesrgan_models(self)
|
|
|
|
|
|
|
|
|
|
|
|
def get_realesrgan_models(scaler):
|
2022-09-22 04:59:27 +00:00
|
|
|
try:
|
|
|
|
from basicsr.archs.rrdbnet_arch import RRDBNet
|
|
|
|
from realesrgan.archs.srvgg_arch import SRVGGNetCompact
|
|
|
|
models = [
|
2022-09-29 22:46:23 +00:00
|
|
|
UpscalerData(
|
|
|
|
name="R-ESRGAN General 4xV3",
|
2022-09-30 08:42:40 +00:00
|
|
|
path="https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.5.0/realesr-general-x4v3.pth",
|
2022-09-29 22:46:23 +00:00
|
|
|
scale=4,
|
|
|
|
upscaler=scaler,
|
2022-09-30 08:42:40 +00:00
|
|
|
model=lambda: SRVGGNetCompact(num_in_ch=3, num_out_ch=3, num_feat=64, num_conv=32, upscale=4, act_type='prelu')
|
2022-09-22 04:59:27 +00:00
|
|
|
),
|
2022-09-29 22:46:23 +00:00
|
|
|
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,
|
2022-09-30 08:42:40 +00:00
|
|
|
model=lambda: SRVGGNetCompact(num_in_ch=3, num_out_ch=3, num_feat=64, num_conv=32, upscale=4, act_type='prelu')
|
2022-09-22 04:59:27 +00:00
|
|
|
),
|
2022-09-29 22:46:23 +00:00
|
|
|
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,
|
2022-09-30 08:42:40 +00:00
|
|
|
model=lambda: SRVGGNetCompact(num_in_ch=3, num_out_ch=3, num_feat=64, num_conv=16, upscale=4, act_type='prelu')
|
2022-09-22 04:59:27 +00:00
|
|
|
),
|
2022-09-29 22:46:23 +00:00
|
|
|
UpscalerData(
|
|
|
|
name="R-ESRGAN 4x+",
|
|
|
|
path="https://github.com/xinntao/Real-ESRGAN/releases/download/v0.1.0/RealESRGAN_x4plus.pth",
|
|
|
|
scale=4,
|
|
|
|
upscaler=scaler,
|
2022-09-22 04:59:27 +00:00
|
|
|
model=lambda: RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32, scale=4)
|
|
|
|
),
|
2022-09-29 22:46:23 +00:00
|
|
|
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,
|
2022-09-22 04:59:27 +00:00
|
|
|
model=lambda: RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=6, num_grow_ch=32, scale=4)
|
|
|
|
),
|
2022-09-29 22:46:23 +00:00
|
|
|
UpscalerData(
|
|
|
|
name="R-ESRGAN 2x+",
|
|
|
|
path="https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.1/RealESRGAN_x2plus.pth",
|
|
|
|
scale=2,
|
|
|
|
upscaler=scaler,
|
2022-09-22 04:59:27 +00:00
|
|
|
model=lambda: RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32, scale=2)
|
|
|
|
),
|
|
|
|
]
|
|
|
|
return models
|
2023-05-10 04:52:45 +00:00
|
|
|
except Exception:
|
2023-05-31 16:56:37 +00:00
|
|
|
errors.report("Error making Real-ESRGAN models list", exc_info=True)
|