-
Notifications
You must be signed in to change notification settings - Fork 4
/
Mobility_LPAN_L1.py
229 lines (153 loc) · 6.72 KB
/
Mobility_LPAN_L1.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# -*- coding: utf-8 -*-
"""
Created on Fri Jun 24 19:51:09 2022
@author: WiCi
"""
import torch
import torch.nn as nn
import numpy as np
import torch.nn.init as init
from torch.nn.utils import weight_norm
from einops import rearrange
P1=2*2
L1=2*6
class SELayer(nn.Module):
def __init__(self, channel, reduction=1):
super(SELayer, self).__init__()
# Squeeze
self.avg_pool = nn.AdaptiveAvgPool2d(1)
# Excitation
self.fc = nn.Sequential(
nn.Linear(channel, channel // reduction, bias=True),
# nn.LeakyReLU(negative_slope=0.2),
# nn.ReLU(),
# nn.Linear(channel // reduction, channel, bias=True),
# nn.Sigmoid()
nn.Tanh()
)
def forward(self, x):
b, c, _, _ = x.size()
y = self.avg_pool(x).view(b, c)
y = self.fc(y).view(b, c, 1, 1)
return x * y.expand_as(x)
class _Res_Blocka(nn.Module):
def __init__(self, in_ch, out_ch):
super(_Res_Blocka, self).__init__()
self.res_conv = weight_norm(nn.Conv2d(in_ch, out_ch, kernel_size=3, padding=2,dilation=2))
self.res_conb = weight_norm(nn.Conv2d(out_ch, out_ch, kernel_size=3, padding=1))
self.relu = nn.LeakyReLU(negative_slope=0.2)
self.ca = SELayer(out_ch)
def forward(self, x,al=1):
y = self.relu(self.res_conv(x))
y = self.res_conb(y)
y = self.ca(y)
y *= al
y = torch.add(y, x)
return y
class _Res_Block(nn.Module):
def __init__(self, in_ch, out_ch):
super(_Res_Block, self).__init__()
self.res_conv = weight_norm(nn.Conv2d(in_ch, out_ch, kernel_size=3, padding=2, groups=16,dilation=2))
self.res_conb = weight_norm(nn.Conv2d(out_ch, out_ch, kernel_size=3, padding=1, groups=4,dilation=1))
self.res_cona = weight_norm(nn.Conv2d(out_ch, out_ch, kernel_size=1))
self.relu = nn.LeakyReLU(negative_slope=0.2)
self.ca = SELayer(out_ch)
def forward(self, x,al=1):
y = self.relu(self.res_conv(x))
y = self.relu(self.res_conb(y))
y = self.res_cona(y)
y = self.ca(y)
y *= al
y = torch.add(y, x)
return y
class FeatureExtraction(nn.Module):
def __init__(self):
super(FeatureExtraction, self).__init__()
numf=96
self.res1 = _Res_Block(numf,numf)
self.conv4 = weight_norm(nn.Conv2d(numf, numf, (3, 3), (1, 1), (1, 1)))
self.convt_F = nn.Upsample(size=None, scale_factor=(1,2), mode='nearest', align_corners=None)
self.LReLus = nn.LeakyReLU(negative_slope=0.2)
m_body = [
_Res_Blocka(numf,numf) for _ in range(2)
]
self.body = nn.Sequential(*m_body)
def forward(self, x):
for i in range(2):
out = self.res1(x)
out = self.body(out)
out = self.LReLus(self.conv4(self.convt_F(out)))
return out
class FeatureExtraction1(nn.Module):
def __init__(self):
super(FeatureExtraction1, self).__init__()
numf=96
self.res1 = _Res_Block(numf,numf)
self.conv4 = nn.Conv2d(numf, numf, (3, 3), (1, 1), (1, 1))
self.convt_F = nn.Upsample(size=None, scale_factor=(1,2), mode='nearest', align_corners=None)
self.LReLus = nn.LeakyReLU(negative_slope=0.2)
def forward(self, x):
for i in range(4):
out = self.res1(x)
out = self.LReLus(self.conv4(self.convt_F(out)))
return out
class FeatureExtractionT(nn.Module):
def __init__(self):
super(FeatureExtractionT, self).__init__()
numf=96
self.conv0 = weight_norm(nn.Conv2d(64, numf, (3, 3), (1, 1), padding=1))
self.res1 = _Res_Block(numf,numf)
self.conv4 = nn.Conv2d(numf, numf, (3, 3), (1, 1), (1, 1))
self.convt_F = nn.Upsample(size=(256,L1), mode='nearest', align_corners=None)
self.LReLus = nn.LeakyReLU(negative_slope=0.2)
def forward(self, x):
x = rearrange(x, 'b c h w -> b w h c')
x = self.conv0(x)
for i in range(4):
out = self.res1(x)
out = self.LReLus(self.conv4(self.convt_F(out)))
x = rearrange(x, 'b w h c -> b c h w')
return out
class ImageReconstruction(nn.Module):
def __init__(self):
super(ImageReconstruction, self).__init__()
self.conv_R = weight_norm(nn.Conv2d(96, L1, (3, 3), (1, 1), padding=1))
self.convt_I = nn.Upsample(size=None, scale_factor=(1,2), mode='nearest', align_corners=None)
self.conv_1 = nn.Conv2d(P1, L1, (3, 3), (1, 1), padding=1)
def forward(self, LR, convt_F):
convt_I = self.conv_1(self.convt_I(LR))
conv_R = self.conv_R(convt_F)
HR = convt_I+conv_R
return HR
class ImageReconstruction2(nn.Module):
def __init__(self):
super(ImageReconstruction2, self).__init__()
self.conv_R = weight_norm(nn.Conv2d(96, L1, (3, 3), (1, 1), padding=1))
self.convt_I = nn.Upsample(size=None, scale_factor=(1,2), mode='nearest', align_corners=None)
self.conv_1 = nn.Conv2d(L1, L1, (3, 3), (1, 1), padding=1)
def forward(self, LR, convt_F):
convt_I = self.conv_1(self.convt_I(LR))
conv_R = self.conv_R(convt_F)
HR = convt_I+conv_R
return HR
class LPAN_L(nn.Module):
def __init__(self):
super(LPAN_L, self).__init__()
numf=96
self.conv0 = weight_norm(nn.Conv2d(P1, numf, (3, 3), (1, 1), padding=1))
self.FeatureExtraction1 = FeatureExtraction()
self.FeatureExtraction2 = FeatureExtraction()
self.FeatureExtraction3 = FeatureExtraction1()
self.ImageReconstruction1 = ImageReconstruction()
self.ImageReconstruction2 = ImageReconstruction2()
# self.convf = nn.Conv2d(numf, L1, (3, 3), (1, 1), padding=1)
def forward(self, LR):
LR1 = self.conv0(LR)
convt_F1 = self.FeatureExtraction1(LR1)
HR_2 = self.ImageReconstruction1(LR, convt_F1)
convt_F2 = self.FeatureExtraction2(convt_F1)
HR_4 = self.ImageReconstruction2(HR_2, convt_F2)
convt_F3 = self.FeatureExtraction3(convt_F2)
# HR_8 = self.convf(convt_F3)
HR_8 = self.ImageReconstruction2(HR_4, convt_F3)
return HR_2, HR_4, HR_8