"""RRDBNet architecture (Real-ESRGAN / ESRGAN generator). Self-contained re-implementation of `basicsr.archs.rrdbnet_arch.RRDBNet` so that the OpenVINO export does not require the whole BasicSR package to be installed. The module layout (parameter names) is identical to the original `RealESRGAN_x4plus` checkpoint, so `load_state_dict(..., strict=True)` works. """ import torch from torch import nn as nn from torch.nn import functional as F class ResidualDenseBlock(nn.Module): """Residual dense block used inside every RRDB.""" def __init__(self, num_feat=64, num_grow_ch=32): super().__init__() self.conv1 = nn.Conv2d(num_feat, num_grow_ch, 3, 1, 1) self.conv2 = nn.Conv2d(num_feat + num_grow_ch, num_grow_ch, 3, 1, 1) self.conv3 = nn.Conv2d(num_feat + 2 * num_grow_ch, num_grow_ch, 3, 1, 1) self.conv4 = nn.Conv2d(num_feat + 3 * num_grow_ch, num_grow_ch, 3, 1, 1) self.conv5 = nn.Conv2d(num_feat + 4 * num_grow_ch, num_feat, 3, 1, 1) self.lrelu = nn.LeakyReLU(negative_slope=0.2, inplace=True) def forward(self, x): x1 = self.lrelu(self.conv1(x)) x2 = self.lrelu(self.conv2(torch.cat((x, x1), 1))) x3 = self.lrelu(self.conv3(torch.cat((x, x1, x2), 1))) x4 = self.lrelu(self.conv4(torch.cat((x, x1, x2, x3), 1))) x5 = self.conv5(torch.cat((x, x1, x2, x3, x4), 1)) return x5 * 0.2 + x class RRDB(nn.Module): """Residual-in-residual dense block.""" def __init__(self, num_feat, num_grow_ch=32): super().__init__() self.rdb1 = ResidualDenseBlock(num_feat, num_grow_ch) self.rdb2 = ResidualDenseBlock(num_feat, num_grow_ch) self.rdb3 = ResidualDenseBlock(num_feat, num_grow_ch) def forward(self, x): out = self.rdb1(x) out = self.rdb2(out) out = self.rdb3(out) return out * 0.2 + x class RRDBNet(nn.Module): """The ESRGAN / Real-ESRGAN generator. `RealESRGAN_x4plus` uses num_feat=64, num_block=23, num_grow_ch=32, scale=4. """ def __init__(self, num_in_ch=3, num_out_ch=3, scale=4, num_feat=64, num_block=23, num_grow_ch=32): super().__init__() self.scale = scale if scale == 2: num_in_ch = num_in_ch * 4 elif scale == 1: num_in_ch = num_in_ch * 16 self.conv_first = nn.Conv2d(num_in_ch, num_feat, 3, 1, 1) self.body = nn.Sequential(*[RRDB(num_feat, num_grow_ch) for _ in range(num_block)]) self.conv_body = nn.Conv2d(num_feat, num_feat, 3, 1, 1) self.conv_up1 = nn.Conv2d(num_feat, num_feat, 3, 1, 1) self.conv_up2 = nn.Conv2d(num_feat, num_feat, 3, 1, 1) self.conv_hr = nn.Conv2d(num_feat, num_feat, 3, 1, 1) self.conv_last = nn.Conv2d(num_feat, num_out_ch, 3, 1, 1) self.lrelu = nn.LeakyReLU(negative_slope=0.2, inplace=True) def forward(self, x): feat = self.conv_first(x) body_feat = self.conv_body(self.body(feat)) feat = feat + body_feat feat = self.lrelu(self.conv_up1(F.interpolate(feat, scale_factor=2, mode='nearest'))) feat = self.lrelu(self.conv_up2(F.interpolate(feat, scale_factor=2, mode='nearest'))) return self.conv_last(self.lrelu(self.conv_hr(feat)))