2023-01-28 23:09:19 +00:00
|
|
|
from logging import getLogger
|
2023-01-28 05:28:14 +00:00
|
|
|
from os import path
|
2023-02-05 13:53:26 +00:00
|
|
|
|
|
|
|
import numpy as np
|
2023-01-28 05:28:14 +00:00
|
|
|
from PIL import Image
|
|
|
|
|
2023-02-05 13:53:26 +00:00
|
|
|
from ..onnx import OnnxNet
|
|
|
|
from ..params import DeviceParams, ImageParams, StageParams, UpscaleParams
|
2023-02-26 05:49:39 +00:00
|
|
|
from ..server import ServerContext
|
2023-02-19 02:28:21 +00:00
|
|
|
from ..utils import run_gc
|
2023-02-26 20:15:30 +00:00
|
|
|
from ..worker import WorkerContext
|
2023-01-28 05:28:14 +00:00
|
|
|
|
2023-01-28 23:09:19 +00:00
|
|
|
logger = getLogger(__name__)
|
2023-01-28 05:28:14 +00:00
|
|
|
|
|
|
|
last_pipeline_instance = None
|
|
|
|
last_pipeline_params = (None, None)
|
|
|
|
|
2023-02-18 14:41:15 +00:00
|
|
|
TAG_X4_V3 = "real-esrgan-x4-v3"
|
2023-02-07 03:36:20 +00:00
|
|
|
|
2023-01-28 05:28:14 +00:00
|
|
|
|
2023-02-05 13:53:26 +00:00
|
|
|
def load_resrgan(
|
2023-02-14 00:10:11 +00:00
|
|
|
server: ServerContext, params: UpscaleParams, device: DeviceParams, tile=0
|
2023-02-05 13:53:26 +00:00
|
|
|
):
|
2023-02-14 04:23:17 +00:00
|
|
|
# must be within load function for patches to take effect
|
|
|
|
from basicsr.archs.rrdbnet_arch import RRDBNet
|
|
|
|
from realesrgan import RealESRGANer
|
|
|
|
from realesrgan.archs.srvgg_arch import SRVGGNetCompact
|
|
|
|
|
2023-02-05 13:53:26 +00:00
|
|
|
model_file = "%s.%s" % (params.upscale_model, params.format)
|
2023-02-14 00:10:11 +00:00
|
|
|
model_path = path.join(server.model_path, model_file)
|
2023-01-28 05:28:14 +00:00
|
|
|
|
2023-02-14 00:10:11 +00:00
|
|
|
cache_key = (model_path, params.format)
|
|
|
|
cache_pipe = server.cache.get("resrgan", cache_key)
|
|
|
|
if cache_pipe is not None:
|
2023-02-05 13:53:26 +00:00
|
|
|
logger.info("reusing existing Real ESRGAN pipeline")
|
2023-02-14 00:10:11 +00:00
|
|
|
return cache_pipe
|
|
|
|
|
|
|
|
if not path.isfile(model_path):
|
2023-02-20 04:10:35 +00:00
|
|
|
raise FileNotFoundError("Real ESRGAN model not found at %s" % model_path)
|
2023-01-28 05:28:14 +00:00
|
|
|
|
2023-02-18 14:41:15 +00:00
|
|
|
if params.format == "onnx":
|
2023-02-07 03:36:20 +00:00
|
|
|
# use ONNX acceleration, if available
|
2023-02-05 13:53:26 +00:00
|
|
|
model = OnnxNet(
|
2023-02-14 00:33:06 +00:00
|
|
|
server,
|
|
|
|
model_file,
|
2023-02-15 00:57:50 +00:00
|
|
|
provider=device.ort_provider(),
|
|
|
|
sess_options=device.sess_options(),
|
2023-02-05 13:53:26 +00:00
|
|
|
)
|
|
|
|
elif params.format == "pth":
|
2023-02-18 14:46:46 +00:00
|
|
|
if TAG_X4_V3 in model_file:
|
2023-02-18 05:25:42 +00:00
|
|
|
# the x4-v3 model needs a different network
|
|
|
|
model = SRVGGNetCompact(
|
|
|
|
num_in_ch=3,
|
|
|
|
num_out_ch=3,
|
|
|
|
num_feat=64,
|
|
|
|
num_conv=32,
|
|
|
|
upscale=4,
|
|
|
|
act_type="prelu",
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
model = RRDBNet(
|
|
|
|
num_in_ch=3,
|
|
|
|
num_out_ch=3,
|
|
|
|
num_feat=64,
|
|
|
|
num_block=23,
|
|
|
|
num_grow_ch=32,
|
|
|
|
scale=params.scale,
|
|
|
|
)
|
2023-02-07 03:36:20 +00:00
|
|
|
else:
|
2023-02-20 04:10:35 +00:00
|
|
|
raise ValueError("unknown platform %s" % params.format)
|
2023-01-28 05:28:14 +00:00
|
|
|
|
|
|
|
dni_weight = None
|
2023-02-18 14:41:15 +00:00
|
|
|
if params.upscale_model == TAG_X4_V3 and params.denoise != 1:
|
|
|
|
wdn_model_path = model_path.replace(TAG_X4_V3, "%s-wdn" % TAG_X4_V3)
|
2023-01-28 05:28:14 +00:00
|
|
|
model_path = [model_path, wdn_model_path]
|
|
|
|
dni_weight = [params.denoise, 1 - params.denoise]
|
|
|
|
|
2023-02-05 13:53:26 +00:00
|
|
|
logger.debug("loading Real ESRGAN upscale model from %s", model_path)
|
2023-02-03 05:34:02 +00:00
|
|
|
|
2023-01-28 05:28:14 +00:00
|
|
|
# TODO: shouldn't need the PTH file
|
2023-02-14 06:12:07 +00:00
|
|
|
model_path_pth = path.join(server.cache_path, ("%s.pth" % params.upscale_model))
|
2023-01-28 05:28:14 +00:00
|
|
|
upsampler = RealESRGANer(
|
|
|
|
scale=params.scale,
|
2023-02-03 05:34:02 +00:00
|
|
|
model_path=model_path_pth,
|
2023-01-28 05:28:14 +00:00
|
|
|
dni_weight=dni_weight,
|
|
|
|
model=model,
|
|
|
|
tile=tile,
|
|
|
|
tile_pad=params.tile_pad,
|
|
|
|
pre_pad=params.pre_pad,
|
2023-02-05 13:53:26 +00:00
|
|
|
half=params.half,
|
|
|
|
)
|
2023-01-28 05:28:14 +00:00
|
|
|
|
2023-02-14 00:10:11 +00:00
|
|
|
server.cache.set("resrgan", cache_key, upsampler)
|
2023-02-17 00:11:35 +00:00
|
|
|
run_gc([device])
|
2023-01-28 05:28:14 +00:00
|
|
|
|
|
|
|
return upsampler
|
|
|
|
|
|
|
|
|
|
|
|
def upscale_resrgan(
|
2023-02-26 05:49:39 +00:00
|
|
|
job: WorkerContext,
|
2023-02-05 03:17:39 +00:00
|
|
|
server: ServerContext,
|
2023-01-28 05:28:14 +00:00
|
|
|
stage: StageParams,
|
|
|
|
_params: ImageParams,
|
2023-02-18 22:35:57 +00:00
|
|
|
source: Image.Image,
|
2023-01-28 05:28:14 +00:00
|
|
|
*,
|
|
|
|
upscale: UpscaleParams,
|
2023-02-19 04:11:44 +00:00
|
|
|
stage_source: Image.Image = None,
|
2023-01-29 04:31:34 +00:00
|
|
|
**kwargs,
|
2023-01-28 18:42:02 +00:00
|
|
|
) -> Image.Image:
|
2023-02-19 04:11:44 +00:00
|
|
|
source = stage_source or source
|
2023-02-05 13:53:26 +00:00
|
|
|
logger.info("upscaling image with Real ESRGAN: x%s", upscale.scale)
|
2023-01-28 05:28:14 +00:00
|
|
|
|
2023-02-18 22:35:57 +00:00
|
|
|
output = np.array(source)
|
2023-02-05 03:17:39 +00:00
|
|
|
upsampler = load_resrgan(server, upscale, job.get_device(), tile=stage.tile_size)
|
2023-01-28 05:28:14 +00:00
|
|
|
|
|
|
|
output, _ = upsampler.enhance(output, outscale=upscale.outscale)
|
|
|
|
|
2023-02-05 13:53:26 +00:00
|
|
|
output = Image.fromarray(output, "RGB")
|
|
|
|
logger.info("final output image size: %sx%s", output.width, output.height)
|
2023-01-28 05:28:14 +00:00
|
|
|
return output
|