-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPgnet_structure.py
274 lines (208 loc) · 9.8 KB
/
Pgnet_structure.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
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
"""
@author: lsl
E-mail: [email protected]
"""
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
kernelsize_temp = 3
kernelsize_temp2 = 5
padding_mode = 'circular'
def log(base, x):
return np.log(x) / np.log(base)
class simple_net(nn.Module):
def __init__(self,
input_channel: int,
output_channel: int,
kernelsize: int = 3):
super(simple_net, self).__init__()
self.net = nn.Sequential(
nn.Conv2d(input_channel, output_channel, kernelsize, stride=1, padding_mode=padding_mode,
padding=int(kernelsize // 2)),
nn.BatchNorm2d(output_channel),
nn.LeakyReLU())
def forward(self, x: torch.Tensor):
return self.net(x)
class simple_net_res(nn.Module):
def __init__(self,
input_channel: int,
output_channel: int,
kernelsize: int = 3):
super(simple_net_res, self).__init__()
self.net = nn.Sequential(
nn.Conv2d(input_channel, output_channel, kernelsize, stride=1, padding_mode=padding_mode,
padding=int(kernelsize // 2)),
nn.LeakyReLU())
def forward(self, x: torch.Tensor):
return self.net(x)
class basic_net(nn.Module):
def __init__(self,
input_channel: int,
output_channel: int,
mid_channel: int = 64,
kernelsize=kernelsize_temp):
super(basic_net, self).__init__()
self.conv1 = nn.Sequential(
nn.Conv2d(input_channel, mid_channel, kernelsize, stride=1, padding_mode=padding_mode,
padding=int(kernelsize // 2)),
nn.BatchNorm2d(mid_channel),
nn.LeakyReLU()) # Lrelu
self.conv2 = nn.Sequential(
nn.Conv2d(mid_channel, output_channel, kernelsize, stride=1, padding_mode=padding_mode,
padding=int(kernelsize // 2)),
nn.BatchNorm2d(output_channel))
def forward(self, x: torch.Tensor):
return self.conv2(self.conv1(x))
class basic_net_nobn(nn.Module):
def __init__(self,
input_channel: int,
output_channel: int,
mid_channel: int = 64,
kernelsize=kernelsize_temp):
super(basic_net_nobn, self).__init__()
self.conv1 = nn.Sequential(
nn.Conv2d(input_channel, mid_channel, kernelsize, stride=1, padding_mode=padding_mode,
padding=int(kernelsize // 2)),
nn.LeakyReLU()) # Lrelu
self.conv2 = nn.Conv2d(mid_channel, output_channel, kernelsize, stride=1, padding_mode=padding_mode,
padding=int(kernelsize // 2))
def forward(self, x: torch.Tensor):
temp = self.conv1(x)
temp2 = self.conv2(temp)
return temp2
# downsample pan image
class pan_net(nn.Module):
def __init__(self,
input_channel: int,
output_channel: int,
mid_channel: int = 64,
kernelsize=3,
stride0=1,
stride1=1):
super(pan_net, self).__init__()
self.output_ch = output_channel
self.conv1 = nn.Sequential(
nn.Conv2d(input_channel, mid_channel, kernelsize, stride=[stride0, stride0],
padding_mode=padding_mode,
padding=int(kernelsize // 2)),
nn.BatchNorm2d(mid_channel),
nn.LeakyReLU())
self.conv2 = nn.Sequential(
nn.Conv2d(mid_channel, output_channel, kernelsize, stride=[stride1, stride1],
padding_mode=padding_mode,
padding=int(kernelsize // 2)),
nn.BatchNorm2d(output_channel))
def forward(self, x: torch.Tensor):
return self.conv2(self.conv1(x))
class res_dense_net(nn.Module):
def __init__(self,
in_size,
pan_size,
endmember_num=32,
kernelsize=3,
pan_dim=1,
ratio=4,
padding=2):
super(res_dense_net, self).__init__()
self.deconv = nn.Sequential(
nn.Upsample(scale_factor=ratio, mode='bicubic'),
simple_net(endmember_num, endmember_num, 1))
self.pan_pro = pan_net(1, pan_dim, mid_channel=32, stride0=int(np.sqrt(pan_size/(ratio*in_size))),
stride1=int(np.sqrt(pan_size/(ratio*in_size))))
self.fuse = pan_abunstd(endmember_num=endmember_num)
def forward(self, w1, w2, b1, b2, abun, pan, dim_band=1):
return self.fuse(w1, w2, b1, b2, self.pan_pro(pan), self.deconv(abun))
class pan_net2(nn.Module): # pixel attention
def __init__(self,
endmember_num=32,
pan_dim=16,
kernelsize=3
):
super(pan_net2, self).__init__()
self.fuse2 = pan_abunstd(endmember_num=endmember_num)
self.weight = simple_net(endmember_num, endmember_num)
self.act = nn.Sigmoid()
def forward(self, w1, w2, b1, b2, abun, pan, dim_band=1):
temp1 = self.fuse2(w1, w2, b1, b2, pan, abun)
return temp1 * self.act(self.weight(temp1)) + abun
# PDIN
class pan_abunstd(nn.Module):
def __init__(self, endmember_num=30):
super(pan_abunstd, self).__init__()
self.multiply_weight = simple_net_res(1, endmember_num, kernelsize=3)
self.plus_weight = simple_net_res(1, endmember_num, kernelsize=3)
def forward(self, w1, w2, b1, b2, pan, abun, dim_band=1):
# print(pan.shape)
update_weight = torch.sigmoid(F.relu(pan - w1 * torch.std(abun, dim=1, keepdim=True) - b1))
update_weight2 = torch.sigmoid(F.relu(w2 * torch.std(abun, dim=1, keepdim=True) + b2 - pan))
update_weight = update_weight + update_weight2
result0 = self.multiply_weight(pan) * update_weight * abun + self.plus_weight(pan) * update_weight
return result0 + abun
class Pg_net(nn.Module):
def __init__(self, band=126, endmember_num=10, ratio=16, abun_block_num=4, pan_dim=1,
hs_size=4, pan_size=64, up_chan_num=10):
super(Pg_net, self).__init__()
self.band = band
self.endmember_num = endmember_num
self.up_chan_num = up_chan_num
self.ratio = ratio
self.abun_block_num = abun_block_num # attention block number
self.pan_dim = pan_dim
self.pan_blocks0 = 1 # every attention block could have more sub-attention block
self.w1 = nn.Parameter(torch.tensor(1.0))
self.w2 = nn.Parameter(torch.tensor(-1.0))
self.b1 = nn.Parameter(torch.tensor(0.3))
self.b2 = nn.Parameter(torch.tensor(0.3))
self.upsample16 = nn.Upsample(scale_factor=self.ratio, mode='bicubic')
def pan_dict(endmember_num, pan_dim):
return nn.ModuleList(
[pan_net2(endmember_num=endmember_num, pan_dim=pan_dim, kernelsize=kernelsize_temp) for _ in
range(self.pan_blocks0)])
def one_conv(in_ch, out_ch, ks):
return nn.Sequential(
nn.Conv2d(in_ch, out_ch, ks),
nn.BatchNorm2d(out_ch),
nn.LeakyReLU())
self.encode_filter = simple_net(self.band, self.endmember_num, kernelsize=1)
self.decode_filter = basic_net(self.endmember_num, self.band, kernelsize=1)
self.up_res4 = res_dense_net(hs_size, pan_size, endmember_num=self.endmember_num)
self.up_res16 = res_dense_net(hs_size*int(np.sqrt(self.ratio)), pan_size, endmember_num=self.endmember_num)
# attention blocks
self.abun_process_net = nn.ModuleList(
[pan_dict(endmember_num, pan_dim) for _ in range(self.abun_block_num)])
# last convolution in every attention block
self.conv_temp0_list = nn.ModuleList(
[one_conv(self.endmember_num, self.endmember_num, 1) for _ in range(self.abun_block_num)])
# part 3 last
self.dr_layer2 = simple_net(self.abun_block_num * self.endmember_num, self.endmember_num,
kernelsize=kernelsize_temp) # 降维
def encode_forward(self, images_hs, dim_band=1):
return self.encode_filter(images_hs)
def abun_pan_pro(self, abun, pan, i2, dim_band=1):
temp0 = (self.abun_process_net[i2])[0](self.w1, self.w2, self.b1, self.b2, abun, pan)
if self.pan_blocks0 > 1:
for i0 in range(1, self.pan_blocks0):
temp0 = (self.abun_process_net[i2])[i0](self.w1, self.w2, self.b1, self.b2, temp0, pan)
return self.conv_temp0_list[i2](temp0) + abun
def abun_pan_pro1(self, abun, pan, dim_band=1):
temp1 = self.abun_pan_pro(abun, pan, 0)
result = temp1
for i2 in range(1, self.abun_block_num):
temp1 = self.abun_pan_pro(temp1, pan, i2)
result = torch.cat((result, temp1), dim_band)
return self.dr_layer2(result)
def decode_forward(self, abun_last, dim_band=1):
# decode to spectral
return self.decode_filter(abun_last)
def forward(self, images_hs, images_pan, dim_band=1):
# part 1
abun = self.encode_forward(images_hs)
# part 2
abun4 = self.up_res4(self.w1, self.w2, self.b1, self.b2, abun, images_pan)
abun16 = self.up_res16(self.w1, self.w2, self.b1, self.b2, abun4, images_pan) + self.upsample16(abun)
# part 3
abun_last = self.abun_pan_pro1(abun16, images_pan) + self.upsample16(abun)
# part 4
hrhs = self.decode_forward(abun_last)
return hrhs