DL-Art-School/codes/models/archs/RRDBNet_arch.py

145 lines
4.8 KiB
Python
Raw Normal View History

2019-08-23 13:42:47 +00:00
import torch
import torch.nn as nn
import torch.nn.functional as F
2020-10-27 16:25:31 +00:00
from torch.utils.checkpoint import checkpoint_sequential
2019-08-23 13:42:47 +00:00
2020-10-27 16:25:31 +00:00
from models.archs.arch_util import make_layer, default_init_weights
2019-08-23 13:42:47 +00:00
2020-10-27 16:25:31 +00:00
class ResidualDenseBlock(nn.Module):
"""Residual Dense Block.
Used in RRDB block in ESRGAN.
Args:
mid_channels (int): Channel number of intermediate features.
growth_channels (int): Channels for each growth.
"""
def __init__(self, mid_channels=64, growth_channels=32):
super(ResidualDenseBlock, self).__init__()
for i in range(5):
out_channels = mid_channels if i == 4 else growth_channels
self.add_module(
f'conv{i+1}',
nn.Conv2d(mid_channels + i * growth_channels, out_channels, 3,
1, 1))
2019-08-23 13:42:47 +00:00
self.lrelu = nn.LeakyReLU(negative_slope=0.2, inplace=True)
2020-10-27 16:25:31 +00:00
for i in range(5):
default_init_weights(getattr(self, f'conv{i+1}'), 0.1)
2019-08-23 13:42:47 +00:00
def forward(self, x):
2020-10-27 16:25:31 +00:00
"""Forward function.
Args:
x (Tensor): Input tensor with shape (n, c, h, w).
Returns:
Tensor: Forward results.
"""
2019-08-23 13:42:47 +00:00
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))
2020-10-27 16:25:31 +00:00
# Emperically, we use 0.2 to scale the residual for better performance
2019-08-23 13:42:47 +00:00
return x5 * 0.2 + x
2020-06-06 03:02:08 +00:00
2019-08-23 13:42:47 +00:00
class RRDB(nn.Module):
2020-10-27 16:25:31 +00:00
"""Residual in Residual Dense Block.
2019-08-23 13:42:47 +00:00
2020-10-27 16:25:31 +00:00
Used in RRDB-Net in ESRGAN.
2020-10-27 16:25:31 +00:00
Args:
mid_channels (int): Channel number of intermediate features.
growth_channels (int): Channels for each growth.
"""
2020-10-27 16:25:31 +00:00
def __init__(self, mid_channels, growth_channels=32):
super(RRDB, self).__init__()
self.rdb1 = ResidualDenseBlock(mid_channels, growth_channels)
self.rdb2 = ResidualDenseBlock(mid_channels, growth_channels)
self.rdb3 = ResidualDenseBlock(mid_channels, growth_channels)
2020-06-06 03:02:08 +00:00
2019-08-23 13:42:47 +00:00
def forward(self, x):
2020-10-27 16:25:31 +00:00
"""Forward function.
Args:
x (Tensor): Input tensor with shape (n, c, h, w).
Returns:
Tensor: Forward results.
"""
out = self.rdb1(x)
out = self.rdb2(out)
out = self.rdb3(out)
# Emperically, we use 0.2 to scale the residual for better performance
return out * 0.2 + x
2020-06-09 19:28:55 +00:00
2020-10-27 16:25:31 +00:00
class RRDBNet(nn.Module):
"""Networks consisting of Residual in Residual Dense Block, which is used
in ESRGAN.
ESRGAN: Enhanced Super-Resolution Generative Adversarial Networks.
Currently, it supports x4 upsampling scale factor.
Args:
in_channels (int): Channel number of inputs.
out_channels (int): Channel number of outputs.
mid_channels (int): Channel number of intermediate features.
Default: 64
num_blocks (int): Block number in the trunk network. Defaults: 23
growth_channels (int): Channels for each growth. Default: 32.
"""
def __init__(self,
in_channels,
out_channels,
mid_channels=64,
num_blocks=23,
growth_channels=32):
2020-06-09 19:28:55 +00:00
super(RRDBNet, self).__init__()
2020-10-27 16:25:31 +00:00
self.conv_first = nn.Conv2d(in_channels, mid_channels, 3, 1, 1)
self.body = make_layer(
RRDB,
num_blocks,
mid_channels=mid_channels,
growth_channels=growth_channels)
self.conv_body = nn.Conv2d(mid_channels, mid_channels, 3, 1, 1)
# upsample
self.conv_up1 = nn.Conv2d(mid_channels, mid_channels, 3, 1, 1)
self.conv_up2 = nn.Conv2d(mid_channels, mid_channels, 3, 1, 1)
self.conv_hr = nn.Conv2d(mid_channels, mid_channels, 3, 1, 1)
self.conv_last = nn.Conv2d(mid_channels, out_channels, 3, 1, 1)
2020-06-09 19:28:55 +00:00
self.lrelu = nn.LeakyReLU(negative_slope=0.2, inplace=True)
2020-10-27 16:25:31 +00:00
for m in [
self.conv_first, self.conv_body, self.conv_up1,
self.conv_up2, self.conv_hr, self.conv_last
]:
default_init_weights(m, 0.1)
def forward(self, x):
2020-10-27 16:25:31 +00:00
"""Forward function.
Args:
x (Tensor): Input tensor with shape (n, c, h, w).
Returns:
Tensor: Forward results.
"""
feat = self.conv_first(x)
body_feat = self.conv_body(checkpoint_sequential(self.body, 5, feat))
feat = feat + body_feat
# upsample
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')))
out = self.conv_last(self.lrelu(self.conv_hr(feat)))
return out