-
Notifications
You must be signed in to change notification settings - Fork 7
/
model.py
70 lines (57 loc) · 2.3 KB
/
model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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