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neosr.archs.arch_util import net_opt
|
| 5 |
+
from neosr.utils.registry import ARCH_REGISTRY
|
| 6 |
+
|
| 7 |
+
upscale, __ = net_opt()
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def conv3x3(in_channels, out_channels, bias=True):
|
| 11 |
+
return nn.Conv2d(
|
| 12 |
+
in_channels, out_channels, kernel_size=3, stride=1, padding=1, bias=bias
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
class LWGRB(nn.Module):
|
| 17 |
+
def __init__(self, channels: int, bias: bool = True, identity=True):
|
| 18 |
+
super().__init__()
|
| 19 |
+
self.identity = identity
|
| 20 |
+
self.conv1 = conv3x3(channels, channels, bias)
|
| 21 |
+
self.act = nn.LeakyReLU(0.1, inplace=True)
|
| 22 |
+
self.conv2 = conv3x3(channels, channels, bias)
|
| 23 |
+
nn.init.zeros_(self.conv2.weight)
|
| 24 |
+
nn.init.zeros_(self.conv2.bias)
|
| 25 |
+
|
| 26 |
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
| 27 |
+
r = self.conv2(self.act(self.conv1(x)))
|
| 28 |
+
a = torch.sigmoid(r)
|
| 29 |
+
return x + r * a
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
class LWDRB(nn.Module):
|
| 33 |
+
def __init__(self, c, bias=True, dil=4):
|
| 34 |
+
super().__init__()
|
| 35 |
+
self.conv1 = conv3x3(c, c, bias)
|
| 36 |
+
self.act = nn.LeakyReLU(0.1, inplace=True)
|
| 37 |
+
self.conv2 = nn.Conv2d(c, c, 3, padding=dil, dilation=dil, bias=bias)
|
| 38 |
+
nn.init.zeros_(self.conv2.weight)
|
| 39 |
+
nn.init.zeros_(self.conv2.bias)
|
| 40 |
+
|
| 41 |
+
def forward(self, x):
|
| 42 |
+
r = self.conv2(self.act(self.conv1(x)))
|
| 43 |
+
return x + r
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
class LWGRBShuffle(nn.Module):
|
| 47 |
+
def __init__(self, in_ch, out_ch, scale, bias: bool = True):
|
| 48 |
+
super().__init__()
|
| 49 |
+
self.expand = conv3x3(in_ch, out_ch * scale * scale, bias)
|
| 50 |
+
self.up = nn.PixelShuffle(scale)
|
| 51 |
+
self.refine = LWGRB(out_ch, bias=bias)
|
| 52 |
+
|
| 53 |
+
def forward(self, x):
|
| 54 |
+
x = self.expand(x)
|
| 55 |
+
x = self.up(x)
|
| 56 |
+
return self.refine(x)
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
@ARCH_REGISTRY.register()
|
| 60 |
+
class GRDFNet(nn.Module):
|
| 61 |
+
"""
|
| 62 |
+
GRDFNet (Gated + Residual Dilated Fast Network)
|
| 63 |
+
Configurable block stacking via integer `num_sets`.
|
| 64 |
+
|
| 65 |
+
Structure:
|
| 66 |
+
Stem: 3xLWGRB + 2xLWDRB
|
| 67 |
+
Repeated: num_sets x [LWGRB + LWDRB]
|
| 68 |
+
"""
|
| 69 |
+
|
| 70 |
+
def __init__(
|
| 71 |
+
self,
|
| 72 |
+
num_in_ch: int = 3,
|
| 73 |
+
num_out_ch: int = 3,
|
| 74 |
+
feature_channels: int = 32,
|
| 75 |
+
upscale: int = upscale,
|
| 76 |
+
bias: bool = True,
|
| 77 |
+
norm: bool = False,
|
| 78 |
+
img_range: float = 1.0,
|
| 79 |
+
rgb_mean=(0.5, 0.5, 0.5),
|
| 80 |
+
num_sets: int = 3,
|
| 81 |
+
):
|
| 82 |
+
super().__init__()
|
| 83 |
+
|
| 84 |
+
self.in_ch = num_in_ch
|
| 85 |
+
self.out_ch = num_out_ch
|
| 86 |
+
self.c = feature_channels
|
| 87 |
+
self.scale = upscale
|
| 88 |
+
self.img_range = img_range
|
| 89 |
+
self.gamma = nn.Parameter(torch.tensor(0.5))
|
| 90 |
+
self.num_sets = num_sets
|
| 91 |
+
|
| 92 |
+
self.mean = torch.Tensor(rgb_mean).view(1, 3, 1, 1)
|
| 93 |
+
if not norm:
|
| 94 |
+
self.register_buffer("no_norm", torch.zeros(1))
|
| 95 |
+
else:
|
| 96 |
+
self.no_norm = None
|
| 97 |
+
|
| 98 |
+
self.head = conv3x3(self.in_ch, self.c, bias)
|
| 99 |
+
self.body = self._make_body(bias)
|
| 100 |
+
self.tail = conv3x3(self.c, self.out_ch, bias)
|
| 101 |
+
|
| 102 |
+
if self.scale == 1:
|
| 103 |
+
self.upsample0 = nn.Identity()
|
| 104 |
+
else:
|
| 105 |
+
self.upsample0 = LWGRBShuffle(
|
| 106 |
+
self.out_ch, self.out_ch, self.scale, bias=bias
|
| 107 |
+
)
|
| 108 |
+
|
| 109 |
+
def _make_body(self, bias: bool):
|
| 110 |
+
blocks = [
|
| 111 |
+
LWGRB(self.c, bias),
|
| 112 |
+
LWGRB(self.c, bias),
|
| 113 |
+
LWGRB(self.c, bias),
|
| 114 |
+
LWDRB(self.c, bias),
|
| 115 |
+
LWDRB(self.c, bias),
|
| 116 |
+
]
|
| 117 |
+
for _ in range(self.num_sets):
|
| 118 |
+
blocks += [LWGRB(self.c, bias), LWDRB(self.c, bias)]
|
| 119 |
+
return nn.Sequential(*blocks)
|
| 120 |
+
|
| 121 |
+
@property
|
| 122 |
+
def is_norm(self) -> bool:
|
| 123 |
+
return getattr(self, "no_norm", None) is None
|
| 124 |
+
|
| 125 |
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
| 126 |
+
if self.is_norm:
|
| 127 |
+
self.mean = self.mean.type_as(x)
|
| 128 |
+
x = (x - self.mean) * self.img_range
|
| 129 |
+
|
| 130 |
+
feat = self.head(x)
|
| 131 |
+
feat = self.body(feat)
|
| 132 |
+
out_feat = self.tail(feat)
|
| 133 |
+
out_feat = (1.0 - self.gamma) * x + self.gamma * out_feat
|
| 134 |
+
|
| 135 |
+
out = self.upsample0(out_feat)
|
| 136 |
+
|
| 137 |
+
if self.is_norm:
|
| 138 |
+
out = out / self.img_range + self.mean
|
| 139 |
+
|
| 140 |
+
return out
|