Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ONNX acceleration for Real ESRGAN v3 #175

Merged
merged 3 commits into from
Feb 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 23 additions & 22 deletions api/onnx_web/chain/upscale_resrgan.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
last_pipeline_instance = None
last_pipeline_params = (None, None)

x4_v3_tag = "real-esrgan-x4-v3"
TAG_X4_V3 = "real-esrgan-x4-v3"


def load_resrgan(
Expand All @@ -37,17 +37,7 @@ def load_resrgan(
if not path.isfile(model_path):
raise Exception("Real ESRGAN model not found at %s" % model_path)

if x4_v3_tag in model_file:
# 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",
)
elif params.format == "onnx":
if params.format == "onnx":
# use ONNX acceleration, if available
model = OnnxNet(
server,
Expand All @@ -56,20 +46,31 @@ def load_resrgan(
sess_options=device.sess_options(),
)
elif params.format == "pth":
model = RRDBNet(
num_in_ch=3,
num_out_ch=3,
num_feat=64,
num_block=23,
num_grow_ch=32,
scale=params.scale,
)
if TAG_X4_V3 in model_file:
# 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,
)
else:
raise Exception("unknown platform %s" % params.format)

dni_weight = None
if params.upscale_model == x4_v3_tag and params.denoise != 1:
wdn_model_path = model_path.replace(x4_v3_tag, "%s-wdn" % (x4_v3_tag))
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)
model_path = [model_path, wdn_model_path]
dni_weight = [params.denoise, 1 - params.denoise]

Expand Down
31 changes: 23 additions & 8 deletions api/onnx_web/convert/upscale_resrgan.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@

import torch
from basicsr.archs.rrdbnet_arch import RRDBNet
from realesrgan.archs.srvgg_arch import SRVGGNetCompact
from torch.onnx import export

from .utils import ConversionContext, ModelDict

logger = getLogger(__name__)

TAG_X4_V3 = "real-esrgan-x4-v3"


@torch.no_grad()
def convert_upscale_resrgan(
Expand All @@ -28,14 +31,26 @@ def convert_upscale_resrgan(
return

logger.info("loading and training model")
model = RRDBNet(
num_in_ch=3,
num_out_ch=3,
num_feat=64,
num_block=23,
num_grow_ch=32,
scale=scale,
)

if TAG_X4_V3 in name:
# 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=scale,
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=scale,
)

torch_model = torch.load(source, map_location=ctx.map_location)
if "params_ema" in torch_model:
Expand Down
2 changes: 1 addition & 1 deletion api/onnx_web/onnx/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from .onnx_net import OnnxImage, OnnxNet
from .onnx_net import OnnxTensor, OnnxNet
4 changes: 2 additions & 2 deletions api/onnx_web/onnx/onnx_net.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from ..utils import ServerContext


class OnnxImage:
class OnnxTensor:
def __init__(self, source) -> None:
self.source = source
self.data = self
Expand Down Expand Up @@ -58,7 +58,7 @@ def __call__(self, image: Any) -> Any:
input_name = self.session.get_inputs()[0].name
output_name = self.session.get_outputs()[0].name
output = self.session.run([output_name], {input_name: image.cpu().numpy()})[0]
return OnnxImage(output)
return OnnxTensor(output)

def eval(self) -> None:
pass
Expand Down