-
Notifications
You must be signed in to change notification settings - Fork 0
/
SGCNNet.py
402 lines (360 loc) · 15.5 KB
/
SGCNNet.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: Gaodian Zhou(zhougaodian@cug.edu.cn)
# Pytorch implementation of SGCN
import torch
import torch.nn.functional as F
import torch.nn as nn
import numpy as np
BatchNorm2d = nn.BatchNorm2d
BatchNorm1d = nn.BatchNorm1d
device = torch.device("cuda:0")
class Bottleneck(nn.Module):
expansion = 4
def __init__(self, inplanes, planes, stride=1, dilation=1, downsample=None, fist_dilation=1, multi_grid=1):
super(Bottleneck, self).__init__()
self.conv1 = nn.Conv2d(inplanes, planes, kernel_size=1, bias=False)
self.bn1 = BatchNorm2d(planes)
self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=stride,
padding=dilation*multi_grid, dilation=dilation*multi_grid, bias=False)
self.bn2 = BatchNorm2d(planes)
self.conv3 = nn.Conv2d(planes, planes * 4, kernel_size=1, bias=False)
self.bn3 = BatchNorm2d(planes * 4)
self.relu = nn.ReLU(inplace=False)
self.relu_inplace = nn.ReLU(inplace=True)
self.downsample = downsample
self.dilation = dilation
self.stride = stride
def forward(self, x):
residual = x
out = self.conv1(x)
out = self.bn1(out)
out = self.relu(out)
out = self.conv2(out)
out = self.bn2(out)
out = self.relu(out)
out = self.conv3(out)
out = self.bn3(out)
if self.downsample is not None:
residual = self.downsample(x)
out = out + residual
out = self.relu_inplace(out)
return out
class Sobel(nn.Module):
def __init__(self,in_channel,out_channel):
super(Sobel,self).__init__()
kernel_x = [[-1.0,0.0,1.0],[-2.0,0.0,2.0],[-1.0,0.0,1.0]]
kernel_y = [[-1.0,-2.0,-1.0],[0.0,0.0,0.0],[1.0,2.0,1.0]]
kernel_x = torch.FloatTensor(kernel_x).expand(out_channel,in_channel,3,3)
kernel_x = kernel_x.type(torch.cuda.FloatTensor)
kernel_y = torch.cuda.FloatTensor(kernel_y).expand(out_channel,in_channel,3,3)
kernel_y = kernel_y.type(torch.cuda.FloatTensor)
self.weight_x = nn.Parameter(data=kernel_x, requires_grad=False).clone()
self.weight_y = nn.Parameter(data=kernel_y, requires_grad=False).clone()
self.softmax = nn.Softmax()
def forward(self,x):
b,c,h,w = x.size()
sobel_x = F.conv2d(x,self.weight_x,stride=1, padding=1)
sobel_x = torch.abs(sobel_x)
sobel_y = F.conv2d(x,self.weight_y,stride=1, padding=1)
sobel_y = torch.abs(sobel_y)
if c == 1:
sobel_x = sobel_x.view(b, h, -1)
sobel_y = sobel_y.view(b, h, -1).permute(0,2,1)
else:
sobel_x = sobel_x.view(b, c, -1)
sobel_y = sobel_y.view(b, c, -1).permute(0,2,1)
sobel_A = torch.bmm(sobel_x,sobel_y)
sobel_A = self.softmax(sobel_A)
return sobel_A
class GCNSpatial(nn.Module):
def __init__(self , channels):
super(GCNSpatial,self).__init__()
self.sobel = Sobel(channels , channels)
self.fc1 = nn.Conv1d(channels, channels, kernel_size=1, bias=False)
self.fc2 = nn.Conv1d(channels, channels, kernel_size=1, bias=False)
self.fc3 = nn.Conv1d(channels, channels, kernel_size=1, bias=False)
def normalize(self,A):
b,c,im = A.size()
out = np.array([])
for i in range(b):
A1 = A[i].to(device="cpu")
I = torch.eye(c,im)
A1 = A1 + I
# degree matrix
d = A1.sum(1)
#D = D^-1/2
D = torch.diag(torch.pow(d , -0.5))
new_A = D.mm(A1).mm(D).detach().numpy()
out = np.append(out,new_A)
out = out.reshape(b,c,im)
normalize_A = torch.from_numpy(out)
normalize_A = normalize_A.type(torch.cuda.FloatTensor)
return normalize_A
def forward(self,x):
b,c,h,w = x.size()
A = self.sobel(x)
A = self.normalize(A)
x = x.view(b, c, -1)
x = F.relu(self.fc1(A.bmm(x)))
x = F.relu(self.fc2(A.bmm(x)))
x = self.fc3(A.bmm(x))
out = x.view(b, c, h ,w)
return out
class GCNChannel(nn.Module):
def __init__(self , channels):
super(GCNChannel,self).__init__()
self.input = nn.Sequential(
nn.Conv2d(channels, channels, kernel_size=3, stride=2, padding=1),
BatchNorm2d(channels),
nn.ReLU(inplace=True)
)
self.sobel = Sobel(1,1)
self.fc1 = nn.Conv1d(channels, channels, kernel_size=1, bias=False)
self.fc2 = nn.Conv1d(channels, channels, kernel_size=1, bias=False)
self.fc3 = nn.Conv1d(channels, channels, kernel_size=1, bias=False)
# def sobel_channel(self,x):
# b,c,h,w = x.size()
# sobel = Sobel(h*w,h*w)
# return sobel
def pre(self,x):
b,c,h,w = x.size()
x = x.view(b, c, -1).permute(0, 2, 1)
x = x.view(b, 1, h*w, c)
return x
def normalize(self,A):
b,c,im = A.size()
out = np.array([])
for i in range(b):
# A = A = I
A1 = A[i].to(device="cpu")
I = torch.eye(c,im)
A1 = A1 + I
# degree matrix
d = A1.sum(1)
# D = D^-1/2
D = torch.diag(torch.pow(d , -0.5))
new_A = D.mm(A1).mm(D).detach().numpy()
out = np.append(out,new_A)
out = out.reshape(b,c,im)
normalize_A = torch.from_numpy(out)
normalize_A = normalize_A.type(torch.cuda.FloatTensor)
return normalize_A
def forward(self,x):
b,c,h,w = x.size()
x = self.input(x)
b,c,h1,w1 = x.size()
x = self.pre(x)
# A = self.sobel_channel(x)
A = self.sobel(x)
A = self.normalize(A)
x = x.view(b, -1, c)
x = F.relu(self.fc1(A.bmm(x).permute(0,2,1))).permute(0,2,1)
x = F.relu(self.fc2(A.bmm(x).permute(0,2,1))).permute(0,2,1)
x = self.fc3(A.bmm(x).permute(0,2,1))
out = x.view(b, c, h1 ,w1)
out = F.interpolate(out, size=(h,w), mode='bilinear', align_corners=True)
return out
class decoder(nn.Module):
def __init__(self, in_channels, out_channels):
super(decoder, self).__init__()
# ConvTranspose
self.up = nn.ConvTranspose2d(in_channels, out_channels, kernel_size=2, stride=2)
self.up_conv = nn.Sequential(
nn.Conv2d(in_channels, out_channels, kernel_size=3, padding=1),
nn.BatchNorm2d(out_channels),
nn.ReLU(inplace=True),
nn.Conv2d(out_channels, out_channels, kernel_size=3, padding=1),
nn.BatchNorm2d(out_channels),
nn.ReLU(inplace=True)
)
def forward(self, x_copy, x, interpolate=True):
out = self.up(x)
if interpolate:
# Iterative filling, in order to obtain better results
out = F.interpolate(out, size=(x_copy.size(2), x_copy.size(3)),
mode="bilinear", align_corners=True
)
else:
# Different filling volume
diffY = x_copy.size()[2] - x.size()[2]
diffX = x_copy.size()[3] - x.size()[3]
out = F.pad(out, (diffX // 2, diffX - diffX // 2, diffY, diffY - diffY // 2))
# Splicing
out = torch.cat([x_copy, out], dim=1)
out_conv = self.up_conv(out)
return out_conv
class TwofoldGCN(nn.Module):
def __init__(self, in_channels, out_channels, num_classes):
super(TwofoldGCN, self).__init__()
# Depthwise convolution # for spatial feature extraction
self.depth_conv=nn.Sequential(
nn.Conv2d(in_channels, out_channels, kernel_size=3, stride=1, padding=1, groups=out_channels),
BatchNorm2d(out_channels),
nn.ReLU(inplace=True),
nn.Conv2d(out_channels, out_channels, kernel_size=3, stride=1, padding=1, groups=out_channels),
BatchNorm2d(out_channels),
nn.ReLU(inplace=True),
nn.Conv2d(out_channels, out_channels, kernel_size=3, stride=1, padding=1, groups=out_channels),
BatchNorm2d(out_channels),
nn.ReLU(inplace=True),
nn.Conv2d(out_channels, out_channels, kernel_size=3, stride=1, padding=1, groups=out_channels),
BatchNorm2d(out_channels),
nn.ReLU(inplace=True),
nn.Conv2d(out_channels, out_channels, kernel_size=3, stride=1, padding=1, groups=out_channels),
BatchNorm2d(out_channels)
)
# GCN Spatial #
self.spatial_in = nn.Sequential(
nn.Conv2d(out_channels, out_channels // 2, kernel_size=3, stride=1, padding=1),
BatchNorm2d(out_channels // 2),
nn.ReLU(inplace=True)
)
self.gcn_s = GCNSpatial(out_channels // 2)
self.conv_s = nn.Sequential(
nn.Conv2d(out_channels // 2, out_channels // 2, kernel_size=3, stride=1, padding=1),
BatchNorm2d(out_channels // 2)
)
# Pointwise convolution # for channel feature extraction
self.channel_conv=nn.Sequential(
nn.Conv2d(in_channels, out_channels, kernel_size=1, stride=1),
BatchNorm2d(out_channels),
nn.ReLU(inplace=True),
nn.Conv2d(out_channels, out_channels, kernel_size=1, stride=1),
BatchNorm2d(out_channels),
nn.ReLU(inplace=True),
nn.Conv2d(out_channels, out_channels, kernel_size=1, stride=1),
BatchNorm2d(out_channels),
nn.ReLU(inplace=True),
nn.Conv2d(out_channels, out_channels, kernel_size=1, stride=1),
BatchNorm2d(out_channels),
nn.ReLU(inplace=True),
nn.Conv2d(out_channels, out_channels, kernel_size=1, stride=1),
BatchNorm2d(out_channels)
)
# GCN Channel #
self.channel_in = nn.Sequential(
nn.Conv2d(out_channels, out_channels // 2, kernel_size=1, stride=1),
BatchNorm2d(out_channels // 2),
nn.ReLU(inplace=True)
)
self.gcn_c = GCNChannel(out_channels // 2)
self.conv_c = nn.Sequential(
nn.Conv2d(out_channels // 2, out_channels // 2, kernel_size=3, stride=1, padding=1),
BatchNorm2d(out_channels // 2)
)
# output
self.combine = nn.Sequential(
nn.Conv2d(out_channels, out_channels, kernel_size=1, bias=False),
BatchNorm2d(out_channels)
)
self.output = nn.Sequential(nn.Conv2d(out_channels, out_channels, kernel_size=3, stride=1, padding=1, bias=False),
BatchNorm2d(out_channels),
nn.ReLU(out_channels),
nn.Conv2d(out_channels, num_classes, kernel_size=1, stride=1, bias=True)
)
def forward(self, x):
# GCN_Spatial
x_spatial_in = self.depth_conv(x)
x_spatial_in = self.spatial_in(x_spatial_in)
x_spatial = self.gcn_s(x_spatial_in)
x_spatial = x_spatial_in + x_spatial
# GCN_Channel
x_channel_in = self.channel_conv(x)
x_channel_in = self.channel_in(x_channel_in)
x_channel = self.gcn_c(x_channel_in)
x_channel = x_channel_in + x_channel
# out
out = torch.cat((x_spatial, x_channel), 1) + x
out = self.combine(out)
out = self.output(out)
return out
class ResNet(nn.Module):
def __init__(self, block, layers, num_classes):
self.inplanes = 128
super(ResNet, self).__init__()
# ResNet # for feature extraction
self.conv0 = nn.Sequential(
nn.Conv2d(3, 64, kernel_size=3, padding=1),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True),
nn.Conv2d(64,64, kernel_size=3, padding=1),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True),
nn.Conv2d(64,64, kernel_size=3, padding=1),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True)
)
self.conv1 = nn.Sequential(
nn.Conv2d(64, 64, kernel_size=3, stride=2, padding=1, bias=False),
BatchNorm2d(64),
nn.ReLU(inplace=True),
nn.Conv2d(64, 64, kernel_size=3, stride=1,padding=1, bias=False),
BatchNorm2d(64),
nn.ReLU(inplace=True),
nn.Conv2d(64, 128, kernel_size=3, stride=1,padding=1, bias=False)
)
self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
self.bn1 = BatchNorm2d(self.inplanes)
self.relu = nn.ReLU(inplace=False)
self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1, ceil_mode=True)
self.layer1 = self._make_layer(block, 64, layers[0])
self.layer2 = self._make_layer(block, 128, layers[1], stride=2)
self.layer3 = self._make_layer(block, 256, layers[2], stride=1, dilation=2)
self.layer4 = self._make_layer(block, 512, layers[3], stride=1, dilation=4, multi_grid=(1, 2, 4))
self.conv2 = nn.Sequential(nn.Conv2d(2048,512, kernel_size=3, stride=1, padding=1, bias=False),
BatchNorm2d(512),
nn.ReLU(512))
# TwofoldGCN # for channel and spatial feature
self.gcn_out =TwofoldGCN(512, 512, 512)
self.up1 = decoder(512, 256)
self.up2 = decoder(256, 128)
self.up3 = decoder(128, 64)
#Full connection
self.final_conv = nn.Conv2d(64, num_classes, kernel_size=1)
self._initalize_weights()
def _initalize_weights(self):
for module in self.modules():
if isinstance(module, nn.Conv2d) or isinstance(module, nn.Linear):
nn.init.kaiming_normal_(module.weight)
if module.bias is not None:
module.bias.data.zero_()
elif isinstance(module, nn.BatchNorm2d):
module.weight.data.fill_(1)
module.bias.data.zero_()
def _make_layer(self, block, planes, blocks, stride=1, dilation=1, multi_grid=1):
downsample = None
if stride != 1 or self.inplanes != planes * block.expansion:
downsample = nn.Sequential(
nn.Conv2d(self.inplanes, planes * block.expansion,
kernel_size=1, stride=stride, bias=False),
BatchNorm2d(planes * block.expansion))
layers = []
generate_multi_grid = lambda index, grids: grids[index % len(grids)] if isinstance(grids, tuple) else 1
layers.append(block(self.inplanes, planes, stride, dilation=dilation, downsample=downsample,
multi_grid=generate_multi_grid(0, multi_grid)))
self.inplanes = planes * block.expansion
for i in range(1, blocks):
layers.append(
block(self.inplanes, planes, dilation=dilation, multi_grid=generate_multi_grid(i, multi_grid)))
return nn.Sequential(*layers)
def forward(self, x):
x0 = self.conv0(x)
x1 = self.conv1(x0)
x1 = self.bn1(x1)
x1 = self.relu(x1)
x2 = self.maxpool(x1)
x2 = self.layer1(x2)
x3 = self.layer2(x2)
x3 = self.layer3(x3)
x3 = self.layer4(x3)
x3 = self.conv2(x3)
x3 = self.gcn_out(x3)
x3 = self.up1(x2,x3)
x3 = self.up2(x1,x3)
x3 = self.up3(x0,x3)
final = self.final_conv(x3)
return final
def SGCN_res50(num_classes=1):
model = ResNet(Bottleneck, [3, 4, 6, 3], num_classes)
return model