| """RRDBNet (ESRGAN generator). Weight keys match official Real-ESRGAN checkpoints."""
|
| import torch
|
| import torch.nn as nn
|
| import torch.nn.functional as F
|
|
|
|
|
| class ResidualDenseBlock(nn.Module):
|
| def __init__(self, num_features: int, growth_channels: int):
|
| super().__init__()
|
| self.conv1 = nn.Conv2d(num_features, growth_channels, 3, 1, 1)
|
| self.conv2 = nn.Conv2d(num_features + growth_channels, growth_channels, 3, 1, 1)
|
| self.conv3 = nn.Conv2d(num_features + 2 * growth_channels, growth_channels, 3, 1, 1)
|
| self.conv4 = nn.Conv2d(num_features + 3 * growth_channels, growth_channels, 3, 1, 1)
|
| self.conv5 = nn.Conv2d(num_features + 4 * growth_channels, num_features, 3, 1, 1)
|
| self.lrelu = nn.LeakyReLU(0.2, inplace=True)
|
|
|
| def forward(self, x):
|
| f1 = self.lrelu(self.conv1(x))
|
| f2 = self.lrelu(self.conv2(torch.cat((x, f1), 1)))
|
| f3 = self.lrelu(self.conv3(torch.cat((x, f1, f2), 1)))
|
| f4 = self.lrelu(self.conv4(torch.cat((x, f1, f2, f3), 1)))
|
| f5 = self.conv5(torch.cat((x, f1, f2, f3, f4), 1))
|
| return x + 0.2 * f5
|
|
|
|
|
| class RRDB(nn.Module):
|
| def __init__(self, num_features: int, growth_channels: int):
|
| super().__init__()
|
|
|
| self.rdb1 = ResidualDenseBlock(num_features, growth_channels)
|
| self.rdb2 = ResidualDenseBlock(num_features, growth_channels)
|
| self.rdb3 = ResidualDenseBlock(num_features, growth_channels)
|
|
|
| def forward(self, x):
|
| out = self.rdb3(self.rdb2(self.rdb1(x)))
|
| return x + 0.2 * out
|
|
|
|
|
| class RRDBNet(nn.Module):
|
| def __init__(self, num_features=64, num_blocks=16, growth_channels=32, scale=4):
|
| super().__init__()
|
| assert scale == 4, "This head is built for 4x"
|
| self.conv_first = nn.Conv2d(3, num_features, 3, 1, 1)
|
| self.body = nn.Sequential(*[RRDB(num_features, growth_channels) for _ in range(num_blocks)])
|
| self.conv_body = nn.Conv2d(num_features, num_features, 3, 1, 1)
|
| self.conv_up1 = nn.Conv2d(num_features, num_features, 3, 1, 1)
|
| self.conv_up2 = nn.Conv2d(num_features, num_features, 3, 1, 1)
|
| self.conv_hr = nn.Conv2d(num_features, num_features, 3, 1, 1)
|
| self.conv_last = nn.Conv2d(num_features, 3, 3, 1, 1)
|
| self.lrelu = nn.LeakyReLU(0.2, inplace=True)
|
|
|
| def forward(self, x):
|
| shallow = self.conv_first(x)
|
| deep = self.conv_body(self.body(shallow))
|
| features = shallow + deep
|
| features = self.lrelu(self.conv_up1(F.interpolate(features, scale_factor=2, mode="nearest")))
|
| features = self.lrelu(self.conv_up2(F.interpolate(features, scale_factor=2, mode="nearest")))
|
| return self.conv_last(self.lrelu(self.conv_hr(features)))
|
|
|
|
|
|
|