-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathnet.py
592 lines (485 loc) · 22.3 KB
/
net.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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import math
import random
import pdb
EPSILON = 1e-10
def var(x, dim=0):
x_zero_meaned = x - x.mean(dim).expand_as(x)
return x_zero_meaned.pow(2).mean(dim)
class MultConst(nn.Module):
def forward(self, input):
return 255*input
class UpsampleReshape_eval(torch.nn.Module):
def __init__(self):
super(UpsampleReshape_eval, self).__init__()
self.up = nn.Upsample(scale_factor=2)
def forward(self, x1, x2):
x2 = self.up(x2)
shape_x1 = x1.size()
shape_x2 = x2.size()
left = 0
right = 0
top = 0
bot = 0
if shape_x1[3] != shape_x2[3]:
lef_right = shape_x1[3] - shape_x2[3]
if lef_right%2 is 0.0:
left = int(lef_right/2)
right = int(lef_right/2)
else:
left = int(lef_right / 2)
right = int(lef_right - left)
if shape_x1[2] != shape_x2[2]:
top_bot = shape_x1[2] - shape_x2[2]
if top_bot%2 is 0.0:
top = int(top_bot/2)
bot = int(top_bot/2)
else:
top = int(top_bot / 2)
bot = int(top_bot - top)
reflection_padding = [left, right, top, bot]
reflection_pad = nn.ReflectionPad2d(reflection_padding)
x2 = reflection_pad(x2)
return x2
# Dense convolution unit
class DenseConv2d(torch.nn.Module):
def __init__(self, in_channels, out_channels, kernel_size, stride):
super(DenseConv2d, self).__init__()
self.dense_conv = ConvLayer(in_channels, out_channels, kernel_size, stride)
def forward(self, x):
out = self.dense_conv(x)
out = torch.cat([x, out], 1)
return out
def conv1x1(in_planes, out_planes, stride=1):
"""1x1 convolution"""
return nn.Conv2d(in_planes, out_planes, kernel_size=1, stride=stride, bias=False)
class qkv_transform(nn.Conv1d):
"""Conv1d for qkv_transform"""
def _make_layer(self, block, planes, blocks, kernel_size=56, stride=1, dilate=False):
norm_layer = self._norm_layer
downsample = None
previous_dilation = self.dilation
if dilate:
self.dilation *= stride
stride = 1
if stride != 1 or self.inplanes != planes * block.expansion:
downsample = nn.Sequential(
conv1x1(self.inplanes, planes * block.expansion, stride),
norm_layer(planes * block.expansion),
)
layers = []
layers.append(block(self.inplanes, planes, stride, downsample, groups=self.groups,
base_width=self.base_width, dilation=previous_dilation,
norm_layer=norm_layer, kernel_size=kernel_size))
self.inplanes = planes * block.expansion
if stride != 1:
kernel_size = kernel_size // 2
for _ in range(1, blocks):
layers.append(block(self.inplanes, planes, groups=self.groups,
base_width=self.base_width, dilation=self.dilation,
norm_layer=norm_layer, kernel_size=kernel_size))
return nn.Sequential(*layers)
# Dense Block unit
# light version
class DenseBlock_light(torch.nn.Module):
def __init__(self, in_channels, out_channels, kernel_size, stride):
super(DenseBlock_light, self).__init__()
# out_channels_def = 16
out_channels_def = int(in_channels / 2)
# out_channels_def = out_channels
denseblock = []
denseblock += [ConvLayer(in_channels, out_channels_def, kernel_size, stride),
ConvLayer(out_channels_def, out_channels, 1, stride)]
self.denseblock = nn.Sequential(*denseblock)
def forward(self, x):
out = self.denseblock(x)
return out
class ConvLayer(torch.nn.Module):
def __init__(self, in_channels, out_channels, kernel_size, stride, is_last=False):
super(ConvLayer, self).__init__()
reflection_padding = int(np.floor(kernel_size / 2))
self.reflection_pad = nn.ReflectionPad2d(reflection_padding)
self.conv2d = nn.Conv2d(in_channels, out_channels, kernel_size, stride)
self.dropout = nn.Dropout2d(p=0.5)
self.is_last = is_last
def forward(self, x):
out = self.reflection_pad(x)
out = self.conv2d(out)
if self.is_last is False:
# out = F.normalize(out)
out = F.relu(out, inplace=True)
# out = self.dropout(out)
return out
# Convolution operation
class f_ConvLayer(torch.nn.Module):
def __init__(self, in_channels, out_channels, kernel_size, stride, is_last=False):
super(f_ConvLayer, self).__init__()
reflection_padding = int(np.floor(kernel_size / 2))
self.reflection_pad = nn.ReflectionPad2d(reflection_padding)
self.conv2d = nn.Conv2d(in_channels, out_channels, kernel_size, stride)
#self.batch_norm = nn.BatchNorm2d(out_channels)
self.dropout = nn.Dropout2d(p=0.5)
self.is_last = is_last
def forward(self, x):
out = self.reflection_pad(x)
out = self.conv2d(out)
#out = self.batch_norm(out)
out = F.relu(out, inplace=True)
return out
class FusionBlock_res(torch.nn.Module):
def __init__(self, channels, img_size, index):
super(FusionBlock_res, self).__init__()
self.axial_attn = AxialBlock(channels, channels//2, kernel_size=img_size)
self.axial_fusion = nn.Sequential(f_ConvLayer(2*channels, channels, 1, 1))
self.conv_fusion = nn.Sequential(f_ConvLayer(channels, channels, 1, 1))
#self.conv_fusion_bn = nn.BatchNorm2d(channels)
block = []
block += [f_ConvLayer(2*channels, channels, 1, 1),
f_ConvLayer(channels, channels, 3, 1),
f_ConvLayer(channels, channels, 3, 1)]
self.bottelblock = nn.Sequential(*block)
#self.block_bn = nn.BatchNorm2d(channels)
#self.relu = nn.ReLU(inplace=True)
def forward(self, x_ir, x_vi):
# initial fusion - conv
a_cat = torch.cat([self.axial_attn(x_ir), self.axial_attn(x_vi)], 1)
a_init = self.axial_fusion(a_cat)
x_cvi = self.conv_fusion(x_vi)
x_cir = self.conv_fusion(x_ir)
out = torch.cat([x_cvi, x_cir], 1)
out = self.bottelblock(out)
out = a_init + out
return out
# Fusion network, 4 groups of features
class Fusion_network(nn.Module):
def __init__(self, nC, fs_type):
super(Fusion_network, self).__init__()
self.fs_type = fs_type
img_size = [256,128,64,32]
#img_size = [84,42,21,10]
self.fusion_block1 = FusionBlock_res(nC[0], img_size[0], 0)
self.fusion_block2 = FusionBlock_res(nC[1], img_size[1], 1)
self.fusion_block3 = FusionBlock_res(nC[2], img_size[2], 2)
self.fusion_block4 = FusionBlock_res(nC[3], img_size[3], 3)
def forward(self, en_ir, en_vi):
f1_0 = self.fusion_block1(en_ir[0], en_vi[0])
f2_0 = self.fusion_block2(en_ir[1], en_vi[1])
f3_0 = self.fusion_block3(en_ir[2], en_vi[2])
f4_0 = self.fusion_block4(en_ir[3], en_vi[3])
return [f1_0, f2_0, f3_0, f4_0]
class Fusion_ADD(torch.nn.Module):
def forward(self, en_ir, en_vi):
temp = en_ir + en_vi
return temp
class Fusion_AVG(torch.nn.Module):
def forward(self, en_ir, en_vi):
temp = (en_ir + en_vi) / 2
return temp
class Fusion_MAX(torch.nn.Module):
def forward(self, en_ir, en_vi):
temp = torch.max(en_ir, en_vi)
return temp
class Fusion_SPA(torch.nn.Module):
def forward(self, en_ir, en_vi):
shape = en_ir.size()
spatial_type = 'mean'
# calculate spatial attention
spatial1 = spatial_attention(en_ir, spatial_type)
spatial2 = spatial_attention(en_vi, spatial_type)
# get weight map, soft-max
spatial_w1 = torch.exp(spatial1) / (torch.exp(spatial1) + torch.exp(spatial2) + EPSILON)
spatial_w2 = torch.exp(spatial2) / (torch.exp(spatial1) + torch.exp(spatial2) + EPSILON)
spatial_w1 = spatial_w1.repeat(1, shape[1], 1, 1)
spatial_w2 = spatial_w2.repeat(1, shape[1], 1, 1)
tensor_f = spatial_w1 * en_ir + spatial_w2 * en_vi
return tensor_f
# spatial attention
def spatial_attention(tensor, spatial_type='sum'):
spatial = []
if spatial_type is 'mean':
spatial = tensor.mean(dim=1, keepdim=True)
elif spatial_type is 'sum':
spatial = tensor.sum(dim=1, keepdim=True)
return spatial
# fuison strategy based on nuclear-norm (channel attention form NestFuse)
class Fusion_Nuclear(torch.nn.Module):
def forward(self, en_ir, en_vi):
shape = en_ir.size()
# calculate channel attention
global_p1 = nuclear_pooling(en_ir)
global_p2 = nuclear_pooling(en_vi)
# get weight map
global_p_w1 = global_p1 / (global_p1 + global_p2 + EPSILON)
global_p_w2 = global_p2 / (global_p1 + global_p2 + EPSILON)
global_p_w1 = global_p_w1.repeat(1, 1, shape[2], shape[3])
global_p_w2 = global_p_w2.repeat(1, 1, shape[2], shape[3])
tensor_f = global_p_w1 * en_ir + global_p_w2 * en_vi
return tensor_f
# sum of S V for each chanel
def nuclear_pooling(tensor):
shape = tensor.size()
vectors = torch.zeros(1, shape[1], 1, 1).cuda()
for i in range(shape[1]):
u, s, v = torch.svd(tensor[0, i, :, :] + EPSILON)
s_sum = torch.sum(s)
vectors[0, i, 0, 0] = s_sum
return vectors
# Fusion strategy, two type
class Fusion_strategy(nn.Module):
def __init__(self, fs_type):
super(Fusion_strategy, self).__init__()
self.fs_type = fs_type
self.fusion_add = Fusion_ADD()
self.fusion_avg = Fusion_AVG()
self.fusion_max = Fusion_MAX()
self.fusion_spa = Fusion_SPA()
self.fusion_nuc = Fusion_Nuclear()
def forward(self, en_ir, en_vi):
if self.fs_type is 'add':
fusion_operation = self.fusion_add
elif self.fs_type is 'avg':
fusion_operation = self.fusion_avg
elif self.fs_type is 'max':
fusion_operation = self.fusion_max
elif self.fs_type is 'spa':
fusion_operation = self.fusion_spa
elif self.fs_type is 'nuclear':
fusion_operation = self.fusion_nuc
f1_0 = fusion_operation(en_ir[0], en_vi[0])
f2_0 = fusion_operation(en_ir[1], en_vi[1])
f3_0 = fusion_operation(en_ir[2], en_vi[2])
f4_0 = fusion_operation(en_ir[3], en_vi[3])
return [f1_0, f2_0, f3_0, f4_0]
# NestFuse network - light, no desnse
class NestFuse_light2_nodense(nn.Module):
def __init__(self, nb_filter, input_nc=1, output_nc=1, deepsupervision=True):
super(NestFuse_light2_nodense, self).__init__()
self.deepsupervision = deepsupervision
block = DenseBlock_light
output_filter = 16
kernel_size = 3
stride = 1
self.pool = nn.MaxPool2d(2, 2)
self.up = nn.Upsample(scale_factor=2)
self.up_eval = UpsampleReshape_eval()
# encoder
self.conv0 = ConvLayer(input_nc, output_filter, 1, stride)
self.DB1_0 = block(output_filter, nb_filter[0], kernel_size, 1)
self.DB2_0 = block(nb_filter[0], nb_filter[1], kernel_size, 1)
self.DB3_0 = block(nb_filter[1], nb_filter[2], kernel_size, 1)
self.DB4_0 = block(nb_filter[2], nb_filter[3], kernel_size, 1)
# decoder
self.DB1_1 = block(nb_filter[0] + nb_filter[1], nb_filter[0], kernel_size, 1)
self.DB2_1 = block(nb_filter[1] + nb_filter[2], nb_filter[1], kernel_size, 1)
self.DB3_1 = block(nb_filter[2] + nb_filter[3], nb_filter[2], kernel_size, 1)
# short connection
self.DB1_2 = block(nb_filter[0] * 2 + nb_filter[1], nb_filter[0], kernel_size, 1)
self.DB2_2 = block(nb_filter[1] * 2+ nb_filter[2], nb_filter[1], kernel_size, 1)
self.DB1_3 = block(nb_filter[0] * 3 + nb_filter[1], nb_filter[0], kernel_size, 1)
if self.deepsupervision:
self.conv1 = ConvLayer(nb_filter[0], output_nc, 1, stride)
self.conv2 = ConvLayer(nb_filter[0], output_nc, 1, stride)
self.conv3 = ConvLayer(nb_filter[0], output_nc, 1, stride)
# self.conv4 = ConvLayer(nb_filter[0], output_nc, 1, stride)
else:
self.conv_out = ConvLayer(nb_filter[0], output_nc, 1, stride)
def encoder(self, input):
x = self.conv0(input)
x1_0 = self.DB1_0(x)
x2_0 = self.DB2_0(self.pool(x1_0))
x3_0 = self.DB3_0(self.pool(x2_0))
x4_0 = self.DB4_0(self.pool(x3_0))
# x5_0 = self.DB5_0(self.pool(x4_0))
return [x1_0, x2_0, x3_0, x4_0]
def decoder_train(self, f_en):
x1_1 = self.DB1_1(torch.cat([f_en[0], self.up(f_en[1])], 1))
x2_1 = self.DB2_1(torch.cat([f_en[1], self.up(f_en[2])], 1))
x1_2 = self.DB1_2(torch.cat([f_en[0], x1_1, self.up(x2_1)], 1))
x3_1 = self.DB3_1(torch.cat([f_en[2], self.up(f_en[3])], 1))
x2_2 = self.DB2_2(torch.cat([f_en[1], x2_1, self.up(x3_1)], 1))
x1_3 = self.DB1_3(torch.cat([f_en[0], x1_1, x1_2, self.up(x2_2)], 1))
if self.deepsupervision:
output1 = self.conv1(x1_1)
output2 = self.conv2(x1_2)
output3 = self.conv3(x1_3)
# output4 = self.conv4(x1_4)
return [output1, output2, output3]
else:
output = self.conv_out(x1_3)
return [output]
def decoder_eval(self, f_en):
x1_1 = self.DB1_1(torch.cat([f_en[0], self.up_eval(f_en[0], f_en[1])], 1))
x2_1 = self.DB2_1(torch.cat([f_en[1], self.up_eval(f_en[1], f_en[2])], 1))
x1_2 = self.DB1_2(torch.cat([f_en[0], x1_1, self.up_eval(f_en[0], x2_1)], 1))
x3_1 = self.DB3_1(torch.cat([f_en[2], self.up_eval(f_en[2], f_en[3])], 1))
x2_2 = self.DB2_2(torch.cat([f_en[1], x2_1, self.up_eval(f_en[1], x3_1)], 1))
x1_3 = self.DB1_3(torch.cat([f_en[0], x1_1, x1_2, self.up_eval(f_en[0], x2_2)], 1))
if self.deepsupervision:
output1 = self.conv1(x1_1)
output2 = self.conv2(x1_2)
output3 = self.conv3(x1_3)
# output4 = self.conv4(x1_4)
return [output1, output2, output3]
else:
output = self.conv_out(x1_3)
return [output]
class RFN_decoder(nn.Module):
def __init__(self, nb_filter, input_nc=1, output_nc=1, deepsupervision=True):
super(RFN_decoder, self).__init__()
self.deepsupervision = deepsupervision
block = DenseBlock_light
output_filter = 16
kernel_size = 3
stride = 1
self.pool = nn.MaxPool2d(2, 2)
self.up = nn.Upsample(scale_factor=2)
self.up_eval = UpsampleReshape_eval()
# decoder
self.DB1_1 = block(nb_filter[0] + nb_filter[1], nb_filter[0], kernel_size, 1)
self.DB2_1 = block(nb_filter[1] + nb_filter[2], nb_filter[1], kernel_size, 1)
self.DB3_1 = block(nb_filter[2] + nb_filter[3], nb_filter[2], kernel_size, 1)
# short connection
self.DB1_2 = block(nb_filter[0] * 2 + nb_filter[1], nb_filter[0], kernel_size, 1)
self.DB2_2 = block(nb_filter[1] * 2+ nb_filter[2], nb_filter[1], kernel_size, 1)
self.DB1_3 = block(nb_filter[0] * 3 + nb_filter[1], nb_filter[0], kernel_size, 1)
if self.deepsupervision:
self.conv1 = ConvLayer(nb_filter[0], output_nc, 1, stride)
self.conv2 = ConvLayer(nb_filter[0], output_nc, 1, stride)
self.conv3 = ConvLayer(nb_filter[0], output_nc, 1, stride)
# self.conv4 = ConvLayer(nb_filter[0], output_nc, 1, stride)
else:
self.conv_out = ConvLayer(nb_filter[0], output_nc, 1, stride)
def decoder_train(self, f_en):
x1_1 = self.DB1_1(torch.cat([f_en[0], self.up(f_en[1])], 1))
x2_1 = self.DB2_1(torch.cat([f_en[1], self.up(f_en[2])], 1))
x1_2 = self.DB1_2(torch.cat([f_en[0], x1_1, self.up(x2_1)], 1))
x3_1 = self.DB3_1(torch.cat([f_en[2], self.up(f_en[3])], 1))
x2_2 = self.DB2_2(torch.cat([f_en[1], x2_1, self.up(x3_1)], 1))
x1_3 = self.DB1_3(torch.cat([f_en[0], x1_1, x1_2, self.up(x2_2)], 1))
if self.deepsupervision:
output1 = self.conv1(x1_1)
output2 = self.conv2(x1_2)
output3 = self.conv3(x1_3)
# output4 = self.conv4(x1_4)
return [output1, output2, output3]
else:
output = self.conv_out(x1_3)
return [output]
def decoder_eval(self, f_en):
x1_1 = self.DB1_1(torch.cat([f_en[0], self.up_eval(f_en[0], f_en[1])], 1))
x2_1 = self.DB2_1(torch.cat([f_en[1], self.up_eval(f_en[1], f_en[2])], 1))
x1_2 = self.DB1_2(torch.cat([f_en[0], x1_1, self.up_eval(f_en[0], x2_1)], 1))
x3_1 = self.DB3_1(torch.cat([f_en[2], self.up_eval(f_en[2], f_en[3])], 1))
x2_2 = self.DB2_2(torch.cat([f_en[1], x2_1, self.up_eval(f_en[1], x3_1)], 1))
x1_3 = self.DB1_3(torch.cat([f_en[0], x1_1, x1_2, self.up_eval(f_en[0], x2_2)], 1))
if self.deepsupervision:
output1 = self.conv1(x1_1)
output2 = self.conv2(x1_2)
output3 = self.conv3(x1_3)
# output4 = self.conv4(x1_4)
return [output1, output2, output3]
else:
output = self.conv_out(x1_3)
return [output]
class AxialAttention(nn.Module):
def __init__(self, in_planes, out_planes, groups=8, kernel_size=56,
stride=1, bias=False, width=False):
assert (in_planes % groups == 0) and (out_planes % groups == 0)
super(AxialAttention, self).__init__()
self.in_planes = in_planes
self.out_planes = out_planes
self.groups = groups
self.group_planes = out_planes // groups
self.kernel_size = kernel_size
self.stride = stride
self.bias = bias
self.width = width
# Multi-head self attention
self.qkv_transform = qkv_transform(in_planes, out_planes * 2, kernel_size=1, stride=1,
padding=0, bias=False)
self.bn_qkv = nn.BatchNorm1d(out_planes * 2)
self.bn_similarity = nn.BatchNorm2d(groups * 3)
self.bn_output = nn.BatchNorm1d(out_planes * 2)
# Position embedding
self.relative = nn.Parameter(torch.randn(self.group_planes * 2, kernel_size * 2 - 1), requires_grad=True)
query_index = torch.arange(kernel_size).unsqueeze(0)
key_index = torch.arange(kernel_size).unsqueeze(1)
relative_index = key_index - query_index + kernel_size - 1
self.register_buffer('flatten_index', relative_index.view(-1))
if stride > 1:
self.pooling = nn.AvgPool2d(stride, stride=stride)
self.reset_parameters()
def forward(self, x):
if self.width:
x = x.permute(0, 2, 1, 3)
else:
x = x.permute(0, 3, 1, 2) # N, W, C, H
N, W, C, H = x.shape
x = x.contiguous().view(N * W, C, H)
# Transformations
qkv = self.bn_qkv(self.qkv_transform(x))
q, k, v = torch.split(qkv.reshape(N * W, self.groups, self.group_planes * 2, H), [self.group_planes // 2, self.group_planes // 2, self.group_planes], dim=2)
# Calculate position embedding
all_embeddings = torch.index_select(self.relative, 1, self.flatten_index).view(self.group_planes * 2, self.kernel_size, self.kernel_size)
q_embedding, k_embedding, v_embedding = torch.split(all_embeddings, [self.group_planes // 2, self.group_planes // 2, self.group_planes], dim=0)
#pdb.set_trace()
qr = torch.einsum('bgci,cij->bgij', q, q_embedding)
kr = torch.einsum('bgci,cij->bgij', k, k_embedding).transpose(2, 3)
qk = torch.einsum('bgci, bgcj->bgij', q, k)
stacked_similarity = torch.cat([qk, qr, kr], dim=1)
stacked_similarity = self.bn_similarity(stacked_similarity).view(N * W, 3, self.groups, H, H).sum(dim=1)
#stacked_similarity = self.bn_qr(qr) + self.bn_kr(kr) + self.bn_qk(qk)
# (N, groups, H, H, W)
similarity = F.softmax(stacked_similarity, dim=3)
sv = torch.einsum('bgij,bgcj->bgci', similarity, v)
sve = torch.einsum('bgij,cij->bgci', similarity, v_embedding)
stacked_output = torch.cat([sv, sve], dim=-1).view(N * W, self.out_planes * 2, H)
output = self.bn_output(stacked_output).view(N, W, self.out_planes, 2, H).sum(dim=-2)
if self.width:
output = output.permute(0, 2, 1, 3)
else:
output = output.permute(0, 2, 3, 1)
if self.stride > 1:
output = self.pooling(output)
return output
def reset_parameters(self):
self.qkv_transform.weight.data.normal_(0, math.sqrt(1. / self.in_planes))
#nn.init.uniform_(self.relative, -0.1, 0.1)
nn.init.normal_(self.relative, 0., math.sqrt(1. / self.group_planes))
class AxialBlock(nn.Module):
expansion = 2
def __init__(self, inplanes, planes, stride=1, downsample=None, groups=1,
base_width=64, dilation=1, norm_layer=None, kernel_size=56):
super(AxialBlock, self).__init__()
if norm_layer is None:
norm_layer = nn.BatchNorm2d
width = int(planes * (base_width / 64.))
# Both self.conv2 and self.downsample layers downsample the input when stride != 1
self.conv_down = conv1x1(inplanes, width)
self.bn1 = norm_layer(width)
self.hight_block = AxialAttention(width, width, groups=groups, kernel_size=kernel_size)
self.width_block = AxialAttention(width, width, groups=groups, kernel_size=kernel_size, stride=stride, width=True)
self.conv_up = conv1x1(width, planes * self.expansion)
self.bn2 = norm_layer(planes * self.expansion)
self.relu = nn.ReLU(inplace=True)
self.downsample = downsample
self.stride = stride
def forward(self, x):
identity = x
out = self.conv_down(x)
out = self.bn1(out)
out = self.relu(out)
# print(out.shape)
out = self.hight_block(out)
out = self.width_block(out)
out = self.relu(out)
out = self.conv_up(out)
out = self.bn2(out)
if self.downsample is not None:
identity = self.downsample(x)
out += identity
out = self.relu(out)
return out