CHEONMA010 commited on
Commit
0356719
·
verified ·
1 Parent(s): dfd4096

Upload RRDBNet_arch.py

Browse files
Files changed (1) hide show
  1. scripts/RRDBNet_arch.py +94 -0
scripts/RRDBNet_arch.py ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright [2021] Xintao Wang
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+
16
+
17
+ import functools
18
+ import torch
19
+ import torch.nn as nn
20
+ import torch.nn.functional as F
21
+
22
+
23
+ def make_layer(block, n_layers):
24
+ layers = []
25
+ for _ in range(n_layers):
26
+ layers.append(block())
27
+ return nn.Sequential(*layers)
28
+
29
+
30
+ class ResidualDenseBlock_5C(nn.Module):
31
+ def __init__(self, nf=64, gc=32, bias=True):
32
+ super(ResidualDenseBlock_5C, self).__init__()
33
+ # gc: growth channel, i.e. intermediate channels
34
+ self.conv1 = nn.Conv2d(nf, gc, 3, 1, 1, bias=bias)
35
+ self.conv2 = nn.Conv2d(nf + gc, gc, 3, 1, 1, bias=bias)
36
+ self.conv3 = nn.Conv2d(nf + 2 * gc, gc, 3, 1, 1, bias=bias)
37
+ self.conv4 = nn.Conv2d(nf + 3 * gc, gc, 3, 1, 1, bias=bias)
38
+ self.conv5 = nn.Conv2d(nf + 4 * gc, nf, 3, 1, 1, bias=bias)
39
+ self.lrelu = nn.LeakyReLU(negative_slope=0.2, inplace=True)
40
+
41
+ # initialization
42
+ # mutil.initialize_weights([self.conv1, self.conv2, self.conv3, self.conv4, self.conv5], 0.1)
43
+
44
+ def forward(self, x):
45
+ x1 = self.lrelu(self.conv1(x))
46
+ x2 = self.lrelu(self.conv2(torch.cat((x, x1), 1)))
47
+ x3 = self.lrelu(self.conv3(torch.cat((x, x1, x2), 1)))
48
+ x4 = self.lrelu(self.conv4(torch.cat((x, x1, x2, x3), 1)))
49
+ x5 = self.conv5(torch.cat((x, x1, x2, x3, x4), 1))
50
+ return x5 * 0.2 + x
51
+
52
+
53
+ class RRDB(nn.Module):
54
+ '''Residual in Residual Dense Block'''
55
+
56
+ def __init__(self, nf, gc=32):
57
+ super(RRDB, self).__init__()
58
+ self.RDB1 = ResidualDenseBlock_5C(nf, gc)
59
+ self.RDB2 = ResidualDenseBlock_5C(nf, gc)
60
+ self.RDB3 = ResidualDenseBlock_5C(nf, gc)
61
+
62
+ def forward(self, x):
63
+ out = self.RDB1(x)
64
+ out = self.RDB2(out)
65
+ out = self.RDB3(out)
66
+ return out * 0.2 + x
67
+
68
+
69
+ class RRDBNet(nn.Module):
70
+ def __init__(self, in_nc, out_nc, nf, nb, gc=32):
71
+ super(RRDBNet, self).__init__()
72
+ RRDB_block_f = functools.partial(RRDB, nf=nf, gc=gc)
73
+
74
+ self.conv_first = nn.Conv2d(in_nc, nf, 3, 1, 1, bias=True)
75
+ self.RRDB_trunk = make_layer(RRDB_block_f, nb)
76
+ self.trunk_conv = nn.Conv2d(nf, nf, 3, 1, 1, bias=True)
77
+ #### upsampling
78
+ self.upconv1 = nn.Conv2d(nf, nf, 3, 1, 1, bias=True)
79
+ self.upconv2 = nn.Conv2d(nf, nf, 3, 1, 1, bias=True)
80
+ self.HRconv = nn.Conv2d(nf, nf, 3, 1, 1, bias=True)
81
+ self.conv_last = nn.Conv2d(nf, out_nc, 3, 1, 1, bias=True)
82
+
83
+ self.lrelu = nn.LeakyReLU(negative_slope=0.2, inplace=True)
84
+
85
+ def forward(self, x):
86
+ fea = self.conv_first(x)
87
+ trunk = self.trunk_conv(self.RRDB_trunk(fea))
88
+ fea = fea + trunk
89
+
90
+ fea = self.lrelu(self.upconv1(F.interpolate(fea, scale_factor=2, mode='nearest')))
91
+ fea = self.lrelu(self.upconv2(F.interpolate(fea, scale_factor=2, mode='nearest')))
92
+ out = self.conv_last(self.lrelu(self.HRconv(fea)))
93
+
94
+ return out