-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
134 lines (109 loc) · 4.36 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
"""
@author: Jebearssica
"""
from torch import nn, cat, Tensor
from torch.nn import functional as F
class AdaptiveNorm(nn.Module):
def __init__(self, n):
super(AdaptiveNorm, self).__init__()
self.w_0 = nn.Parameter(Tensor([1.0]))
self.w_1 = nn.Parameter(Tensor([0.0]))
self.bn = nn.BatchNorm2d(n, momentum=0.999, eps=0.0001)
def forward(self, x):
return self.w_0 * x + self.w_1 * self.bn(x)
class LFN(nn.Module):
"""
our LFN with 3 Linear fusion blocks
"""
def __init__(self, c, norm=AdaptiveNorm):
super(LFN, self).__init__()
self.LNB1 = nn.Sequential(
nn.Conv2d(6, c, kernel_size=3, stride=1,
padding=1, dilation=1, bias=False),
norm(c),
nn.LeakyReLU(0.2, inplace=True),
nn.Conv2d(c, c, kernel_size=3, stride=1,
padding=2, dilation=2, bias=False),
norm(c),
nn.LeakyReLU(0.2, inplace=True),
nn.Conv2d(c, c, kernel_size=3, stride=1,
padding=4, dilation=4, bias=False),
norm(c),
nn.LeakyReLU(0.2, inplace=True),
nn.Conv2d(c, c, kernel_size=3, stride=1,
padding=8, dilation=8, bias=False),
norm(c),
nn.LeakyReLU(0.2, inplace=True),
nn.Conv2d(c, c, kernel_size=3, stride=1,
padding=16, dilation=16, bias=False),
norm(c),
nn.LeakyReLU(0.2, inplace=True),
nn.Conv2d(c, c, kernel_size=3, stride=1,
padding=32, dilation=32, bias=False),
norm(c),
nn.LeakyReLU(0.2, inplace=True),
nn.Conv2d(c, c, kernel_size=3, stride=1,
padding=1, dilation=1, bias=False),
norm(c),
nn.LeakyReLU(0.2, inplace=True),
nn.Conv2d(c, 3, kernel_size=1, stride=1, padding=0, dilation=1),
)
self.LNB2 = nn.Sequential(
nn.Conv2d(3, c, 3, bias=False,
padding=1, dilation=1),
norm(c),
nn.ReLU(inplace=True),
nn.Conv2d(c, c, 3, bias=False,
padding=1, dilation=1),
norm(c),
nn.ReLU(inplace=True),
nn.Conv2d(c, 3, 1),
)
self.LNB3 = nn.Sequential(
nn.Conv2d(6, c, kernel_size=3, stride=1,
padding=1, dilation=1, bias=False),
norm(c),
nn.LeakyReLU(0.2, inplace=True),
nn.Conv2d(c, c, kernel_size=3, stride=1,
padding=2, dilation=2, bias=False),
norm(c),
nn.LeakyReLU(0.2, inplace=True),
nn.Conv2d(c, c, kernel_size=3, stride=1,
padding=4, dilation=4, bias=False),
norm(c),
nn.LeakyReLU(0.2, inplace=True),
nn.Conv2d(c, c, kernel_size=3, stride=1,
padding=8, dilation=8, bias=False),
norm(c),
nn.LeakyReLU(0.2, inplace=True),
nn.Conv2d(c, c, kernel_size=3, stride=1,
padding=16, dilation=16, bias=False),
norm(c),
nn.LeakyReLU(0.2, inplace=True),
nn.Conv2d(c, c, kernel_size=3, stride=1,
padding=32, dilation=32, bias=False),
norm(c),
nn.LeakyReLU(0.2, inplace=True),
nn.Conv2d(c, c, kernel_size=3, stride=1,
padding=1, dilation=1, bias=False),
norm(c),
nn.LeakyReLU(0.2, inplace=True),
nn.Conv2d(c, 3, kernel_size=1, stride=1, padding=0, dilation=1),
nn.Sigmoid()
)
def forward(self, lrRF, hrFF):
_, _, h_lrx, w_lrx = lrRF.size()
_, _, h_hrx, w_hrx = hrFF.size()
LrGuidedUp = F.interpolate(
lrRF, (h_hrx, w_hrx), mode='bilinear', align_corners=True)
HrDown = F.interpolate(
hrFF, (h_lrx, w_lrx), mode='bilinear', align_corners=True)
xLr = self.LNB1(cat([lrRF, HrDown], dim=1))
A1 = F.interpolate(
xLr, (h_hrx, w_hrx), mode='bilinear', align_corners=True)
output1 = (1-A1)*LrGuidedUp+A1*hrFF
A2 = self.LNB2(output1)
output2 = (1-A2)*LrGuidedUp+A2*hrFF
A3 = self.LNB3(cat([output1, output2], dim=1))
output3 = (1-A3)*LrGuidedUp+A3*hrFF
return output3