-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
298 lines (223 loc) · 9.62 KB
/
models.py
File metadata and controls
298 lines (223 loc) · 9.62 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
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
import math
class Interp23Tap(nn.Module):
def __init__(self, ratio=4):
super(Interp23Tap, self).__init__()
assert (2 ** (round(np.log2(ratio)))) == ratio, 'Error: Only resize factors power of 2'
self.ratio = ratio
# 定义CDF23系数
CDF23 = np.asarray([0.5, 0.305334091185, 0, -0.072698593239, 0, 0.021809577942,
0, -0.005192756653, 0, 0.000807762146, 0, -0.000060081482])
CDF23 = [element * 2 for element in CDF23]
BaseCoeff = np.expand_dims(np.concatenate([np.flip(CDF23[1:]), CDF23]), axis=-1)
BaseCoeff = np.expand_dims(BaseCoeff, axis=(0, 1))
# 转换为torch tensor
self.BaseCoeff = torch.from_numpy(BaseCoeff.astype(np.float32))
def forward(self, x):
if x.dim() == 3:
x = x.unsqueeze(0)
squeeze_output = True
else:
squeeze_output = False
batch_size, channels, height, width = x.shape
img = x
# 将BaseCoeff扩展到匹配的通道数并移动到相同设备
BaseCoeff_expanded = self.BaseCoeff.repeat(channels, 1, 1, 1).to(x.device)
for z in range(int(np.log2(self.ratio))):
# 创建上采样图像
new_height = (2 ** (z + 1)) * height
new_width = (2 ** (z + 1)) * width
I1LRU = torch.zeros(batch_size, channels, new_height, new_width,
dtype=img.dtype, device=img.device)
if z == 0:
I1LRU[:, :, 1::2, 1::2] = img
else:
I1LRU[:, :, ::2, ::2] = img
conv = nn.Conv2d(in_channels=channels, out_channels=channels, padding=(11, 0),
kernel_size=BaseCoeff_expanded.shape, groups=channels, bias=False, padding_mode='circular')
conv.weight.data = BaseCoeff_expanded
conv.weight.requires_grad = False
t = conv(torch.transpose(I1LRU, 2, 3))
img = conv(torch.transpose(t, 2, 3))
if squeeze_output:
img = img.squeeze(0)
return img
class TorchInterp23(nn.Module):
def __init__(self, start_point=1):
super(TorchInterp23, self).__init__()
self.start_point = start_point
# 预计算卷积核
basecoeff = np.array([[-4.63495665e-03, -3.63442646e-03, 3.84904063e-18,
5.76678319e-03, 1.08358664e-02, 1.01980790e-02,
-9.31747402e-18, -1.75033181e-02, -3.17660068e-02,
-2.84531643e-02, 1.85181518e-17, 4.42450253e-02,
7.71733386e-02, 6.70554910e-02, -2.85299239e-17,
-1.01548683e-01, -1.78708388e-01, -1.60004642e-01,
3.61741232e-17, 2.87940558e-01, 6.25431459e-01,
8.97067600e-01, 1.00107877e+00, 8.97067600e-01,
6.25431459e-01, 2.87940558e-01, 3.61741232e-17,
-1.60004642e-01, -1.78708388e-01, -1.01548683e-01,
-2.85299239e-17, 6.70554910e-02, 7.71733386e-02,
4.42450253e-02, 1.85181518e-17, -2.84531643e-02,
-3.17660068e-02, -1.75033181e-02, -9.31747402e-18,
1.01980790e-02, 1.08358664e-02, 5.76678319e-03,
3.84904063e-18, -3.63442646e-03, -4.63495665e-03]])
coeff = np.dot(basecoeff.T, basecoeff)
coeff = torch.FloatTensor(coeff)
coeff = coeff.unsqueeze(0).unsqueeze(0) # (1, 1, 45, 45)
self.register_buffer('coeff', coeff)
def unpool(self, x):
batch_size, channels, height, width = x.shape
new_height = height * 4
new_width = width * 4
out = torch.zeros(batch_size, channels, new_height, new_width,
device=x.device, dtype=x.dtype)
if self.start_point == 1:
out[:, :, 1::4, 1::4] = x
elif self.start_point == 0:
out[:, :, 0::4, 0::4] = x
return out
def tf_inter23(self, x):
groups = x.shape[1]
kernel = self.coeff.repeat(groups, 1, 1, 1)
padding = self.coeff.shape[-1] // 2
out = F.conv2d(x, kernel, padding=padding, groups=groups)
return out
def forward(self, x):
temp = self.unpool(x)
temp = self.tf_inter23(temp)
return temp
class HPFilter(nn.Module):
def __init__(self):
super(HPFilter, self).__init__()
kernel = torch.ones(1, 1, 5, 5) / 25.0
self.register_buffer('kernel', kernel)
def forward(self, x):
# x shape: (batch, channels, height, width)
# Apply average pooling as low-pass filter
low_pass = F.conv2d(x, self.kernel.repeat(x.size(1), 1, 1, 1),
padding=2, groups=x.size(1))
# High-pass = original - low-pass
high_pass = x - low_pass
return high_pass
class Resize(nn.Module):
def __init__(self, target_size):
super(Resize, self).__init__()
self.target_size = target_size
def forward(self, x):
# x shape: (batch, channels, height, width)
return F.interpolate(x, size=self.target_size, mode='bicubic', align_corners=True)
class Duplicate(nn.Module):
def __init__(self, target_channels):
super(Duplicate, self).__init__()
self.target_channels = target_channels
def forward(self, x):
# x shape: (batch, channels, height, width)
return x.repeat(1, self.target_channels, 1, 1)
class ConvBlock1(nn.Module):
def __init__(self, in_channels=32, nf=32, block_name='1'):
super(ConvBlock1, self).__init__()
self.conv1 = nn.Conv2d(in_channels, nf, 3, padding=1)
self.relu1 = nn.ReLU()
self.conv2 = nn.Conv2d(nf, nf, 3, padding=1)
def forward(self, x):
residual = x
out = self.conv1(x)
out = self.relu1(out)
out = self.conv2(out)
return out + residual
# Main networks
class Pannet(nn.Module):
def __init__(self, msi_channels=4, pan_channels=1):
super(Pannet, self).__init__()
self.hp_filter = HPFilter()
self.interp23 = TorchInterp23()
# Mixed input processing
self.mixed_conv = nn.Conv2d(msi_channels + pan_channels, 32, 3, padding=1)
self.relu = nn.ReLU()
# Residual blocks
self.res_blocks = nn.Sequential(
ConvBlock1(32, 32, '1'),
ConvBlock1(32, 32, '2'),
ConvBlock1(32, 32, '3'),
ConvBlock1(32, 32, '4')
)
# Final convolution
self.final_conv = nn.Conv2d(32, msi_channels, 3, padding=1)
def forward(self, msi, pan):
# msi: (batch, channels, 16, 16)
# pan: (batch, channels, 64, 64)
h_msi = self.hp_filter(msi)
h_pan = self.hp_filter(pan)
re_h_msi = self.interp23(h_msi)
re_msi = self.interp23(msi)
mixed = torch.cat([re_h_msi, h_pan], dim=1)
mixed1 = self.relu(self.mixed_conv(mixed))
x = self.res_blocks(mixed1)
x = self.final_conv(x)
last = x + re_msi
return last
class FusNet(nn.Module):
def __init__(self, msi_channels=4, pan_channels=1):
super(FusNet, self).__init__()
self.interp23 = TorchInterp23()
self.duplicate = Duplicate(msi_channels)
self.sub_conv = nn.Conv2d(msi_channels, 32, 3, padding=1)
self.res_blocks = nn.Sequential(
ConvBlock1(32, 32, '1'),
ConvBlock1(32, 32, '2'),
ConvBlock1(32, 32, '3'),
ConvBlock1(32, 32, '4')
)
self.final_conv = nn.Conv2d(32, msi_channels, 3, padding=1)
def forward(self, msi, pan):
msi_inputs1 = self.interp23(msi)
pan_inputs1 = self.duplicate(pan)
sub = pan_inputs1 - msi_inputs1
sub1 = self.sub_conv(sub)
c1 = self.res_blocks(sub1)
c4 = self.final_conv(c1)
c6 = msi_inputs1 + c4
return c6
class PNNNet(nn.Module):
def __init__(self, msi_channels=4, pan_channels=1):
super(PNNNet, self).__init__()
self.interp23 = TorchInterp23()
self.conv1 = nn.Conv2d(msi_channels + pan_channels, 64, 9, padding=4)
self.relu1 = nn.ReLU()
self.conv2 = nn.Conv2d(64, 32, 5, padding=2)
self.relu2 = nn.ReLU()
self.conv3 = nn.Conv2d(32, msi_channels, 5, padding=2)
self.relu3 = nn.ReLU()
def forward(self, msi, pan):
msi_inputs1 = self.interp23(msi)
mixed = torch.cat([msi_inputs1, pan], dim=1)
mixed1 = self.relu1(self.conv1(mixed))
mixed1 = self.relu2(self.conv2(mixed1))
output = self.relu3(self.conv3(mixed1))
return output
# Usage example:
if __name__ == "__main__":
# Test the networks
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# Pannet
pannet = Pannet(msi_channels=4, pan_channels=1).to(device)
msi = torch.randn(2, 4, 16, 16).to(device)
pan = torch.randn(2, 1, 64, 64).to(device)
output = pannet(msi, pan)
print(f"Pannet output shape: {output.shape}")
# FusNet
fusnet = FusNet(msi_channels=4, pan_channels=1).to(device)
msi = torch.randn(2, 4, 16, 16).to(device)
pan = torch.randn(2, 1, 64, 64).to(device)
output = fusnet(msi, pan)
print(f"FusNet output shape: {output.shape}")
# PNNNet
pnnnet = PNNNet(msi_channels=4, pan_channels=1).to(device)
msi = torch.randn(2, 4, 16, 16).to(device)
pan = torch.randn(2, 1, 64, 64).to(device)
output = pnnnet(msi, pan)
print(f"PNNNet output shape: {output.shape}")