2022-09-03 09:08:45 +00:00
|
|
|
import sys
|
|
|
|
import traceback
|
|
|
|
from collections import namedtuple
|
|
|
|
import numpy as np
|
2022-09-21 01:11:53 +00:00
|
|
|
import torch
|
2022-09-03 09:08:45 +00:00
|
|
|
from PIL import Image
|
2022-09-21 01:11:53 +00:00
|
|
|
from basicsr.archs.rrdbnet_arch import RRDBNet
|
|
|
|
from realesrgan import RealESRGANer
|
|
|
|
from realesrgan.archs.srvgg_arch import SRVGGNetCompact
|
2022-09-03 09:08:45 +00:00
|
|
|
|
2022-09-04 15:54:12 +00:00
|
|
|
import modules.images
|
2022-09-08 12:19:36 +00:00
|
|
|
from modules.shared import cmd_opts, opts
|
2022-09-03 09:08:45 +00:00
|
|
|
|
|
|
|
RealesrganModelInfo = namedtuple("RealesrganModelInfo", ["name", "location", "model", "netscale"])
|
|
|
|
|
|
|
|
realesrgan_models = []
|
|
|
|
have_realesrgan = False
|
|
|
|
RealESRGANer_constructor = None
|
|
|
|
|
2022-09-04 15:54:12 +00:00
|
|
|
|
|
|
|
class UpscalerRealESRGAN(modules.images.Upscaler):
|
|
|
|
def __init__(self, upscaling, model_index):
|
|
|
|
self.upscaling = upscaling
|
|
|
|
self.model_index = model_index
|
|
|
|
self.name = realesrgan_models[model_index].name
|
|
|
|
|
|
|
|
def do_upscale(self, img):
|
|
|
|
return upscale_with_realesrgan(img, self.upscaling, self.model_index)
|
|
|
|
|
|
|
|
|
2022-09-03 09:08:45 +00:00
|
|
|
def setup_realesrgan():
|
|
|
|
global realesrgan_models
|
|
|
|
global have_realesrgan
|
|
|
|
global RealESRGANer_constructor
|
|
|
|
|
|
|
|
try:
|
|
|
|
from basicsr.archs.rrdbnet_arch import RRDBNet
|
|
|
|
from realesrgan import RealESRGANer
|
|
|
|
from realesrgan.archs.srvgg_arch import SRVGGNetCompact
|
|
|
|
|
|
|
|
realesrgan_models = [
|
2022-09-21 01:11:53 +00:00
|
|
|
RealesrganModelInfo(
|
|
|
|
name="Real-ESRGAN General x4x3",
|
|
|
|
location="https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.5.0/realesr-general-x4v3.pth",
|
|
|
|
netscale=4,
|
|
|
|
model=lambda: SRVGGNetCompact(num_in_ch=3, num_out_ch=3, num_feat=64, num_conv=32, upscale=4, act_type='prelu')
|
|
|
|
),
|
|
|
|
RealesrganModelInfo(
|
|
|
|
name="Real-ESRGAN General WDN x4x3",
|
|
|
|
location="https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.5.0/realesr-general-wdn-x4v3.pth",
|
|
|
|
netscale=4,
|
|
|
|
model=lambda: SRVGGNetCompact(num_in_ch=3, num_out_ch=3, num_feat=64, num_conv=32, upscale=4, act_type='prelu')
|
|
|
|
),
|
|
|
|
RealesrganModelInfo(
|
|
|
|
name="Real-ESRGAN AnimeVideo",
|
|
|
|
location="https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.5.0/realesr-animevideov3.pth",
|
|
|
|
netscale=4,
|
|
|
|
model=lambda: SRVGGNetCompact(num_in_ch=3, num_out_ch=3, num_feat=64, num_conv=16, upscale=4, act_type='prelu')
|
|
|
|
),
|
2022-09-03 09:08:45 +00:00
|
|
|
RealesrganModelInfo(
|
|
|
|
name="Real-ESRGAN 4x plus",
|
2022-09-21 01:11:53 +00:00
|
|
|
location="https://github.com/xinntao/Real-ESRGA N/releases/download/v0.1.0/RealESRGAN_x4plus.pth",
|
2022-09-03 09:08:45 +00:00
|
|
|
netscale=4, model=lambda: RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32, scale=4)
|
|
|
|
),
|
|
|
|
RealesrganModelInfo(
|
|
|
|
name="Real-ESRGAN 4x plus anime 6B",
|
|
|
|
location="https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.2.4/RealESRGAN_x4plus_anime_6B.pth",
|
|
|
|
netscale=4, model=lambda: RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=6, num_grow_ch=32, scale=4)
|
|
|
|
),
|
|
|
|
RealesrganModelInfo(
|
|
|
|
name="Real-ESRGAN 2x plus",
|
|
|
|
location="https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.1/RealESRGAN_x2plus.pth",
|
|
|
|
netscale=2, model=lambda: RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32, scale=2)
|
|
|
|
),
|
|
|
|
]
|
|
|
|
have_realesrgan = True
|
|
|
|
RealESRGANer_constructor = RealESRGANer
|
|
|
|
|
2022-09-04 15:54:12 +00:00
|
|
|
for i, model in enumerate(realesrgan_models):
|
|
|
|
modules.shared.sd_upscalers.append(UpscalerRealESRGAN(model.netscale, i))
|
|
|
|
|
2022-09-03 09:08:45 +00:00
|
|
|
except Exception:
|
|
|
|
print("Error importing Real-ESRGAN:", file=sys.stderr)
|
|
|
|
print(traceback.format_exc(), file=sys.stderr)
|
|
|
|
|
|
|
|
realesrgan_models = [RealesrganModelInfo('None', '', 0, None)]
|
|
|
|
have_realesrgan = False
|
|
|
|
|
|
|
|
def upscale_with_realesrgan(image, RealESRGAN_upscaling, RealESRGAN_model_index):
|
2022-09-21 01:11:53 +00:00
|
|
|
if not have_realesrgan:
|
2022-09-03 09:08:45 +00:00
|
|
|
return image
|
|
|
|
|
|
|
|
info = realesrgan_models[RealESRGAN_model_index]
|
|
|
|
|
|
|
|
model = info.model()
|
2022-09-21 01:11:53 +00:00
|
|
|
upsampler = RealESRGANer(
|
2022-09-03 09:08:45 +00:00
|
|
|
scale=info.netscale,
|
|
|
|
model_path=info.location,
|
|
|
|
model=model,
|
2022-09-08 12:19:36 +00:00
|
|
|
half=not cmd_opts.no_half,
|
2022-09-21 01:11:53 +00:00
|
|
|
tile=opts.GAN_tile,
|
|
|
|
tile_pad=opts.GAN_tile_overlap,
|
2022-09-03 09:08:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
upsampled = upsampler.enhance(np.array(image), outscale=RealESRGAN_upscaling)[0]
|
|
|
|
|
|
|
|
image = Image.fromarray(upsampled)
|
|
|
|
return image
|