-
Notifications
You must be signed in to change notification settings - Fork 8
/
model.py
196 lines (167 loc) · 8.28 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
# import matplotlib.pyplot as plt
# from skimage import morphology
# from torchvision import transforms
class Net(nn.Module):
def __init__(self, upscale_factor):
super(Net, self).__init__()
self.upscale_factor = upscale_factor
self.init_feature = nn.Conv2d(3, 64, 3, 1, 1, bias=True)
self.deep_feature = RDG(G0=64, C=4, G=24, n_RDB=4)
self.pam = PAM(64)
self.fusion = nn.Sequential(
RDB(G0=128, C=4, G=32),
CALayer(128),
nn.Conv2d(128, 64, kernel_size=1, stride=1, padding=0, bias=True))
self.reconstruct = RDG(G0=64, C=4, G=24, n_RDB=4)
self.upscale = nn.Sequential(
nn.Conv2d(64, 64 * upscale_factor ** 2, 1, 1, 0, bias=True),
nn.PixelShuffle(upscale_factor),
nn.Conv2d(64, 3, 3, 1, 1, bias=True))
def forward(self, x_left, x_right, is_training):
x_left_upscale = F.interpolate(x_left, scale_factor=self.upscale_factor, mode='bicubic', align_corners=False)
x_right_upscale = F.interpolate(x_right, scale_factor=self.upscale_factor, mode='bicubic', align_corners=False)
buffer_left = self.init_feature(x_left)
buffer_right = self.init_feature(x_right)
buffer_left, catfea_left = self.deep_feature(buffer_left)
buffer_right, catfea_right = self.deep_feature(buffer_right)
if is_training == 1:
buffer_leftT, buffer_rightT, (M_right_to_left, M_left_to_right), (V_left, V_right)\
= self.pam(buffer_left, buffer_right, catfea_left, catfea_right, is_training)
if is_training == 0:
buffer_leftT, buffer_rightT \
= self.pam(buffer_left, buffer_right, catfea_left, catfea_right, is_training)
buffer_leftF = self.fusion(torch.cat([buffer_left, buffer_leftT], dim=1))
buffer_rightF = self.fusion(torch.cat([buffer_right, buffer_rightT], dim=1))
buffer_leftF, _ = self.reconstruct(buffer_leftF)
buffer_rightF, _ = self.reconstruct(buffer_rightF)
out_left = self.upscale(buffer_leftF) + x_left_upscale
out_right = self.upscale(buffer_rightF) + x_right_upscale
if is_training == 1:
return out_left, out_right, (M_right_to_left, M_left_to_right), (V_left, V_right)
if is_training == 0:
return out_left, out_right
class one_conv(nn.Module):
def __init__(self, G0, G):
super(one_conv, self).__init__()
self.conv = nn.Conv3d(G0, G, kernel_size=3, stride=1, padding=1, bias=True)
self.relu = nn.LeakyReLU(0.1, inplace=True)
def forward(self, x):
output = self.relu(self.conv(x))
return torch.cat((x, output), dim=1)
class RDB(nn.Module):
def __init__(self, G0, C, G):
super(RDB, self).__init__()
convs = []
for i in range(C):
convs.append(one_conv(G0+i*G, G))
self.conv = nn.Sequential(*convs)
self.LFF = nn.Conv3d(G0+C*G, G0, kernel_size=1, stride=1, padding=0, bias=True)
def forward(self, x):
out = self.conv(x)
lff = self.LFF(out)
return lff + x
class RDG(nn.Module):
def __init__(self, G0, C, G, n_RDB):
super(RDG, self).__init__()
self.n_RDB = n_RDB
RDBs = []
for i in range(n_RDB):
RDBs.append(RDB(G0, C, G))
self.RDB = nn.Sequential(*RDBs)
self.conv = nn.Conv3d(G0*n_RDB, G0, kernel_size=1, stride=1, padding=0, bias=True)
def forward(self, x):
buffer = x
temp = []
for i in range(self.n_RDB):
buffer = self.RDB[i](buffer)
temp.append(buffer)
buffer_cat = torch.cat(temp, dim=1)
out = self.conv(buffer_cat)
return out, buffer_cat
class CALayer(nn.Module):
def __init__(self, channel):
super(CALayer, self).__init__()
self.avg_pool = nn.AdaptiveAvgPool2d(1)
self.conv_du = nn.Sequential(
nn.Conv2d(channel, channel//16, 1, padding=0, bias=True),
nn.LeakyReLU(0.1, inplace=True),
nn.Conv2d(channel//16, channel, 1, padding=0, bias=True),
nn.Sigmoid())
def forward(self, x):
y = self.avg_pool(x)
y = self.conv_du(y)
return x * y
class ResB(nn.Module):
def __init__(self, channels):
super(ResB, self).__init__()
self.body = nn.Sequential(
nn.Conv2d(channels, channels, 3, 1, 1, groups=4, bias=True),
nn.LeakyReLU(0.1, inplace=True),
nn.Conv2d(channels, channels, 3, 1, 1, groups=4, bias=True),
)
def __call__(self,x):
out = self.body(x)
return out + x
class PAM(nn.Module):
def __init__(self, channels):
super(PAM, self).__init__()
self.bq = nn.Conv2d(4*channels, channels, 1, 1, 0, groups=4, bias=True)
self.bs = nn.Conv2d(4*channels, channels, 1, 1, 0, groups=4, bias=True)
self.softmax = nn.Softmax(-1)
self.rb = ResB(4 * channels)
self.bn = nn.BatchNorm2d(4 * channels)
def __call__(self, x_left, x_right, catfea_left, catfea_right, is_training):
b, c0, h0, w0 = x_left.shape
Q = self.bq(self.rb(self.bn(catfea_left)))
b, c, h, w = Q.shape
Q = Q - torch.mean(Q, 3).unsqueeze(3).repeat(1, 1, 1, w)
K = self.bs(self.rb(self.bn(catfea_right)))
K = K - torch.mean(K, 3).unsqueeze(3).repeat(1, 1, 1, w)
score = torch.bmm(Q.permute(0, 2, 3, 1).contiguous().view(-1, w, c), # (B*H) * Wl * C
K.permute(0, 2, 1, 3).contiguous().view(-1, c, w)) # (B*H) * C * Wr
M_right_to_left = self.softmax(score) # (B*H) * Wl * Wr
M_left_to_right = self.softmax(score.permute(0, 2, 1)) # (B*H) * Wr * Wl
M_right_to_left_relaxed = M_Relax(M_right_to_left, num_pixels=2)
V_left = torch.bmm(M_right_to_left_relaxed.contiguous().view(-1, w).unsqueeze(1),
M_left_to_right.permute(0, 2, 1).contiguous().view(-1, w).unsqueeze(2)
).detach().contiguous().view(b, 1, h, w) # (B*H*Wr) * Wl * 1
M_left_to_right_relaxed = M_Relax(M_left_to_right, num_pixels=2)
V_right = torch.bmm(M_left_to_right_relaxed.contiguous().view(-1, w).unsqueeze(1), # (B*H*Wl) * 1 * Wr
M_right_to_left.permute(0, 2, 1).contiguous().view(-1, w).unsqueeze(2)
).detach().contiguous().view(b, 1, h, w) # (B*H*Wr) * Wl * 1
V_left_tanh = torch.tanh(5 * V_left)
V_right_tanh = torch.tanh(5 * V_right)
x_leftT = torch.bmm(M_right_to_left, x_right.permute(0, 2, 3, 1).contiguous().view(-1, w0, c0)
).contiguous().view(b, h0, w0, c0).permute(0, 3, 1, 2) # B, C0, H0, W0
x_rightT = torch.bmm(M_left_to_right, x_left.permute(0, 2, 3, 1).contiguous().view(-1, w0, c0)
).contiguous().view(b, h0, w0, c0).permute(0, 3, 1, 2) # B, C0, H0, W0
out_left = x_left * (1 - V_left_tanh.repeat(1, c0, 1, 1)) + x_leftT * V_left_tanh.repeat(1, c0, 1, 1)
out_right = x_right * (1 - V_right_tanh.repeat(1, c0, 1, 1)) + x_rightT * V_right_tanh.repeat(1, c0, 1, 1)
if is_training == 1:
return out_left, out_right, \
(M_right_to_left.contiguous().view(b, h, w, w), M_left_to_right.contiguous().view(b, h, w, w)),\
(V_left_tanh, V_right_tanh)
if is_training == 0:
return out_left, out_right
def M_Relax(M, num_pixels):
_, u, v = M.shape
M_list = []
M_list.append(M.unsqueeze(1))
for i in range(num_pixels):
pad = nn.ZeroPad2d(padding=(0, 0, i+1, 0))
pad_M = pad(M[:, :-1-i, :])
M_list.append(pad_M.unsqueeze(1))
for i in range(num_pixels):
pad = nn.ZeroPad2d(padding=(0, 0, 0, i+1))
pad_M = pad(M[:, i+1:, :])
M_list.append(pad_M.unsqueeze(1))
M_relaxed = torch.sum(torch.cat(M_list, 1), dim=1)
return M_relaxed
if __name__ == "__main__":
net = Net(upscale_factor=4)
total = sum([param.nelement() for param in net.parameters()])
print(' Number of params: %.2fM' % (total / 1e6))