-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrrdbnet.py
More file actions
56 lines (47 loc) · 2.21 KB
/
Copy pathrrdbnet.py
File metadata and controls
56 lines (47 loc) · 2.21 KB
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
"""Real-ESRGAN generator (RRDBNet), matching the official RealESRGAN_x4plus weights.
Self-contained (no basicsr/realesrgan deps) so the tool stays lean and robust."""
import torch
from torch import nn
from torch.nn import functional as F
class ResidualDenseBlock(nn.Module):
def __init__(self, nf=64, gc=32):
super().__init__()
self.conv1 = nn.Conv2d(nf, gc, 3, 1, 1)
self.conv2 = nn.Conv2d(nf + gc, gc, 3, 1, 1)
self.conv3 = nn.Conv2d(nf + 2 * gc, gc, 3, 1, 1)
self.conv4 = nn.Conv2d(nf + 3 * gc, gc, 3, 1, 1)
self.conv5 = nn.Conv2d(nf + 4 * gc, nf, 3, 1, 1)
self.lrelu = nn.LeakyReLU(0.2, inplace=True)
def forward(self, x):
x1 = self.lrelu(self.conv1(x))
x2 = self.lrelu(self.conv2(torch.cat((x, x1), 1)))
x3 = self.lrelu(self.conv3(torch.cat((x, x1, x2), 1)))
x4 = self.lrelu(self.conv4(torch.cat((x, x1, x2, x3), 1)))
x5 = self.conv5(torch.cat((x, x1, x2, x3, x4), 1))
return x5 * 0.2 + x
class RRDB(nn.Module):
def __init__(self, nf, gc=32):
super().__init__()
self.rdb1 = ResidualDenseBlock(nf, gc)
self.rdb2 = ResidualDenseBlock(nf, gc)
self.rdb3 = ResidualDenseBlock(nf, gc)
def forward(self, x):
out = self.rdb3(self.rdb2(self.rdb1(x)))
return out * 0.2 + x
class RRDBNet(nn.Module):
def __init__(self, in_ch=3, out_ch=3, nf=64, nb=23, gc=32):
super().__init__()
self.conv_first = nn.Conv2d(in_ch, nf, 3, 1, 1)
self.body = nn.Sequential(*[RRDB(nf, gc) for _ in range(nb)])
self.conv_body = nn.Conv2d(nf, nf, 3, 1, 1)
self.conv_up1 = nn.Conv2d(nf, nf, 3, 1, 1)
self.conv_up2 = nn.Conv2d(nf, nf, 3, 1, 1)
self.conv_hr = nn.Conv2d(nf, nf, 3, 1, 1)
self.conv_last = nn.Conv2d(nf, out_ch, 3, 1, 1)
self.lrelu = nn.LeakyReLU(0.2, inplace=True)
def forward(self, x):
feat = self.conv_first(x)
feat = feat + self.conv_body(self.body(feat))
feat = self.lrelu(self.conv_up1(F.interpolate(feat, scale_factor=2, mode="nearest")))
feat = self.lrelu(self.conv_up2(F.interpolate(feat, scale_factor=2, mode="nearest")))
return self.conv_last(self.lrelu(self.conv_hr(feat)))