# BSD 3-Clause License
#
# Copyright (c) 2017 xxxx
# All rights reserved.
# Copyright 2021 Huawei Technologies Co., Ltd
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# * Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# ============================================================================
from torch import nn
class ChannelAttention(nn.Module):
def __init__(self, num_features, reduction):
super(ChannelAttention, self).__init__()
self.module = nn.Sequential(
nn.AdaptiveAvgPool2d(1),
nn.Conv2d(num_features, num_features // reduction, kernel_size=1),
nn.ReLU(inplace=True),
nn.Conv2d(num_features // reduction, num_features, kernel_size=1),
nn.Sigmoid()
)
def forward(self, x):
return x * self.module(x)
class RCAB(nn.Module):
def __init__(self, num_features, reduction):
super(RCAB, self).__init__()
self.module = nn.Sequential(
nn.Conv2d(num_features, num_features, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(num_features, num_features, kernel_size=3, padding=1),
ChannelAttention(num_features, reduction)
)
def forward(self, x):
return x + self.module(x)
class RG(nn.Module):
def __init__(self, num_features, num_rcab, reduction):
super(RG, self).__init__()
self.module = [RCAB(num_features, reduction) for _ in range(num_rcab)]
self.module.append(nn.Conv2d(num_features, num_features, kernel_size=3, padding=1))
self.module = nn.Sequential(*self.module)
def forward(self, x):
return x + self.module(x)
class RCAN(nn.Module):
def __init__(self, args):
super(RCAN, self).__init__()
scale = args.scale
num_features = args.num_features
num_rg = args.num_rg
num_rcab = args.num_rcab
reduction = args.reduction
self.sf = nn.Conv2d(3, num_features, kernel_size=3, padding=1)
self.rgs = nn.Sequential(*[RG(num_features, num_rcab, reduction) for _ in range(num_rg)])
self.conv1 = nn.Conv2d(num_features, num_features, kernel_size=3, padding=1)
self.upscale = nn.Sequential(
nn.Conv2d(num_features, num_features * (scale ** 2), kernel_size=3, padding=1),
nn.PixelShuffle(scale)
)
self.conv2 = nn.Conv2d(num_features, 3, kernel_size=3, padding=1)
def forward(self, x):
x = self.sf(x)
residual = x
x = self.rgs(x)
x = self.conv1(x)
x += residual
x = self.upscale(x)
x = self.conv2(x)
return x