You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

195 lines
5.7 KiB

# Based on https://github.com/kongdebug/RCAN-Paddle
import math
2 years ago
import paddle
import paddle.nn as nn
from .builder import GENERATORS
def default_conv(in_channels, out_channels, kernel_size, bias=True):
Squashed commit of the following: commit de5651935774034c31be300c421529aafa54b43f Author: Bobholamovic <bob1998425@hotmail.com> Date: Mon Jul 18 02:14:40 2022 +0800 Update workflow commit 25d7f0cdd2fee5e01eaf4c222baec14043c718ad Author: Bobholamovic <bob1998425@hotmail.com> Date: Mon Jul 18 01:57:45 2022 +0800 Test cuda commit e908e613797cf4b8d60bee012b54ebfc5888f021 Author: Bobholamovic <bob1998425@hotmail.com> Date: Mon Jul 18 01:39:43 2022 +0800 Fix bug commit 563fb86d03fd509283f626443029e82e3cfcae48 Author: Bobholamovic <bob1998425@hotmail.com> Date: Mon Jul 18 01:33:05 2022 +0800 Test deploy commit 8d285f94604552bc5944768bb89251ab1a53c1eb Author: Bobholamovic <bob1998425@hotmail.com> Date: Mon Jul 18 01:08:12 2022 +0800 Do not check tools commit 227d98eb7967f0444f5bf7c04fc17d98f6f6dfc1 Author: Bobholamovic <bob1998425@hotmail.com> Date: Mon Jul 18 00:36:51 2022 +0800 test_static_forward->test_to_static commit 91a3cfafb22a645eebaaaff887ca7c7ace2ccb44 Author: Bobholamovic <bob1998425@hotmail.com> Date: Mon Jul 18 00:13:04 2022 +0800 Fix bugs in test_models commit c0bd01ef9d9493f1fe8dc4301a51213cd0814ced Author: Bobholamovic <bob1998425@hotmail.com> Date: Sun Jul 17 23:45:17 2022 +0800 Update URL commit b2718b73a9d93f4c27bc0957b7b9349d4be028ba Author: Bobholamovic <bob1998425@hotmail.com> Date: Sun Jul 17 23:36:32 2022 +0800 Update workflow commit 25ad5ae1477719c1e90e0b1005667c46508e4d9e Author: Bobholamovic <bob1998425@hotmail.com> Date: Sun Jul 17 23:31:31 2022 +0800 Fix bugs commit 4780b365a73557d71f2a1156657cadb2e235e9d1 Author: Bobholamovic <bob1998425@hotmail.com> Date: Sun Jul 17 23:05:00 2022 +0800 Update workflow commit bb79a83447e2efc12d0e1c3a7ed52effcac4e635 Author: Bobholamovic <bob1998425@hotmail.com> Date: Sun Jul 17 22:48:31 2022 +0800 Update workflow commit 1dec49161a7c03c651910772064149f86a2545c0 Author: Bobholamovic <bob1998425@hotmail.com> Date: Sun Jul 17 22:39:17 2022 +0800 Update workflow commit 9df503913be03f147ed4d7d01a4b765f0a03cc1e Author: Bobholamovic <bob1998425@hotmail.com> Date: Sun Jul 17 22:26:07 2022 +0800 Fast tests commit 665a872e6dbf0c326d12c4a86d99315bed6be417 Author: Bobholamovic <bob1998425@hotmail.com> Date: Sun Jul 17 21:16:18 2022 +0800 Update workflows commit d8978cbb2c4638a6cba9bc1378eedeefa36bee60 Author: Bobholamovic <bob1998425@hotmail.com> Date: Sun Jul 17 20:52:30 2022 +0800 Add empty file commit d704501b570a2b9314b3b0974e775a58e2cccfaf Author: Bobholamovic <bob1998425@hotmail.com> Date: Sun Jul 17 20:47:11 2022 +0800 Add install coverage commit dd7c2c4bfd89eb0236ba8f39155a72ba04bf9bce Author: Bobholamovic <bob1998425@hotmail.com> Date: Sun Jul 17 20:39:45 2022 +0800 Add yapf disable commit 1bdebad783ddeab165acafbadd69b42f587bb742 Author: Bobholamovic <bob1998425@hotmail.com> Date: Sun Jul 17 20:19:57 2022 +0800 Apply pre-commit check commit 2de597a5d67bbe2c9bd8ac3f6f3b27b886e9b7d4 Merge: 5f06a86 dfb6978 Author: Bobholamovic <bob1998425@hotmail.com> Date: Sun Jul 17 19:52:54 2022 +0800 Merge branch 'unittest' into develop commit 5f06a861735bb80cde0bca63889ac647664958ad Author: Lin Manhui <mhlin425@whu.edu.cn> Date: Sun Jul 17 19:23:06 2022 +0800 Add unittests (#2) * Init unittest * Add rs_model unittests * Refactor tools * Add data for unittests * Add transforms unittests * test_utils->testing_utils * Add tool unittests * Add scripts for unittests * Rename and polish doc/comments * Update test_operators.py * Add coco det data * Add tools unittests * Update style * Update rs_models unittests * Add predictor unittests * Add tutorial tests * Add github workflow
2 years ago
weight_attr = paddle.ParamAttr(
initializer=paddle.nn.initializer.XavierUniform(), need_clip=True)
return nn.Conv2D(
in_channels,
out_channels,
kernel_size,
padding=(kernel_size // 2),
weight_attr=weight_attr,
bias_attr=bias)
class MeanShift(nn.Conv2D):
def __init__(self, rgb_range, rgb_mean, rgb_std, sign=-1):
super(MeanShift, self).__init__(3, 3, kernel_size=1)
std = paddle.to_tensor(rgb_std)
self.weight.set_value(paddle.eye(3).reshape([3, 3, 1, 1]))
self.weight.set_value(self.weight / (std.reshape([3, 1, 1, 1])))
mean = paddle.to_tensor(rgb_mean)
self.bias.set_value(sign * rgb_range * mean / std)
self.weight.trainable = False
self.bias.trainable = False
## Channel Attention (CA) Layer
class CALayer(nn.Layer):
def __init__(self, channel, reduction=16):
super(CALayer, self).__init__()
# Global average pooling: feature --> point
self.avg_pool = nn.AdaptiveAvgPool2D(1)
# Feature channel downscale and upscale --> channel weight
self.conv_du = nn.Sequential(
nn.Conv2D(
channel, channel // reduction, 1, padding=0, bias_attr=True),
nn.ReLU(),
nn.Conv2D(
channel // reduction, channel, 1, padding=0, bias_attr=True),
nn.Sigmoid())
def forward(self, x):
y = self.avg_pool(x)
y = self.conv_du(y)
return x * y
class RCAB(nn.Layer):
def __init__(self,
conv,
n_feat,
kernel_size,
reduction=16,
bias=True,
bn=False,
act=nn.ReLU(),
res_scale=1):
super(RCAB, self).__init__()
modules_body = []
for i in range(2):
modules_body.append(conv(n_feat, n_feat, kernel_size, bias=bias))
if bn: modules_body.append(nn.BatchNorm2D(n_feat))
if i == 0: modules_body.append(act)
modules_body.append(CALayer(n_feat, reduction))
self.body = nn.Sequential(*modules_body)
self.res_scale = res_scale
def forward(self, x):
res = self.body(x)
res += x
return res
## Residual Group (RG)
class ResidualGroup(nn.Layer):
def __init__(self, conv, n_feat, kernel_size, reduction, act, res_scale,
n_resblocks):
super(ResidualGroup, self).__init__()
modules_body = []
modules_body = [
RCAB(
conv, n_feat, kernel_size, reduction, bias=True, bn=False, act=nn.ReLU(), res_scale=1) \
for _ in range(n_resblocks)]
modules_body.append(conv(n_feat, n_feat, kernel_size))
self.body = nn.Sequential(*modules_body)
def forward(self, x):
res = self.body(x)
res += x
return res
class Upsampler(nn.Sequential):
def __init__(self, conv, scale, n_feats, bn=False, act=False, bias=True):
m = []
if (scale & (scale - 1)) == 0: # Is scale = 2^n?
for _ in range(int(math.log(scale, 2))):
m.append(conv(n_feats, 4 * n_feats, 3, bias))
m.append(nn.PixelShuffle(2))
if bn: m.append(nn.BatchNorm2D(n_feats))
if act == 'relu':
m.append(nn.ReLU())
elif act == 'prelu':
m.append(nn.PReLU(n_feats))
elif scale == 3:
m.append(conv(n_feats, 9 * n_feats, 3, bias))
m.append(nn.PixelShuffle(3))
if bn: m.append(nn.BatchNorm2D(n_feats))
if act == 'relu':
m.append(nn.ReLU())
elif act == 'prelu':
m.append(nn.PReLU(n_feats))
else:
raise NotImplementedError
super(Upsampler, self).__init__(*m)
@GENERATORS.register()
class RCAN(nn.Layer):
def __init__(
self,
scale,
n_resgroups,
n_resblocks,
n_feats=64,
n_colors=3,
rgb_range=255,
kernel_size=3,
reduction=16,
conv=default_conv, ):
super(RCAN, self).__init__()
self.scale = scale
act = nn.ReLU()
n_resgroups = n_resgroups
n_resblocks = n_resblocks
n_feats = n_feats
kernel_size = kernel_size
reduction = reduction
scale = scale
act = nn.ReLU()
rgb_mean = (0.4488, 0.4371, 0.4040)
rgb_std = (1.0, 1.0, 1.0)
self.sub_mean = MeanShift(rgb_range, rgb_mean, rgb_std)
# Define head module
modules_head = [conv(n_colors, n_feats, kernel_size)]
# Define body module
modules_body = [
ResidualGroup(
conv, n_feats, kernel_size, reduction, act=act, res_scale= 1, n_resblocks=n_resblocks) \
for _ in range(n_resgroups)]
modules_body.append(conv(n_feats, n_feats, kernel_size))
# Define tail module
modules_tail = [
Upsampler(
conv, scale, n_feats, act=False),
conv(n_feats, n_colors, kernel_size)
]
self.head = nn.Sequential(*modules_head)
self.body = nn.Sequential(*modules_body)
self.tail = nn.Sequential(*modules_tail)
self.add_mean = MeanShift(rgb_range, rgb_mean, rgb_std, 1)
def forward(self, x):
x = self.sub_mean(x)
x = self.head(x)
res = self.body(x)
res += x
x = self.tail(res)
x = self.add_mean(x)
return x