Spaces:
Sleeping
Sleeping
Thiên Phúc commited on
Commit ·
e633859
1
Parent(s): cd22d1e
add RRDB file
Browse files- RRDBNet_arch.py +78 -0
RRDBNet_arch.py
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import functools
|
| 2 |
+
import torch
|
| 3 |
+
import torch.nn as nn
|
| 4 |
+
import torch.nn.functional as F
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def make_layer(block, n_layers):
|
| 8 |
+
layers = []
|
| 9 |
+
for _ in range(n_layers):
|
| 10 |
+
layers.append(block())
|
| 11 |
+
return nn.Sequential(*layers)
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
class ResidualDenseBlock_5C(nn.Module):
|
| 15 |
+
def __init__(self, nf=64, gc=32, bias=True):
|
| 16 |
+
super(ResidualDenseBlock_5C, self).__init__()
|
| 17 |
+
# gc: growth channel, i.e. intermediate channels
|
| 18 |
+
self.conv1 = nn.Conv2d(nf, gc, 3, 1, 1, bias=bias)
|
| 19 |
+
self.conv2 = nn.Conv2d(nf + gc, gc, 3, 1, 1, bias=bias)
|
| 20 |
+
self.conv3 = nn.Conv2d(nf + 2 * gc, gc, 3, 1, 1, bias=bias)
|
| 21 |
+
self.conv4 = nn.Conv2d(nf + 3 * gc, gc, 3, 1, 1, bias=bias)
|
| 22 |
+
self.conv5 = nn.Conv2d(nf + 4 * gc, nf, 3, 1, 1, bias=bias)
|
| 23 |
+
self.lrelu = nn.LeakyReLU(negative_slope=0.2, inplace=True)
|
| 24 |
+
|
| 25 |
+
# initialization
|
| 26 |
+
# mutil.initialize_weights([self.conv1, self.conv2, self.conv3, self.conv4, self.conv5], 0.1)
|
| 27 |
+
|
| 28 |
+
def forward(self, x):
|
| 29 |
+
x1 = self.lrelu(self.conv1(x))
|
| 30 |
+
x2 = self.lrelu(self.conv2(torch.cat((x, x1), 1)))
|
| 31 |
+
x3 = self.lrelu(self.conv3(torch.cat((x, x1, x2), 1)))
|
| 32 |
+
x4 = self.lrelu(self.conv4(torch.cat((x, x1, x2, x3), 1)))
|
| 33 |
+
x5 = self.conv5(torch.cat((x, x1, x2, x3, x4), 1))
|
| 34 |
+
return x5 * 0.2 + x
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
class RRDB(nn.Module):
|
| 38 |
+
'''Residual in Residual Dense Block'''
|
| 39 |
+
|
| 40 |
+
def __init__(self, nf, gc=32):
|
| 41 |
+
super(RRDB, self).__init__()
|
| 42 |
+
self.RDB1 = ResidualDenseBlock_5C(nf, gc)
|
| 43 |
+
self.RDB2 = ResidualDenseBlock_5C(nf, gc)
|
| 44 |
+
self.RDB3 = ResidualDenseBlock_5C(nf, gc)
|
| 45 |
+
|
| 46 |
+
def forward(self, x):
|
| 47 |
+
out = self.RDB1(x)
|
| 48 |
+
out = self.RDB2(out)
|
| 49 |
+
out = self.RDB3(out)
|
| 50 |
+
return out * 0.2 + x
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
class RRDBNet(nn.Module):
|
| 54 |
+
def __init__(self, in_nc, out_nc, nf, nb, gc=32):
|
| 55 |
+
super(RRDBNet, self).__init__()
|
| 56 |
+
RRDB_block_f = functools.partial(RRDB, nf=nf, gc=gc)
|
| 57 |
+
|
| 58 |
+
self.conv_first = nn.Conv2d(in_nc, nf, 3, 1, 1, bias=True)
|
| 59 |
+
self.RRDB_trunk = make_layer(RRDB_block_f, nb)
|
| 60 |
+
self.trunk_conv = nn.Conv2d(nf, nf, 3, 1, 1, bias=True)
|
| 61 |
+
#### upsampling
|
| 62 |
+
self.upconv1 = nn.Conv2d(nf, nf, 3, 1, 1, bias=True)
|
| 63 |
+
self.upconv2 = nn.Conv2d(nf, nf, 3, 1, 1, bias=True)
|
| 64 |
+
self.HRconv = nn.Conv2d(nf, nf, 3, 1, 1, bias=True)
|
| 65 |
+
self.conv_last = nn.Conv2d(nf, out_nc, 3, 1, 1, bias=True)
|
| 66 |
+
|
| 67 |
+
self.lrelu = nn.LeakyReLU(negative_slope=0.2, inplace=True)
|
| 68 |
+
|
| 69 |
+
def forward(self, x):
|
| 70 |
+
fea = self.conv_first(x)
|
| 71 |
+
trunk = self.trunk_conv(self.RRDB_trunk(fea))
|
| 72 |
+
fea = fea + trunk
|
| 73 |
+
|
| 74 |
+
fea = self.lrelu(self.upconv1(F.interpolate(fea, scale_factor=2, mode='nearest')))
|
| 75 |
+
fea = self.lrelu(self.upconv2(F.interpolate(fea, scale_factor=2, mode='nearest')))
|
| 76 |
+
out = self.conv_last(self.lrelu(self.HRconv(fea)))
|
| 77 |
+
|
| 78 |
+
return out
|