-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGCPNet.py
253 lines (201 loc) · 8.36 KB
/
GCPNet.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
from os import name
import torch.nn as nn
import torch
import torch.nn.functional as F
BatchNorm2d = nn.BatchNorm2d
BatchNorm1d = nn.BatchNorm1d
class SpatialGCN(nn.Module):
def __init__(self, plane):
super(SpatialGCN, self).__init__()
inter_plane = plane // 2
self.node_in1 = nn.Conv2d(plane, inter_plane, kernel_size=1)
self.node_in2 = nn.Conv2d(plane, inter_plane, kernel_size=1)
self.node_in3 = nn.Conv2d(plane, inter_plane, kernel_size=1)
self.conv_wg = nn.Conv1d(inter_plane, inter_plane, kernel_size=1, bias=False)
self.bn_wg = BatchNorm1d(inter_plane)
self.softmax = nn.Softmax(dim=2)
self.out = nn.Sequential(nn.Conv2d(inter_plane, plane, kernel_size=1))#,BatchNorm2d(plane)
def forward(self, x):
# b, c, h, w = x.size()
node_in1 = self.node_in1(x)
node_in2 = self.node_in2(x)
node_in3 = self.node_in3(x)
b,c,h,w = node_in1.size()
node_in1 = node_in1.view(b, c, -1).permute(0, 2, 1)
node_in3 = node_in3.view(b, c, -1)
node_in2 = node_in2.view(b, c, -1).permute(0, 2, 1)
AV = torch.bmm(node_in3,node_in2)
AV = self.softmax(AV)
AV = torch.bmm(node_in1, AV)
AV = AV.transpose(1, 2).contiguous()
AVW = self.conv_wg(AV)
AVW = self.bn_wg(AVW)
AVW = AVW.view(b, c, h, -1)
out = F.relu_(self.out(AVW) + x)
return out
class SpectralGCN(nn.Module):
def __init__(self, planes, ratio=4):
super(SpectralGCN, self).__init__()
self.phi = nn.Conv2d(planes, planes // ratio * 2, kernel_size=1, bias=False)
self.bn_phi = BatchNorm2d(planes // ratio * 2)
self.theta = nn.Conv2d(planes, planes // ratio, kernel_size=1, bias=False)
self.bn_theta = BatchNorm2d(planes // ratio)
# Interaction Space
# Adjacency Matrix: (-)A_g
self.conv_adj = nn.Conv1d(planes // ratio, planes // ratio, kernel_size=1, bias=False)
self.bn_adj = BatchNorm1d(planes // ratio)
# State Update Function: W_g
self.conv_wg = nn.Conv1d(planes // ratio * 2, planes // ratio * 2, kernel_size=1, bias=False)
self.bn_wg = BatchNorm1d(planes // ratio * 2)
# last fc
self.conv3 = nn.Conv2d(planes // ratio * 2, planes, kernel_size=1, bias=False)
#self.bn3 = BatchNorm2d(planes)
def to_matrix(self, x):
n, c, h, w = x.size()
x = x.view(n, c, -1)
return x
def forward(self, x):
x_sqz, b = x, x
x_sqz = self.phi(x_sqz)
x_sqz = self.bn_phi(x_sqz)
x_sqz = self.to_matrix(x_sqz)
b = self.theta(b)
b = self.bn_theta(b)
b = self.to_matrix(b)
# Project
z_idt = torch.matmul(x_sqz, b.transpose(1, 2))
# # # # Interaction Space # # # #
z = z_idt.transpose(1, 2).contiguous()
z = self.conv_adj(z)
z = self.bn_adj(z)
z = z.transpose(1, 2).contiguous()
# Laplacian smoothing: (I - A_g)Z => Z - A_gZ
z += z_idt
z = self.conv_wg(z)
z = self.bn_wg(z)
# # # # Re-projection Space # # # #
# Re-project
y = torch.matmul(z, b)
n, _, h, w = x.size()
y = y.view(n, -1, h, w)
y = self.conv3(y)
g_out = F.relu_(x+y)
return g_out
class DualGCN_Spatial_fist(nn.Module):
def __init__(self, inchannels):
super(DualGCN_Spatial_fist, self).__init__()
self.sGCN = SpatialGCN(inchannels)
self.conv_1 = nn.Sequential(
nn.Conv2d(inchannels, inchannels, 3, padding=1, dilation=1),
nn.ReLU(inchannels)
)
self.conv_2 = nn.Sequential(
nn.Conv2d(inchannels, inchannels, 3, padding=1, dilation=1),
nn.ReLU(inchannels)
)
self.conv_3 = nn.Sequential(
nn.Conv2d(inchannels, inchannels, 3, padding=3, dilation=3),
nn.ReLU(inchannels)
)
self.conv_4 = nn.Sequential(
nn.Conv2d(inchannels, inchannels, 3, padding=3, dilation=3),
nn.ReLU(inchannels)
)
self.conv_5 = nn.Sequential(
nn.Conv2d(inchannels*5, inchannels, 1, padding=0),
nn.ReLU(inchannels)
)
self.cGCN = SpectralGCN(inchannels)
def forward(self, x):
F_sGCN = self.sGCN(x)
conv1 = self.conv_1(F_sGCN)
conv2 = self.conv_2(conv1)
conv3 = self.conv_3(F_sGCN)
conv4 = self.conv_4(conv3)
F_DCM = self.conv_5(torch.cat([F_sGCN, conv1, conv2, conv3, conv4], dim=1))
F_cGCN = self.cGCN(F_DCM)
F_unit = F_cGCN + x
return F_unit
class ConvBlock(torch.nn.Module):
def __init__(self, input_size, output_size, kernel_size=3, stride=1, padding=1, bias=True, activation='prelu', norm=None, pad_model=None):
super(ConvBlock, self).__init__()
self.pad_model = pad_model
self.norm = norm
self.input_size = input_size
self.output_size = output_size
self.kernel_size = kernel_size
self.stride = stride
self.padding = padding
self.bias = bias
if self.norm =='batch':
self.bn = torch.nn.BatchNorm2d(self.output_size)
elif self.norm == 'instance':
self.bn = torch.nn.InstanceNorm2d(self.output_size)
self.activation = activation
if self.activation == 'relu':
self.act = torch.nn.ReLU(True)
elif self.activation == 'prelu':
self.act = torch.nn.PReLU(init=0.5)
elif self.activation == 'lrelu':
self.act = torch.nn.LeakyReLU(0.2, True)
elif self.activation == 'tanh':
self.act = torch.nn.Tanh()
elif self.activation == 'sigmoid':
self.act = torch.nn.Sigmoid()
if self.pad_model == None:
self.conv = torch.nn.Conv2d(self.input_size, self.output_size, self.kernel_size, self.stride, self.padding, bias=self.bias)
elif self.pad_model == 'reflection':
self.padding = nn.Sequential(nn.ReflectionPad2d(self.padding))
self.conv = torch.nn.Conv2d(self.input_size, self.output_size, self.kernel_size, self.stride, 0, bias=self.bias)
def forward(self, x):
out = x
if self.pad_model is not None:
out = self.padding(out)
if self.norm is not None:
out = self.bn(self.conv(out))
else:
out = self.conv(out)
if self.activation is not None:
return self.act(out)
else:
return out
class Net(nn.Module):
def __init__(self, num_channels, base_filter, args):
super(Net, self).__init__()
inchannel=num_channels*2
interplanes=inchannel*2
self.head = ConvBlock(inchannel, interplanes, 9, 1, 4, activation='prelu', norm=None, bias = False)
self.conv1=ConvBlock(16,24 , 5, 1, 2, activation='prelu', norm=None, bias = False)
self.conv2=ConvBlock(48, 24, 3, 1, 1, activation='prelu', norm=None, bias = False)
self.head2=ConvBlock(1, 3, 9, 1, 4, activation='prelu', norm=None, bias = False)
self.gcn_basic1=DualGCN_Spatial_fist(24)
self.gcn_basic2=DualGCN_Spatial_fist(24)
self.output_conv = ConvBlock(40, num_channels, 5, 1, 2, activation=None, norm=None, bias = False)
def forward(self,l_ms,bms,pan):
pan0=self.head2(pan)
pan0=torch.cat((pan, pan0), 1)
cbms=torch.cat((bms, pan0), 1)
x0=self.head(cbms)
x1=self.conv1(x0)
s_x=self.gcn_basic1(x1)
x=self.gcn_basic2(s_x)
x=torch.cat((x,x1),1)
x=self.conv2(x)
x=torch.cat((x,x0),1)
x=self.output_conv(x)+bms
return x
if __name__ == "__main__":
from torchvision.transforms import Compose, ToTensor
def transform():
return Compose([
ToTensor(),
])
device = 'cuda' if torch.cuda.is_available() else 'cpu'
print(device)
model=Net(4) #, map_location=torch.device('cpu')
model.eval()
img=torch.ones((1,4,128,128))
l_ms=torch.ones((1,4,32,32))
pan=torch.ones((1,1,128,128))
output_end=model(l_ms,img,pan)
print(output_end.shape)